这里主要解决文件夹包含文件夹的解压缩问题。
1)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免费版本,“Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] ”点击Download可以下载,解压后里边有好多文件夹,因为不同的版本,我用的FW2.0。
2)引用SharpZipLib.dll,在项目中点击项目右键-->添加引用-->浏览,找到要添加的DLL-->确认
3)改写了文件压缩和解压缩的两个类,新建两个类名字为ZipFloClass.cs,UnZipFloClass.cs
源码如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;

using System.IO;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

/// <summary>
/// ZipFloClass 的摘要说明
/// </summary>
public class ZipFloClass
{
    public void ZipFile(string strFile, string strZip)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
            strFile += Path.DirectorySeparatorChar;
        ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
        s.SetLevel(6); // 0 - store only to 9 - means best compression
        zip(strFile, s, strFile);
        s.Finish();
        s.Close();
    }

  private void zip(string rootFilePath, string strFile, ICSharpCode.SharpZipLib.Zip.ZipOutputStream s, string staticFile)
  {
    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
    Crc32 crc = new Crc32();
    string[] filenames = Directory.GetFileSystemEntries(strFile);
    foreach (string file in filenames)
    {

      if (Directory.Exists(file))
      {
        zip(rootFilePath, file, s, staticFile);
      }

      else // 否则直接压缩文件
      {
        //打开压缩文件
        FileStream fs = File.OpenRead(file);

        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        //string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
        string tempfile = file.Replace(rootFilePath, "");
        ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(tempfile);

        entry.DateTime = DateTime.Now;
        entry.Size = fs.Length;
        fs.Close();
        crc.Reset();
        crc.Update(buffer);
        entry.Crc = crc.Value;
        s.PutNextEntry(entry);

        s.Write(buffer, 0, buffer.Length);
      }
    }
  }

}

、、、、、、、、、、、、、、、

using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;

using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;

/// <summary>
/// UnZipFloClass 的摘要说明
/// </summary>
public class UnZipFloClass
{

public string unZipFile(string TargetFile, string fileDir)
    {
        string rootFile = " ";
        try
        {
            //读取压缩文件(zip文件),准备解压缩
            ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
            ZipEntry theEntry;
            string path = fileDir;                   
            //解压出来的文件保存的路径

string rootDir = " ";                        
            //根目录下的第一个子文件夹的名称
            while ((theEntry = s.GetNextEntry()) != null)
            {
                rootDir = Path.GetDirectoryName(theEntry.Name);                          
                //得到根目录下的第一级子文件夹的名称
                if (rootDir.IndexOf("\\") >= 0)
                {
                    rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
                }
                string dir = Path.GetDirectoryName(theEntry.Name);                    
                //根目录下的第一级子文件夹的下的文件夹的名称
                string fileName = Path.GetFileName(theEntry.Name);                    
                //根目录下的文件名称
                if (dir != " " )                                                        
                    //创建根目录下的子文件夹,不限制级别
                {
                    if (!Directory.Exists(fileDir + "\\" + dir))
                    {
                        path = fileDir + "\\" + dir;                                                
                        //在指定的路径创建文件夹
                        Directory.CreateDirectory(path);
                    }
                }
                else if (dir == " " && fileName != "")                                              
                    //根目录下的文件
                {
                    path = fileDir;
                    rootFile = fileName;
                }
                else if (dir != " " && fileName != "")                                              
                    //根目录下的第一级子文件夹下的文件
                {
                    if (dir.IndexOf("\\") > 0)                                                            
                        //指定文件保存的路径
                    {
                        path = fileDir + "\\" + dir;
                    }
                }

if (dir == rootDir)                                                                                  
                    //判断是不是需要保存在根目录下的文件
                {
                    path = fileDir + "\\" + rootDir;
                }

//以下为解压缩zip文件的基本步骤
                //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
                if (fileName != String.Empty)
                {
                    FileStream streamWriter = File.Create(path + "\\" + fileName);

int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }

streamWriter.Close();
                }
            }
            s.Close();

return rootFile;
        }
        catch (Exception ex)
        {
            return "1; " + ex.Message;
        }
    }   
}

4)引用,新建一个页面,添加两个按钮,为按钮添加Click事件

源码如下

protected void Button1_Click(object sender, EventArgs e)
    {
        string[] FileProperties = new string[2];
        FileProperties[0] = "D:\\unzipped\\";//待压缩文件目录
        FileProperties[1] = "D:\\zip\\a.zip";  //压缩后的目标文件
        ZipFloClass Zc = new ZipFloClass();
        Zc.ZipFile(FileProperties[0], FileProperties[1]);

}
    protected void Button2_Click(object sender, EventArgs e)
    {
        string[] FileProperties = new string[2];
        FileProperties[0] = "D:\\zip\\b.zip";//待解压的文件
        FileProperties[1] = "D:\\unzipped\\";//解压后放置的目标目录
        UnZipFloClass UnZc = new UnZipFloClass();
        UnZc.unZipFile(FileProperties[0], FileProperties[1]);
    }

