在項目中往往使用解壓縮公共類,解壓縮之後的文件占用空間小,也可進行加密,往往可以用於客戶端上傳附件,打包輸出主程序等,其中的好處就不多說了,最近著手的項目中多次使用到了解壓縮方法,現較流行的就是ICSharpCode,穩定,高效,是一個不錯的解壓縮封裝類。通過InterNET和個人的整理,現將該類分享出來,作為資源分享給大家,這樣就可以不用在埋頭苦腦的在InterNET上苦苦尋找了,廢話不多說,上代碼:
using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Zip; namespace Helper { public class Utily { /// <summary> /// 快速壓縮 /// </summary> /// <param name="filesPath">需要壓縮的文件夾路徑</param> /// <param name="zipFilePath">輸出路徑</param> /// <param name="pwd">密碼,可不寫</param> /// <param name="fileFilter">過濾條件</param> /// <param name="CreateEmptyDirectories">是否壓縮空文件夾</param> /// <param name="progressFun">處理進程</param> /// <param name="seconds">觸發的秒數</param> /// <param name="completeFun">完成事件</param> public static void CreateZipFile(string filesPath, string zipFilePath, string pwd, string fileFilter, bool CreateEmptyDirectories, ProgressHandler progressFun, double seconds, CompletedFileHandler completeFun) { FastZipEvents events = new FastZipEvents(); if (progressFun != null) { events.Progress = progressFun; events.ProgressInterval = TimeSpan.FromSeconds(seconds); } if (completeFun != null) { events.CompletedFile = completeFun; } FastZip zip = new FastZip(events); zip.CreateEmptyDirectories = CreateEmptyDirectories; if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd; zip.UseZip64 = UseZip64.On; zip.RestoreAttributesOnExtract = true; zip.RestoreDateTimeOnExtract = true; zip.CreateZip(zipFilePath, filesPath, true, fileFilter); } /// <summary> /// 快速解壓 /// </summary> /// <param name="zipFilePath">壓縮文件路徑</param> /// <param name="extractPath">解壓路徑</param> /// <param name="pwd">壓縮密碼</param> /// <param name="progressFun">進程</param> /// <param name="seconds">觸發時間</param> public static void ExtractZipFile(string zipFilePath, string extractPath, string pwd, ProgressHandler progressFun, double seconds) { FastZipEvents events = new FastZipEvents(); if (progressFun != null) { events.Progress = progressFun; events.ProgressInterval = TimeSpan.FromSeconds(seconds); } FastZip zip = new FastZip(events); zip.CreateEmptyDirectories = true; if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd; zip.UseZip64 = UseZip64.On; zip.RestoreAttributesOnExtract = true; zip.RestoreDateTimeOnExtract = true; zip.ExtractZip(zipFilePath, extractPath, FastZip.Overwrite.Always, null, "", "", true); } /// <summary> /// 快速解壓 /// </summary> /// <param name="zipFilePath">壓縮文件路徑</param> /// <param name="extractPath">解壓路徑</param> /// <param name="pwd">密碼</param> /// <param name="progressFun">進程</param> /// <param name="seconds">觸發時間</param> /// <param name="completeFun">壓縮過程中執行的函數</param> public static void ExtractZipFile(string zipFilePath, string extractPath, string pwd, ProgressHandler progressFun, double seconds, CompletedFileHandler completeFun) { FastZipEvents events = new FastZipEvents(); if (progressFun != null) { events.Progress = progressFun; events.ProgressInterval = TimeSpan.FromSeconds(seconds); } if (completeFun != null) { events.CompletedFile = completeFun; } FastZip zip = new FastZip(events); zip.CreateEmptyDirectories = true; if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd; zip.UseZip64 = UseZip64.On; zip.RestoreAttributesOnExtract = true; zip.RestoreDateTimeOnExtract = true; zip.ExtractZip(zipFilePath, extractPath, FastZip.Overwrite.Always, null, "", "", true); } /// <summary> /// 獲得壓縮包內原文件總大小 /// </summary> /// <param name="fileName"></param> /// <param name="fileFilter"></param> /// <param name="directoryFilter"></param> /// <returns></returns> public static long GetZipFileSize(string fileName, string fileFilter, string directoryFilter) { long b = 0; using (ZipFile zipFile = new ZipFile(fileName)) { PathFilter localFileFilter = new PathFilter(fileFilter); PathFilter localDirFilter = new PathFilter(directoryFilter); if (zipFile.Count == 0) { return 0; } for (int i = 0; i < zipFile.Count; ++i) { ZipEntry e = zipFile[i]; if (e.IsFile) { string path = Path.GetDirectoryName(e.Name); if (localDirFilter.IsMatch(path)) { if (localFileFilter.IsMatch(Path.GetFileName(e.Name))) { b += e.Size; } } } } } return b; } /// <summary> /// 獲得MD5校驗碼 /// </summary> /// <param name="filepath"></param> /// <returns></returns> public static string GetMD5(string filepath) { string returnStr = ""; FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] md5byte = md5.ComputeHash(fs); int i, j; foreach (byte b in md5byte) { i = Convert.ToInt32(b); j = i >> 4; returnStr += Convert.ToString(j, 16); j = ((i << 4) & 0x00ff) >> 4; returnStr += Convert.ToString(j, 16); } fs.Dispose(); return returnStr; } /// <summary> /// 解壓縮特定文件名的文件 /// </summary> /// <param name="path">文件路徑</param> /// <param name="addres">解壓縮路徑</param> ///查看本欄目