加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 利用SharpZipLib生成压缩包

发布时间:2020-12-16 01:12:56 所属栏目:百科 来源:网络整理
导读:本文通过一个简单的小例子简述SharpZipLib压缩文件的常规用法,仅供学习分享使用,如有不足之处,还请指正。 什么是SharpZipLib ? SharpZipLib是一个C#的类库,主要用来解压缩Zip,GZip,BZip2,Tar等格式,是以托管程序集的方式实现,可以方便的应用于其他的项

本文通过一个简单的小例子简述SharpZipLib压缩文件的常规用法,仅供学习分享使用,如有不足之处,还请指正。

什么是SharpZipLib ?

SharpZipLib是一个C#的类库,主要用来解压缩Zip,GZip,BZip2,Tar等格式,是以托管程序集的方式实现,可以方便的应用于其他的项目之中。

在工程中引用SharpZipLib

在项目中,点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,如下图所示:

SharpZipLib的关键类结构图

如下所示:

涉及知识点:

  • ZipOutputStream 压缩输出流,将文件一个接一个的写入压缩文档,此类不是线程安全的。
  • PutNextEntry 开始一个新的ZIP条目,ZipOutputStream中的方法。
  • ZipEntry 一个ZIP文件中的条目,可以理解为压缩包里面的一个文件夹/文件。
  • ZipInputStream 解压缩输出流,从压缩包中一个接一个的读出文档。
  • GetNextEntry 读出ZIP条目,ZipInputStream中的方法。

示例效果图:

关于解压缩小例子的示例效果图,如下:

