分享

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

 集微笔记 2013-08-13

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.         }

关于解压  解压就简单多了。有文件解压文件,有文件夹 遍历,解压其中的文件。解压的文件中已经包含了其与文件夹的层次关系。

  1. /// <summary>
  2.         /// 解压
  3.         /// </summary>
  4.         /// <param name=”_depositPath”>压缩文件路径</param>
  5.         /// <param name=”_floderPath”>解压的路径</param>
  6.         /// <returns></returns>
  7.         public bool DeCompressionZip(string _depositPath, string _floderPath)
  8.         {
  9.             bool result = true;
  10.             FileStream fs=null;
  11.             try
  12.             {
  13.                 ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));
  14.                 ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件
  15.                 Directory.CreateDirectory(_floderPath);//创建解压文件夹
  16.                 while (ze != null)//如果解压完ze则是null
  17.                 {
  18.                     if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名
  19.                     {
  20.                         string[] strs=ze.Name.Split(‘\\’);//如果文件名中包含’\\‘则表明有文件夹
  21.                         if (strs.Length > 1)
  22.                         {
  23.                             //两层循环用于一层一层创建文件夹
  24.                             for (int i = 0; i < strs.Length-1; i )
  25.                             {
  26.                                 string floderPath=_floderPath;
  27.                                 for (int j = 0; j < i; j )
  28.                                 {
  29.                                     floderPath = floderPath   ”\\”   strs[j];
  30.                                 }
  31.                                 floderPath=floderPath ”\\” strs[i];
  32.                                 Directory.CreateDirectory(floderPath);
  33.                             }
  34.                         }
  35.                          fs = new FileStream(_floderPath ”\\” ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//创建文件
  36.                         //循环读取文件到文件流中
  37.                         while (true)
  38.                         {
  39.                             byte[] bts = new byte[1024];
  40.                            int i= InpStream.Read(bts, 0, bts.Length);
  41.                            if (i > 0)
  42.                            {
  43.                                fs.Write(bts, 0, i);
  44.                            }
  45.                            else
  46.                            {
  47.                                fs.Flush();
  48.                                fs.Close();
  49.                                break;
  50.                            }
  51.                         }
  52.                     }
  53.                     ze = InpStream.GetNextEntry();
  54.                 }
  55.             }
  56.             catch (Exception ex)
  57.             {
  58.                 if (fs != null)
  59.                 {
  60.                     fs.Close();
  61.                 }
  62.                 errorMsg = ex.Message;
  63.                 result = false;
  64.             }
  65.             return result;
  66.         }

最后做个总结。C#作为高级语言,其强大的类库和第三方提供的类库。可以做很多事情。但也有弊端,用第三方类库性能不是很高。我压缩几百M的东西。cpu瞬间跑到50%多。比360压缩和zip压缩性能差远了。所以此类也就适用压缩比较小的东西。

完整例子下载地址

原文链接:http://www.cnblogs.com/Bonker/archive/2012/12/25/2831970.html

Tags: ,

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多