ZipFile类用于选择文件或文件夹进行压缩生成压缩包。

常用属性:

属性 说明
Count 文件数目(注意是在ComitUpdat之后才有)
Password 压缩包密码
Size 压缩包占用空间大小
Name 压缩包名称,默认输出是文件路径
ZipEntry 压缩包里的文件,通过索引[]访问

其常用方法如下:

方法 说明
Add 添加要进行压缩的文件
AddDirectory 添加文件夹(不会压缩文件夹里的文件)
Delete 删除文件或文件夹
BeginUpdate 开始修改压缩包
CommitUpdate 提交修改
SetComment 添加注释

示例1(创建压缩文件):

  using (ZipFile zip = ZipFile.Create(@"D:\test.zip"))
          zip.BeginUpdate();
      zip.SetComment("这是我的压缩包");
          zip.Add(@"D:\1.txt");       //添加一个文件
          zip.AddDirectory(@"D:\2");  //添加一个文件夹(这个方法不会压缩文件夹里的文件)
          zip.Add(@"D:\2\2.txt");     //添加文件夹里的文件
          zip.CommitUpdate();

  这样生成的压缩包是包含子文件夹,子文件夹也是包含子文件的。

  其中,注释如下:

  示例2:修改压缩包

  using (ZipFile zip = new ZipFile(@"D:\test.zip"))
          zip.BeginUpdate();
          zip.Add(@"D:\2.txt");
          zip.CommitUpdate();

  留意这个示例和上面的有什么不同,上面的是Create方法创建的ZipFile对象,而这里是直接读。因此,如果压缩包里面有文件,则不会改动原来的压缩文件,而是往会里面添加一个。这样就相当于压缩包的修改,而上面是压缩包的创建。

  示例3:读取压缩包里的文件:

  using (ZipFile zip = new ZipFile(@"D:\test.zip"))
          foreach (ZipEntry z in zip)
              Console.WriteLine(z);
          ZipEntry z1 = zip[0];
          Console.WriteLine(z1.Name);

二、FastZip

  这个类就两个方法:

方法说明
CreateZip压缩目录
ExtractZip解压缩目录

  1、FastZip用于快速压缩目录,示例如下:

