ZipHelper

本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll。另外说明一下的是,这个类压缩格式是ZIP的,所以文件的后缀写成 .zip

还有,如果用这个类来解压rar格式的压缩文件时会报错,就网上说的那个"Wrong Local header signature: 0x21726152"异常。只要解压ZIP压缩格式的压缩文件就不会报错了。 下面就是Helper类的代码

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

 

 

posted @ 2013-03-06 18:05  猴健居士  阅读(1649)  评论(0编辑  收藏  举报