即粘即用的ICSharpCode快速解压缩帮助类2015-12-21 cnblogs _tom在项目中往往使用解压缩公共类,解压缩之后的文件占用空间小,也可进行加密,往往可以用于客户端上传附件,打包输出主程序等,其中的好处就不多说了,最近着手的项目中多次使用到了解压缩方法,现较流行的就是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>////// <param name="zipFileName">文件名称</param>/// <param name="pwd">解压缩包密码</param>public static void ZipToFile(string path, string addres, string zipFileName, string pwd){ZipInputStream ZipStream = new ZipInputStream(System.IO.File.OpenRead(path));if (!string.IsNullOrEmpty(pwd))ZipStream.Password = pwd;ZipEntry fileEntry;while ((fileEntry = ZipStream.GetNextEntry()) != null){string filename = Path.GetFileName(fileEntry.Name);if (filename == zipFileName){filename = addres + "\" + filename;FileStream streamWriter = System.IO.File.Create(filename);int size = (int)fileEntry.Size;byte[] buffer = new byte[size];size = ZipStream.Read(buffer, 0, size);streamWriter.Write(buffer, 0, size);streamWriter.Close();}}ZipStream.Close();}}}
该类能够满足基本常用解压缩的方法了,不过现比较流行的应该是7z压缩,这个也在研究中,以上代码若有不正确的地方,也请各位大牛指正。至于ICSharpCode的DLL文件,网上能够下载的地方也很多,我也就不在给出下载地址了。温馨提醒:在引用ICSharpCode时记的在调用此类方法的类库或应用程序上也要引用ICSharpCode,否则会产生错误。