《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         /// 
 13         /// 快速压缩
 14         /// 
 15         /// 需要压缩的文件夹路径
 16         /// 输出路径
 17         /// 密码,可不写
 18         /// 过滤条件
 19         /// 是否压缩空文件夹
 20         /// 处理进程
 21         /// 触发的秒数
 22         /// 完成事件
 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         /// 
 46         /// 快速解压
 47         /// 
 48         /// 压缩文件路径
 49         /// 解压路径
 50         /// 压缩密码
 51         /// 进程
 52         /// 触发时间
 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         /// 
 73         /// 快速解压
 74         /// 
 75         /// 压缩文件路径
 76         /// 解压路径
 77         /// 密码
 78         /// 进程
 79         /// 触发时间
 80         /// 压缩过程中执行的函数
 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         /// 
105         /// 获得压缩包内原文件总大小
106         /// 
107         /// 
108         /// 
109         /// 
110         /// 
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         /// 
143         /// 获得MD5校验码
144         /// 
145         /// 
146         /// 
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         /// 
167         /// 解压缩特定文件名的文件
168         /// 
169         /// 文件路径
170         /// 解压缩路径
171         /// 文件名称
172         /// 解压缩包密码
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文件,网上能够下载的地方也很多,我也就不在给出下载地址了。

温馨提醒:在引用ICSharpCode时记的在调用此类方法的类库或应用程序上也要引用ICSharpCode,否则会产生错误。

今天就分享这么多吧。

 

版权声明:

  本文由Tom原创并发布于博客园,欢迎转载,未经本人同意必须保留此段声明(否则保留追究责任的权利),且在文章页面明显位置给出原文链接,如有问题,可以通过419187544@qq.com 联系我,非常感谢。

你可能感兴趣的:(《ICSharpCode快速解压缩帮助类》——即粘即用)