文章出處

  上班第二天下班,課外作業,實現一個ZIP壓縮的工具類.本來想用Package,但是寫完了才發現不能解壓其他工具壓縮的zip包,比較麻煩,因此本工具類依賴了第三方的庫(SharpZipLib  version 0.86.0 http://icsharpcode.github.io/SharpZipLib/),上午花了兩個小時整理了下代碼,mark下!

  

 1 /**
 2  * Class: ZIP壓縮工具類
 3  * Reference: SharpZipLib  version 0.86.0 (http://icsharpcode.github.io/SharpZipLib/)
 4  * Author: Zengyq
 5  * Version: 1.0
 6  * Create Date: 2014-05-13
 7  * Modified Date: 2014-05-14
 8  */
 9 
10 /****************構造函數*************************/
11 ZipUtils()
12 ZipUtils(int compressionLevel)
13 
14 /****************方法列表*************************/
15 /// <summary>
16 /// 功能: ZIP方式壓縮文件(壓縮目錄)
17 /// </summary>
18 /// <param name="dirPath">被壓縮的文件夾夾路徑</param>
19 /// <param name="zipFilePath">生成壓縮文件的路徑,缺省值:文件夾名+.zip</param>
20 /// <param name="msg">返回消息</param>
21 /// <returns>是否壓縮成功</returns>
22 public bool ZipFolder(string dirPath, string zipFilePath, out string msg)
23 
24 /// <summary>
25 /// 功能:壓縮文件指定文件
26 /// </summary>
27 /// <param name="filenames">要壓縮文件的絕對路徑</param>
28 /// <param name="zipFilePath">生成壓縮文件的路徑, 缺省值為隨機數字串.zip</param>
29 /// <param name="msg">返回消息</param>
30 /// <returns>是否壓縮成功</returns>
31 public bool ZipFiles(string[] filenames, string zipFilePath, out string msg)
32 
33 /// <summary>
34 /// 解壓ZIP格式的文件。
35 /// </summary>
36 /// <param name="zipFilePath">壓縮文件路徑</param>
37 /// <param name="unZipDir">解壓文件存放路徑,缺省值壓縮文件同一級目錄下,跟壓縮文件同名的文件夾</param>
38 /// <param name="msg">返回消息</param>
39 /// <returns>解壓是否成功</returns>
40 public bool UnZipFile(string zipFilePath, string unZipDir, out string msg)
41 
42 /// <summary>
43 /// 解壓指定ZIP文件到當前路徑
44 /// </summary>
45 /// <param name="zipFilePath">ZIP文件絕對路徑</param>
46 /// <param name="msg">返回消息</param>
47 /// <returns>解壓是否成功</returns>
48 public bool UnZipFileToCurrentPath(string zipFilePath, out string msg)
49 
50 /****************調用樣例*************************/
51 ZipUtils zu = new ZipUtils(); 
52 string msg = "";
53 zu.UnZipFileToCurrentPath(@"C:\Users\zengyiqun\Desktop\package1024.zip", out msg);

 

  類實現,沒有采用靜態類的形式,看了某博客用了out作為msg不懂會不會用到,先這樣了,以下是實現類:

  1 /**
  2  * Class: ZIP壓縮工具類
  3  * Reference: SharpZipLib  version 0.86.0 (http://icsharpcode.github.io/SharpZipLib/)
  4  * Author: Zengyq
  5  * Version: 1.0
  6  * Create Date: 2014-05-13
  7  * Modified Date: 2014-05-14
  8  */
  9 
 10 using ICSharpCode.SharpZipLib.Zip;
 11 using System;
 12 using System.IO;
 13 
 14 public class ZipUtils
 15 {
 16     //緩存大小
 17     private int BufferSize;
 18 
 19     //壓縮率 0(無壓縮)-9(壓縮率最高)
 20     private int CompressionLevel;
 21 
 22     //壓縮是用于存放壓縮路徑
 23     private string CompressPath;
 24 
 25 
 26     #region 構造函數
 27     /// <summary>
 28     /// 無參構造
 29     /// </summary>
 30     public ZipUtils()
 31     {
 32         this.BufferSize = 4096;
 33         this.CompressionLevel = 9;
 34     }
 35 
 36     /// <summary>
 37     /// 帶參數構造
 38     /// </summary>
 39     /// <param name="compressionLevel">0(無壓縮)-9(壓縮率最高)</param>
 40     public ZipUtils(int compressionLevel)
 41     {
 42         this.BufferSize = 4096;
 43         this.CompressionLevel = compressionLevel;
 44     }
 45     #endregion
 46 
 47     #region 壓縮方法
 48     /// <summary>
 49     /// 功能: ZIP方式壓縮文件(壓縮目錄)
 50     /// </summary>
 51     /// <param name="dirPath">被壓縮的文件夾夾路徑</param>
 52     /// <param name="zipFilePath">生成壓縮文件的路徑,缺省值:文件夾名+.zip</param>
 53     /// <param name="msg">返回消息</param>
 54     /// <returns>是否壓縮成功</returns>
 55     public bool ZipFolder(string dirPath, string zipFilePath, out string msg)
 56     {
 57         this.CompressPath = dirPath;
 58         //判斷目錄是否為空
 59         if (dirPath == string.Empty)
 60         {
 61             msg = "The Folder is empty";
 62             return false;
 63         }
 64 
 65         //判斷目錄是否存在
 66         if (!Directory.Exists(dirPath))
 67         {
 68             msg = "The Folder is not exists";
 69             return false;
 70         }
 71 
 72         //壓縮文件名為空時使用 文件夾名.zip
 73         if (zipFilePath == string.Empty || zipFilePath == null)
 74         {
 75             if (dirPath.EndsWith("//"))
 76             {
 77                 dirPath = dirPath.Substring(0, dirPath.Length - 1);
 78             }
 79             zipFilePath = dirPath + ".zip";
 80         }
 81 
 82         try
 83         {
 84             using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
 85             {
 86                 s.SetLevel(this.CompressionLevel);
 87                 ZipDirectory(dirPath, s);//壓縮目錄,包含子目錄
 88                 s.Finish();
 89                 s.Close();
 90             }
 91         }
 92         catch (Exception ex)
 93         {
 94             msg = ex.Message;
 95             return false;
 96         }
 97 
 98         msg = "Success";
 99         return true;
100     }//end function
101 
102     /// <summary>
103     /// 壓縮文件指定文件
104     /// </summary>
105     /// <param name="filenames">要壓縮文件的絕對路徑</param>
106     /// <param name="zipFilePath">生成壓縮文件的路徑, 缺省值為當前時間.zip</param>
107     /// <param name="msg">返回消息</param>
108     /// <returns>是否壓縮成功</returns>
109     public bool ZipFiles(string[] filenames, string zipFilePath, out string msg)
110     {
111         msg = "";
112         if (filenames.Length == 0)
113         {
114             msg = "No File Selected";
115             return false;
116         }
117 
118         //壓縮文件名為空時使用 文件夾名.zip
119         if (zipFilePath == string.Empty || zipFilePath == null)
120         {
121             zipFilePath = System.Environment.CurrentDirectory + "\\" + DateTime.Now.ToFileTimeUtc().ToString() + ".zip";
122         }
123 
124         try
125         {
126             using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
127             {
128                 s.SetLevel(this.CompressionLevel);
129                 ZipFilesAndDirectory(filenames,s);
130                 s.Finish();
131                 s.Close();
132             }//end using
133         }
134         catch (Exception ex)
135         {
136             msg = ex.Message;
137             return false;
138         }
139 
140         msg = "Success";
141         return true;
142     }
143 
144     #endregion
145 
146     #region 解壓方法
147     /// <summary>
148     /// 解壓ZIP格式的文件。
149     /// </summary>
150     /// <param name="zipFilePath">壓縮文件路徑</param>
151     /// <param name="unZipDir">解壓文件存放路徑,缺省值壓縮文件同一級目錄下,跟壓縮文件同名的文件夾</param>
152     /// <param name="msg">返回消息</param>
153     /// <returns>解壓是否成功</returns>
154     public bool UnZipFile(string zipFilePath, string unZipDir, out string msg)
155     {
156         msg = "";
157 
158         //判讀路徑是否為空
159         if (zipFilePath == string.Empty || zipFilePath == null)
160         {
161             msg = "ZipFile No Selected";
162             return false;
163         }
164 
165         //判讀文件是否存在
166         if (!File.Exists(zipFilePath))
167         {
168             msg = "ZipFile No Exists";
169             return false;
170         }
171         //解壓文件夾為空時默認與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾
172         if (unZipDir == string.Empty || unZipDir == null)
173         {
174             unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
175         }
176 
177         if (!unZipDir.EndsWith("//")) 
178         {
179             unZipDir += "//";
180         }
181             
182         if (!Directory.Exists(unZipDir))
183         {
184             Directory.CreateDirectory(unZipDir);
185         }
186 
187         try
188         {
189             using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
190             {
191 
192                 ZipEntry theEntry;
193                 while ((theEntry = s.GetNextEntry()) != null)
194                 {
195                     string directoryName = Path.GetDirectoryName(theEntry.Name);
196                     string fileName = Path.GetFileName(theEntry.Name);
197                     if (directoryName.Length > 0)
198                     {
199                         Directory.CreateDirectory(unZipDir + directoryName);
200                     }
201                     if (!directoryName.EndsWith("//"))
202                     { 
203                         directoryName += "//";
204                     }
205                     if (fileName != String.Empty && fileName != null)
206                     {
207                         using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
208                         {
209                             CopyStream(s, streamWriter);
210                         }
211                     }
212                 }//end while
213             }//end using
214         }
215         catch (Exception ex)
216         {
217             msg = ex.Message;
218             return false;
219         }
220 
221         msg = "UnZip Success ! ";
222         return true;
223     }//end function
224 
225     /// <summary>
226     /// 解壓指定ZIP文件到當前路徑
227     /// </summary>
228     /// <param name="zipFilePath">ZIP文件絕對路徑</param>
229     /// <param name="msg">返回消息</param>
230     /// <returns>解壓是否成功</returns>
231     public bool UnZipFileToCurrentPath(string zipFilePath, out string msg)
232     {
233         msg = "";
234 
235         //判讀路徑是否為空
236         if (zipFilePath == string.Empty || zipFilePath == null)
237         {
238             msg = "ZipFile No Selected";
239             return false;
240         }
241 
242         //判讀文件是否存在
243         if (!File.Exists(zipFilePath))
244         {
245             msg = "ZipFile No Exists";
246             return false;
247         }
248         //解壓到當前目錄
249         string unZipDir = zipFilePath.Substring(0, zipFilePath.LastIndexOf(@"\"));
250 
251         if (!unZipDir.EndsWith("//"))
252         {
253             unZipDir += "//";
254         }
255 
256         if (!Directory.Exists(unZipDir))
257         {
258             Directory.CreateDirectory(unZipDir);
259         }
260 
261         try
262         {
263             using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
264             {
265                 ZipEntry theEntry;
266                 while ((theEntry = s.GetNextEntry()) != null)
267                 {
268                     string directoryName = Path.GetDirectoryName(theEntry.Name);
269                     string fileName = Path.GetFileName(theEntry.Name);
270                     if (directoryName.Length > 0)
271                     {
272                         Directory.CreateDirectory(unZipDir + directoryName);
273                     }
274                     if (!directoryName.EndsWith("//"))
275                     {
276                         directoryName += "//";
277                     }
278                     if (fileName != String.Empty && fileName != null)
279                     {
280                         using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
281                         {
282                             CopyStream(s, streamWriter);
283                         }
284                     }
285                 }//end while
286             }//end using
287         }
288         catch (Exception ex)
289         {
290             msg = ex.Message;
291             return false;
292         }
293 
294         msg = "UnZip Success ! ";
295         return true;
296     }//end function
297 
298     #endregion
299 
300     #region 內部方法
301 
302     /// <summary>
303     /// 內部類:遞歸壓縮目錄
304     /// </summary>
305     /// <param name="dirPath"></param>
306     /// <param name="s"></param>
307     private void ZipDirectory(string dirPath, ZipOutputStream s)
308     {
309         string[] filenames = Directory.GetFiles(dirPath);
310         byte[] buffer = new byte[this.BufferSize];
311         foreach (string file in filenames)
312         {
313             //處理相對路徑問題
314             ZipEntry entry = new ZipEntry(file.Replace(this.CompressPath + "\\", String.Empty));
315             //設置壓縮時間
316             entry.DateTime = DateTime.Now;
317 
318             s.PutNextEntry(entry);
319             using (FileStream fs = File.OpenRead(file))
320             {
321                 CopyStream(fs, s);
322             }
323         }
324 
325         //遞歸處理子目錄
326         DirectoryInfo di = new DirectoryInfo(dirPath);
327         foreach (DirectoryInfo subDi in di.GetDirectories())
328         {
329             ZipDirectory(subDi.FullName, s);
330         }
331 
332     }//end function
333 
334     /// <summary>
335     /// 
336     /// </summary>
337     /// <param name="filenames"></param>
338     /// <param name="s"></param>
339     private void ZipFilesAndDirectory(string[] filenames, ZipOutputStream s)
340     {
341         byte[] buffer = new byte[this.BufferSize];
342         foreach (string file in filenames)
343         {
344             //當時文件的時候
345             if (System.IO.File.Exists(file))
346             {
347                 ZipEntry entry = new ZipEntry(Path.GetFileName(file));
348                 entry.DateTime = DateTime.Now;
349                 s.PutNextEntry(entry);
350                 using (FileStream fs = File.OpenRead(file))
351                 {
352                     CopyStream(fs, s);
353                 }
354             }
355 
356             //當時目錄的時候
357             if(System.IO.Directory.Exists(file))
358             {
359                 this.CompressPath = file.Replace("\\" + Path.GetFileName(file),String.Empty);
360                 ZipDirectory(file, s);
361             }
362 
363         }
364 
365        
366     }
367 
368 
369 
370     /// <summary>
371     /// 按塊復制
372     /// </summary>
373     /// <param name="source">源Stream</param>
374     /// <param name="target">目標Stream</param>
375     private void CopyStream(Stream source, Stream target)
376     {
377         byte[] buf = new byte[this.BufferSize];
378         int byteRead = 0;
379         while ((byteRead = source.Read(buf, 0, this.BufferSize)) > 0)
380         {
381             target.Write(buf, 0, byteRead);
382         }
383 
384     }
385     #endregion
386 
387 }//end class

  代碼風格還請大家輕噴,在.net程序員面前是個非主流java程序員,在java程序員面前是個非主流.net程序員。將以上工具類封裝成了dll可以直接引用。

  附件:ZipUtils.rar

1 ZipUtils.rar
2     -src
3         -ZipUtils.cs
4     -dll
5         -SharpZipLib.dll
6         -ZipUtilsLib.dll

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()