5)一切OK,可以测试一下。

C#实现文件的压缩和解压的更多相关文章

  1. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

    我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...

  2. linux下文件加密压缩和解压的方法

    一.用tar命令 对文件加密压缩和解压 压缩:tar -zcf  - filename |openssl des3 -salt -k password | dd of=filename.des3 此命 ...

  3. java实现文件的压缩和解压

    java实现文件的压缩和解压 代码压缩实现 package com.hjh.demo.zip; import java.io.BufferedInputStream; import java.io.F ...

  4. [Swift通天遁地]七、数据与安全-(9)文件的压缩和解压

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. 用jdk1.6的pack200和unpack200,对jar文件进行压缩和解压 .pack.gz

    用jdk1.6的pack200和unpack200,对jar文件进行压缩和解压 解压xxx.jar.pack.gz为xxx.jar:unpack200 -r xxx.jar.pack.gz xxx.j ...

  6. C#文件或文件夹压缩和解压

    C#文件或文件夹压缩和解压方法有很多,本文通过使用ICSharpCode.SharpZipLib.dll来进行压缩解压 1.新建一个winform项目,选择项目右键 管理NuGet程序包,搜索ICSh ...

  7. gz文件的压缩和解压

    gz文件的压缩和解压 压缩: gzip filename 解压: gunzip filename.gz

  8. 【C#公共帮助类】WinRarHelper帮助类,实现文件或文件夹压缩和解压,实战干货

    关于本文档的说明 本文档使用WinRAR方式来进行简单的压缩和解压动作,纯干货,实际项目这种压缩方式用的少一点,一般我会使用第三方的压缩dll来实现,就如同我上一个压缩类博客,压缩的是zip文件htt ...

  9. 利用c#自带的类对文件进行压缩和解压处理

    在做网络传输文件的小例子的时候,当传输的文件比较大的时候,我们通常都是将文件经过压缩之后才进行传输,以前都是利用第三方插件来对文件进行压缩的,但是现在我发现了c#自带的类库也能够实现文件的压缩,实际上 ...

  10. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

随机推荐

  1. 关于jquery 1.9以上多次点击checkbox无法选择的

    在jquery1.9之前,我们对于一个checkbox对象来进行重复选择或者取消, 我们可以使用这个方法$().attr('checked',checked);//选中 $().removeAttr( ...

  2. CSS3 background-size:cover/contain

    background-size的cover和contain指定背景图片的自适应方式,只能对整张图片进行缩放. cover是拉伸图片使之充满元素,元素肯定是被铺满的,但是图片有可能显示不全. conta ...

  3. JavaScript中常用的Document了解

    在我们使用js的时候经常会用到document 例如: document.write(""); document.getElementById(''); document.crea ...

  4. CentOS单独编译安装PHP gd库扩展

    注意:如果您已经编译安装过GD库,请重新编译安装php不带gd库成功后,执行以下操作 安装libpng wget http://jaist.dl.sourceforge.net/project/lib ...

  5. iBeacon在iPhone锁屏下有几率扫描不到蓝牙设备以及折中解决方案

    前言: 这个问题的确困扰了很久,查了国内外各种论坛.社区,都没找到一个有效的解决办法. 所幸后来用一种折中的办法解决了,但也不是从技术根本上解决的,所以有解决的兄弟还望指导下. 正文如下: 一.需求描 ...

  6. [UWP小白日记-6]页面跳转过度动画

    前言 在学习中发现页面导航默认是没有过度动画的,直接就导航过去太粗暴了( ̄へ ̄),于是打算上动画结果不言而喻自己进了坑完全不懂动画,然后就是各种疯狂(´・_・`)的搜索资料看了后终于有点头绪. 再后来 ...

  7. webkit框架的使用

    // // JSViewController.m // Library // // Created by 朱逸 on 16/7/7. // Copyright © 2016年 朱逸. All righ ...

  8. Egret 学习之 从HelloWorld项目开始 (二)

    1,创建新项目HelloWorld ,可以在界面上点击文件->新建,也可以在命令行使用create: 2,src 目录,存放我们的代码.我们编写的代码都放在src目录下面. bin-debug ...

  9. 利用requestjs优化响应式移动端js加载

    html: <script data-main="main" src="require.js"></script> main.js re ...

  10. Elasticsearch .net 客户端条件拼接查询

    private Func<SearchDescriptor<BaseTest>, ISearchRequest> ConstructWhere(TestCondition wh ...