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

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

之前一个同学问了这个问题后,看了园子里其它园友的封装,都很零碎,调用也不是很方便。所以自己就封装了一个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.         } 

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

  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

【编辑推荐】

  1. 这也是C#代码吗?代码阅读性进阶
  2. C#网络编程系列一:网络协议简介
  3. C#网络编程系列二:HTTP协议详解
  4. C#网络编程系列三:自定义Web服务器
  5. C#网络编程系列四:自定义Web浏览器
责任编辑:张伟 来源: 博客园
相关推荐

2011-08-15 14:07:53

Objective-C解压缩ZIP文件

2012-05-10 09:43:28

2011-12-30 11:14:41

Javazip

2023-01-30 09:04:56

Linux命令unzip

2009-08-17 08:42:00

C#文件存储管理

2009-08-17 13:26:16

压缩备份C#工程

2021-02-06 10:27:45

C#函数参数

2009-06-24 17:32:40

动态加载AppDoma

2010-06-01 13:32:15

Visual Stud

2010-01-15 18:35:25

C++的类

2009-08-24 14:30:49

C# WMI封装

2021-04-14 06:53:52

C# 修饰符 Public

2015-04-08 15:40:53

php在线解压解压zip文件

2024-04-01 13:05:13

C++接口类开发

2018-09-14 16:18:26

Linux压缩文件应用程序

2010-01-04 09:27:31

Linux压缩解压缩命令详解

2009-08-11 13:07:26

C#类库中添加Web

2009-08-03 18:12:31

C#抽象类

2009-08-04 17:08:12

C# Thread类

2018-02-28 13:20:40

Python解压zip
点赞
收藏

51CTO技术栈公众号