//快速压缩目录,包括目录下的所有文件
(new FastZip()).CreateZip(@"D:\test.zip", @"D:\test\", true, "");

  这个是递归压缩的。但是局限性就是只能压缩文件夹。

  否则报如下错误:

  2、快速解压缩目录

//快速解压
(new FastZip()).ExtractZip(@"D:\test.zip", @"D:\解压目录\", "");

三、ZipOutputStream与ZipEntry

  • ZipOutputStream:相当于一个压缩包;
  • ZipEntry:相当于压缩包里的一个文件;

  以上两个类是SharpZipLib的主类,最耐玩的就是这两个类。

  ZipOutputStream常用属性:

属性说明
IsFinishedZipOutputStream是否已结束

  ZipOutputStream常用方法:

方法说明
CloseEntry关闭入口,关闭之后不允许再对ZipOutputStream进行操作
Finish结束写入
GetLevel读取压缩等级
PutNextEntry往ZipOutputStream里写入一个ZipEntry
SetComment压缩包的注释
SetLevel设置压缩等级,等级越高文件越小
Write写入文件内容

  使用ZipOutputStream创建一个压缩包并往里面写入一个文件的示例:

        static void Main(string[] args)
            using (ZipOutputStream s = new ZipOutputStream(File.Create(@"D:\123.zip")))
                s.SetLevel(6);  //设置压缩等级,等级越高压缩效果越明显,但占用CPU也会更多using (FileStream fs = File.OpenRead(@"D:\1.txt"))
                    byte[] buffer = new byte[4 * 1024];  //缓冲区,每次操作大小
                    ZipEntry entry = new ZipEntry(Path.GetFileName(@"改名.txt"));     //创建压缩包内的文件
                    entry.DateTime = DateTime.Now;  //文件创建时间
                    s.PutNextEntry(entry);          //将文件写入压缩包
                    int sourceBytes;
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);    //读取文件内容(1次读4M,写4M)
                        s.Write(buffer, 0, sourceBytes);                    //将文件内容写入压缩相应的文件
                    } while (sourceBytes > 0);
                s.CloseEntry();
            Console.ReadKey();

  以上示例仅仅能够压缩文件,要压缩文件夹就要使用递归的方式,循环子目录并压缩子目录里的文件。

  示例2:文件夹压缩,保持原文件夹架构:

    class Program
        static void Main(string[] args)
            string Source = @"D:\test";
            string TartgetFile = @"D:\test.zip";
            Directory.CreateDirectory(Path.GetDirectoryName(TartgetFile));
            using (ZipOutputStream s = new ZipOutputStream(File.Create(TartgetFile)))
                s.SetLevel(6);
                Compress(Source, s);
                s.Finish();
                s.Close();
            Console.ReadKey();
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="source">源目录</param>
        /// <param name="s">ZipOutputStream对象</param>
        public static void Compress(string source, ZipOutputStream s)
            string[] filenames = Directory.GetFileSystemEntries(source);
            foreach (string file in filenames)
                if (Directory.Exists(file))
                    Compress(file, s);  //递归压缩子文件夹
                    using (FileStream fs = File.OpenRead(file))
                        byte[] buffer = new byte[4 * 1024];
                        ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file),""));     //此处去掉盘符,如D:\123\1.txt 去掉D:
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        int sourceBytes;
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);

   附上解压缩方法:

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="targetPath">目标路经</param>
        public bool Decompress(string sourceFile, string targetPath)
            if (!File.Exists(sourceFile))
                throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));
            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath); 
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourceFile)))
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                    string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));
                    string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
                    // 创建目录
                    if (directorName.Length > 0)
                        Directory.CreateDirectory(directorName);
                    if (fileName != string.Empty)
                        using (FileStream streamWriter = File.Create(fileName))
                            int size = 4096;
                            byte[] data = new byte[ 4 * 1024];
                            while (true)
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                    streamWriter.Write(data, 0, size);
                                else break;
            return true;

   ZipEntry就没什么好说的了,都是一些属性,指示一下,实际用到的很少。

  框架地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

       使用参考:https://www.cnblogs.com/conexpress/p/SharpZipClass.html

转自:https://www.cnblogs.com/kissdodog/p/3525295.html 一、ZipFile  ZipFile类用于选择文件或文件夹进行压缩生成压缩包。  常用属性:属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Size 压缩包占用空间大小 Name...
在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比如,压缩当前程序目录下的某个文件夹(D:\cx\code\program\bin\debug\files),则生成压缩包后,打开需要访问到cx\code\program\bin\debug\files才可以看到内容。通过实践,变相实现了自定义压缩包目录名称和目录深度的功能。代码及说明如下。
 OutputStream out = new FileOutputStream(zipFilePath);         BufferedOutputStream bos = new BufferedOutputStream(out);         ZipOutputStream zos = new ZipOutputStream(bos);         // 解决中文文件名乱码 之前在 压缩与解压1---字符串的压缩与解压(http://www.cnblogs.com/java-pan/archive/2012/05/12/gzip1.html) 中介绍过对字符串的压缩和解压,这个解压和解决用在系统之间大数据传输的时候还是可以大大的减少网络流量的。本节主要本别介绍对单个文件、多个文件压缩。解压这里就不讲了,解压的话没什么实际意义,因为文件压缩后为z
lwei2: 楼主,请问我在xml的下拉框中添加了: <ListLabelElement name = "LabelList1" textcolor ="#003DA6" height="20" selected="true" /> 但是那个textcolor并没有生效,不知道您有什么办法可以优化吗? bsdiff差分算法 neworldor: 请问大佬,完全匹配的区域在图中是指哪一部分? EF开发三种模式 橙-极纪元: 我喜欢你这句话“不想学这个,不男不女的。” 如何打开visual studio的诊断工具(diagnostic tool) 想买CT5的小曹: 诊断工具是空白的如何解决呢 glog基本使用 PanC321: 能抽空排个版吗?偶像表情包