《ICSharpCode快速解壓縮幫助類》——即粘即用

在項目中每每使用解壓縮公共類,解壓縮以後的文件佔用空間小,也可進行加密,每每能夠用於客戶端上傳附件,打包輸出主程序等,其中的好處就很少說了,最近着手的項目中屢次使用到了解壓縮方法,現較流行的就是ICSharpCode,穩定,高效,是一個不錯的解壓縮封裝類。經過InterNET和我的的整理,現將該類分享出來,做爲資源分享給你們,這樣就能夠不用在埋頭苦腦的在InterNET上苦苦尋找了,廢話很少說,上代碼:併發

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Security.Cryptography;
  5 using ICSharpCode.SharpZipLib.Core;
  6 using ICSharpCode.SharpZipLib.Zip;
  7 
  8 namespace Helper
  9 {
 10     public class Utily
 11     {
 12         /// <summary>
 13         /// 快速壓縮
 14         /// </summary>
 15         /// <param name="filesPath">須要壓縮的文件夾路徑</param>
 16         /// <param name="zipFilePath">輸出路徑</param>
 17         /// <param name="pwd">密碼,可不寫</param>
 18         /// <param name="fileFilter">過濾條件</param>
 19         /// <param name="CreateEmptyDirectories">是否壓縮空文件夾</param>
 20         /// <param name="progressFun">處理進程</param>
 21         /// <param name="seconds">觸發的秒數</param>
 22         /// <param name="completeFun">完成事件</param>
 23         public static void CreateZipFile(string filesPath, string zipFilePath, string pwd, string fileFilter, bool CreateEmptyDirectories, ProgressHandler progressFun, double seconds, CompletedFileHandler completeFun)
 24         {
 25             FastZipEvents events = new FastZipEvents();
 26             if (progressFun != null)
 27             {
 28                 events.Progress = progressFun;
 29                 events.ProgressInterval = TimeSpan.FromSeconds(seconds);
 30             }
 31             if (completeFun != null)
 32             {
 33                 events.CompletedFile = completeFun;
 34             }
 35             FastZip zip = new FastZip(events);
 36             zip.CreateEmptyDirectories = CreateEmptyDirectories;
 37             if (!string.IsNullOrEmpty(pwd))
 38                 zip.Password = pwd;
 39             zip.UseZip64 = UseZip64.On;
 40             zip.RestoreAttributesOnExtract = true;
 41             zip.RestoreDateTimeOnExtract = true;
 42             zip.CreateZip(zipFilePath, filesPath, true, fileFilter);
 43         }
 44 
 45         /// <summary>
 46         /// 快速解壓
 47         /// </summary>
 48         /// <param name="zipFilePath">壓縮文件路徑</param>
 49         /// <param name="extractPath">解壓路徑</param>
 50         /// <param name="pwd">壓縮密碼</param>
 51         /// <param name="progressFun">進程</param>
 52         /// <param name="seconds">觸發時間</param>
 53         public static void ExtractZipFile(string zipFilePath, string extractPath, string pwd, ProgressHandler progressFun, double seconds)
 54         {
 55             FastZipEvents events = new FastZipEvents();
 56             if (progressFun != null)
 57             {
 58                 events.Progress = progressFun;
 59                 events.ProgressInterval = TimeSpan.FromSeconds(seconds);
 60             }
 61             FastZip zip = new FastZip(events);
 62 
 63             zip.CreateEmptyDirectories = true;
 64             if (!string.IsNullOrEmpty(pwd))
 65                 zip.Password = pwd;
 66             zip.UseZip64 = UseZip64.On;
 67             zip.RestoreAttributesOnExtract = true;
 68             zip.RestoreDateTimeOnExtract = true;
 69             zip.ExtractZip(zipFilePath, extractPath, FastZip.Overwrite.Always, null, "", "", true);
 70         }
 71 
 72         /// <summary>
 73         /// 快速解壓
 74         /// </summary>
 75         /// <param name="zipFilePath">壓縮文件路徑</param>
 76         /// <param name="extractPath">解壓路徑</param>
 77         /// <param name="pwd">密碼</param>
 78         /// <param name="progressFun">進程</param>
 79         /// <param name="seconds">觸發時間</param>
 80         /// <param name="completeFun">壓縮過程當中執行的函數</param>
 81         public static void ExtractZipFile(string zipFilePath, string extractPath, string pwd, ProgressHandler progressFun, double seconds, CompletedFileHandler completeFun)
 82         {
 83             FastZipEvents events = new FastZipEvents();
 84             if (progressFun != null)
 85             {
 86                 events.Progress = progressFun;
 87                 events.ProgressInterval = TimeSpan.FromSeconds(seconds);
 88             }
 89             if (completeFun != null)
 90             {
 91                 events.CompletedFile = completeFun;
 92             }
 93             FastZip zip = new FastZip(events);
 94 
 95             zip.CreateEmptyDirectories = true;
 96             if (!string.IsNullOrEmpty(pwd))
 97                 zip.Password = pwd;
 98             zip.UseZip64 = UseZip64.On;
 99             zip.RestoreAttributesOnExtract = true;
100             zip.RestoreDateTimeOnExtract = true;
101             zip.ExtractZip(zipFilePath, extractPath, FastZip.Overwrite.Always, null, "", "", true);
102         }
103 
104         /// <summary>
105         /// 得到壓縮包內原文件總大小
106         /// </summary>
107         /// <param name="fileName"></param>
108         /// <param name="fileFilter"></param>
109         /// <param name="directoryFilter"></param>
110         /// <returns></returns>
111         public static long GetZipFileSize(string fileName, string fileFilter, string directoryFilter)
112         {
113             long b = 0;
114             using (ZipFile zipFile = new ZipFile(fileName))
115             {
116                 PathFilter localFileFilter = new PathFilter(fileFilter);
117                 PathFilter localDirFilter = new PathFilter(directoryFilter);
118 
119                 if (zipFile.Count == 0)
120                 {
121                     return 0;
122                 }
123                 for (int i = 0; i < zipFile.Count; ++i)
124                 {
125                     ZipEntry e = zipFile[i];
126                     if (e.IsFile)
127                     {
128                         string path = Path.GetDirectoryName(e.Name);
129                         if (localDirFilter.IsMatch(path))
130                         {
131                             if (localFileFilter.IsMatch(Path.GetFileName(e.Name)))
132                             {
133                                 b += e.Size;
134                             }
135                         }
136                     }
137                 }
138             }
139             return b;
140         }
141 
142         /// <summary>
143         /// 得到MD5校驗碼
144         /// </summary>
145         /// <param name="filepath"></param>
146         /// <returns></returns>
147         public static string GetMD5(string filepath)
148         {
149             string returnStr = "";
150             FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
151             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
152             byte[] md5byte = md5.ComputeHash(fs);
153             int i, j;
154             foreach (byte b in md5byte)
155             {
156                 i = Convert.ToInt32(b);
157                 j = i >> 4;
158                 returnStr += Convert.ToString(j, 16);
159                 j = ((i << 4) & 0x00ff) >> 4;
160                 returnStr += Convert.ToString(j, 16);
161             }
162             fs.Dispose();
163             return returnStr;
164         }
165 
166         /// <summary>
167         /// 解壓縮特定文件名的文件
168         /// </summary>
169         /// <param name="path">文件路徑</param>
170         /// <param name="addres">解壓縮路徑</param>
171         /// <param name="zipFileName">文件名稱</param>
172         /// <param name="pwd">解壓縮包密碼</param>
173         public static void ZipToFile(string path, string addres, string zipFileName, string pwd)
174         {
175             ZipInputStream ZipStream = new ZipInputStream(System.IO.File.OpenRead(path));
176             if (!string.IsNullOrEmpty(pwd))
177                 ZipStream.Password = pwd;
178             ZipEntry fileEntry;
179             while ((fileEntry = ZipStream.GetNextEntry()) != null)
180             {
181                 string filename = Path.GetFileName(fileEntry.Name);
182                 if (filename == zipFileName)
183                 {
184                     filename = addres + "\\" + filename;
185                     FileStream streamWriter = System.IO.File.Create(filename);
186                     int size = (int)fileEntry.Size;
187                     byte[] buffer = new byte[size];
188 
189                     size = ZipStream.Read(buffer, 0, size);
190                     streamWriter.Write(buffer, 0, size);
191                     streamWriter.Close();
192                 }
193             }
194             ZipStream.Close();
195         }
196     }
197 }

該類可以知足基本經常使用解壓縮的方法了,不過現比較流行的應該是7z壓縮,這個也在研究中,以上代碼如有不正確的地方,也請各位大牛指正。至於ICSharpCode的DLL文件,網上可以下載的地方也不少,我也就不在給出下載地址了。ide

舒適提醒:在引用ICSharpCode時記的在調用此類方法的類庫或應用程序上也要引用ICSharpCode,不然會產生錯誤。函數

今天就分享這麼多吧。加密

 

版權聲明:

  本文由Tom原創併發佈於博客園,歡迎轉載,未經本人贊成必須保留此段聲明(不然保留追究責任的權利),且在文章頁面明顯位置給出原文連接,若有問題,能夠經過419187544@qq.com 聯繫我,很是感謝。spa

相關文章
相關標籤/搜索