核心代码

  1 using ICSharpCode.SharpZipLib.Checksum;
  2  ICSharpCode.SharpZipLib.Zip;
  3  System;
  4  System.Collections.Generic;
  5  System.IO;
  6  System.Linq;
  7  System.Text;
  8  System.Threading.Tasks;
  9 
 10 namespace DemoZip
 11 {
 12     class ZipHelper
 13     {
 14         private string rootPath = string.Empty;
 15 
 16         #region 压缩  
 17 
 18         /// <summary>   
 19         /// 递归压缩文件夹的内部方法   
 20         </summary>   
 21         <param name="folderToZip">要压缩的文件夹路径</param>   
 22         <param name="zipStream">压缩输出流 23         <param name="parentFolderName">此文件夹的上级文件夹 24         <returns></returns>   
 25         private  bool ZipDirectory(string folderToZip,ZipOutputStream zipStream, parentFolderName)
 26         {
 27             bool result = true;
 28             [] folders,files;
 29             ZipEntry ent = null 30             FileStream fs =  31             Crc32 crc = new Crc32();
 32 
 33             try
 34             {
 35                 string entName = folderToZip.Replace(this.rootPath,1)">string.Empty)+"/" 36                 //Path.Combine(parentFolderName,Path.GetFileName(folderToZip) + "/")
 37                 ent =  ZipEntry(entName);
 38                 zipStream.PutNextEntry(ent);
 39                 zipStream.Flush();
 40 
 41                 files = Directory.GetFiles(folderToZip);
 42                 foreach (string file in files)
 43                 {
 44                     fs = File.OpenRead(file);
 45 
 46                     byte[] buffer = new byte[fs.Length];
 47                     fs.Read(buffer,0,buffer.Length);
 48                     ent = new ZipEntry(entName + Path.GetFileName(file));
 49                     ent.DateTime = DateTime.Now;
 50                     ent.Size = fs.Length;
 51 
 52                     fs.Close();
 53 
 54                     crc.Reset();
 55                     crc.Update(buffer);
 56 
 57                     ent.Crc = crc.Value;
 58                     zipStream.PutNextEntry(ent);
 59                     zipStream.Write(buffer,1)"> 60                 }
 61 
 62             }
 63             catch
 64  65                 result = false 66  67             finally
 68  69                 if (fs != )
 70  71  72                     fs.Dispose();
 73  74                 if (ent !=  75  76                     ent =  77  78                 GC.Collect();
 79                 GC.Collect(1);
 80  81 
 82             folders = Directory.GetDirectories(folderToZip);
 83             string folder  folders)
 84                 if (!ZipDirectory(folder,zipStream,folderToZip))
 85                     return  86 
 87             return result;
 88         }
 89 
 90          91          压缩文件夹    
 92          93          94         <param name="zipedFile">压缩文件完整路径 95         <param name="password">密码 96         <returns>是否压缩成功</returns>   
 97         public  string zipedFile,1)"> password)
 98  99             100             Directory.Exists(folderToZip))
101                 102 
103             ZipOutputStream zipStream =  ZipOutputStream(File.Create(zipedFile));
104             zipStream.SetLevel(6105             string.IsNullOrEmpty(password)) zipStream.Password = password;
106 
107             result = ZipDirectory(folderToZip,1)">""108 
109             zipStream.Finish();
110             zipStream.Close();
111 
112             113 114 
115         116          压缩文件夹   
117         118         119         120         121          zipedFile)
122 123             bool result = ZipDirectory(folderToZip,zipedFile,1)">124             125 126 
127         128          压缩文件   
129         130         <param name="fileToZip">要压缩的文件全名131         压缩后的文件名132         133         压缩结果134         bool ZipFile(string fileToZip,1)">135 136             137             ZipOutputStream zipStream = 138             FileStream fs = 139             ZipEntry ent = 140 
141             File.Exists(fileToZip))
142                 143 
144             145 146                 fs = File.OpenRead(fileToZip);
147                 148                 fs.Read(buffer,1)">149                 fs.Close();
150 
151                 fs = File.Create(zipedFile);
152                 zipStream =  ZipOutputStream(fs);
153                 154                 ent =  ZipEntry(Path.GetFileName(fileToZip));
155 156                 zipStream.SetLevel(157 
158                 zipStream.Write(buffer,1)">159 
160 161             162 163                 result = 164 165             166 167                 if (zipStream != 168 169                     zipStream.Finish();
170                     zipStream.Close();
171 172                 173 174                     ent = 175 176                 177 178 179 180 181 182             GC.Collect();
183             GC.Collect(184 
185             186 187 
188         189         190         191         192         193         194         195 196             bool result = ZipFile(fileToZip,1)">197             198 199 
200         201          压缩文件或文件夹   
202         203         要压缩的路径204         205         206         207         bool Zip(208 209             210             if (Directory.Exists(fileToZip))
211 212                 this.rootPath = Path.GetDirectoryName(fileToZip);
213                 result = ZipDirectory(fileToZip,password);
214 215             else  (File.Exists(fileToZip))
216 217                 218                 result = ZipFile(fileToZip,1)">219 220             221 222 
223         224         225         226         227         228         229         230 231             bool result = Zip(fileToZip,1)">232             233 
234 235 
236         #endregion
237 
238         #region 解压  
239 
240         241          解压功能(解压压缩文件到指定目录)   
242         243         <param name="fileToUnZip">待解压的文件244         <param name="zipedFolder">指定解压目标目录245         246         解压结果247         public bool UnZip(string fileToUnZip,1)">string zipedFolder,1)">248 249             250             FileStream fs = 251             ZipInputStream zipStream = 252             ZipEntry ent = 253              fileName;
254 
255             File.Exists(fileToUnZip))
256                 257 
258             Directory.Exists(zipedFolder))
259                 Directory.CreateDirectory(zipedFolder);
260 
261             262 263                 zipStream =  ZipInputStream(File.OpenRead(fileToUnZip));
264                 265                 while ((ent = zipStream.GetNextEntry()) != 266 267                     .IsNullOrEmpty(ent.Name))
268                     {
269                         fileName = Path.Combine(zipedFolder,ent.Name);
270                         fileName = fileName.Replace('',1)">');change by Mr.HopeGi   
271 
272                         if (fileName.EndsWith())
273                         {
274                             Directory.CreateDirectory(fileName);
275                             continue276                         }
277 
278                         fs = File.Create(fileName);
279                         int size = 2048280                         byte[] data = [size];
281                         while (282 283                             size = zipStream.Read(data,data.Length);
284                             if (size > 285                                 fs.Write(data,1)">286                             else
287                                 break288 289                     }
290 291 292             293 294                 result = 295 296             297 298                 299 300 301 302 303                 304 305 306                     zipStream.Dispose();
307 308                 309 310                     ent = 311 312 313                 GC.Collect(314 315             316 317 
318         319         320         321         322         323         324          zipedFolder)
325 326             bool result = UnZip(fileToUnZip,zipedFolder,1)">327             328 329 
330         331     }
332 }
View Code

备注

关于生成压缩的方法还有很多,如通过命令行调用winrar的执行文件,SharpZipLib只是方法之一。

关于SharpZipLib的的API文档,可参看链接。

关于源码下载链接

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读