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

发表于:2012-12-27 09:52

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Bonker    来源:51Testing软件测试网采编

  之前一个同学问了这个问题后,看了园子里其它园友的封装,都很零碎,调用也不是很方便。所以自己就封装了一个zip解压的类。后来想整理下怕自己忘了。就把压缩的类也一并封装了。

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

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

  代码如下

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

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号