C#中关于zip压缩解压帮助类的封装

  1. c#下压缩解压,主要是用第三方类库进行封装的。ICSharpCode.SharpZipLib.dll类库,链接地址为你官方下载链接。压缩主要是用流的方式进行压缩的。

      压缩文件及文件夹。文件压缩很简单,把待压缩的文件用流的方式读到内存中,然后放到压缩流中。就可以了。文件夹就稍微麻烦下了。因为要把待压缩的文件夹解压后保留文件夹文件的层次结构。所以我的实现方式就是 递归遍历文件夹中的文件。计算其相对位置放到压缩流中。

  2. /// <summary>  
  3.         /// 压缩文件或者文件夹  
  4.         /// </summary>  
  5.         /// <param name="_depositPath">压缩后文件的存放路径   如C:\\windows\abc.zip</param>  
  6.         /// <returns></returns>  
  7.         public bool CompressionZip(string _depositPath)  
  8.         {  
  9.             bool result = true;  
  10.             FileStream fs = null;  
  11.             try 
  12.             {  
  13.                 ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));  
  14.                 ComStream.SetLevel(9);      //压缩等级  
  15.                 foreach (string path in AbsolutePaths)  
  16.                 {  
  17.                     //如果是目录  
  18.                     if (Directory.Exists(path))  
  19.                     {  
  20.                         ZipFloder(path, ComStream, path);  
  21.                     }  
  22.                     else if (File.Exists(path))//如果是文件  
  23.                     {  
  24.                          fs = File.OpenRead(path);  
  25.                         byte[] bts = new byte[fs.Length];  
  26.                         fs.Read(bts, 0, bts.Length);  
  27.                         ZipEntry ze = new ZipEntry(new FileInfo(path).Name);  
  28.                         ComStream.PutNextEntry(ze);             //为压缩文件流提供一个容器  
  29.                         ComStream.Write(bts, 0, bts.Length);  //写入字节  
  30.                     }  
  31.                 }  
  32.                 ComStream.Finish(); // 结束压缩  
  33.                 ComStream.Close();  
  34.             }  
  35.             catch (Exception ex)  
  36.             {  
  37.                 if (fs != null)  
  38.                 {  
  39.                     fs.Close();  
  40.                 }  
  41.                 errorMsg = ex.Message;  
  42.                 result = false;  
  43.             }  
  44.             return result;  
  45.         }  
  46.         //压缩文件夹  
  47.         private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)  
  48.         {  
  49.             foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())  
  50.             {  
  51.                 if (Directory.Exists(item.FullName))  
  52.                 {  
  53.                     ZipFloder(_OfloderPath, zos, item.FullName);  
  54.                 }  
  55.                 else if (File.Exists(item.FullName))//如果是文件  
  56.                 {  
  57.                     DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);  
  58.                     string fullName2 = new FileInfo(item.FullName).FullName;  
  59.                     string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录  
  60.                     FileStream fs = File.OpenRead(fullName2);  
  61.                     byte[] bts = new byte[fs.Length];  
  62.                     fs.Read(bts, 0, bts.Length);  
  63.                     ZipEntry ze = new ZipEntry(path);  
  64.                     zos.PutNextEntry(ze);             //为压缩文件流提供一个容器  
  65.                     zos.Write(bts, 0, bts.Length);  //写入字节  
  66.                 }  
  67.             }  
  68.         }

引用http://www.51testing.com/html/82/n-831382.html

原文地址:https://www.cnblogs.com/binbinxiang/p/3032620.html