微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 利用SharpZipLib生成压缩包

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

什么是SharpZipLib ?

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

在工程中引用SharpZipLib

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

SharpZipLib的关键类结构图

如下所示:

涉及知识点:

示例效果图:

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

核心代码

  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文档,可参看链接

关于源码下载链接

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