首页 > 其他 > 详细

《ICSharpCode快速解压缩帮助类》——即粘即用

时间:2014-04-02 10:21:57      阅读:481      评论:0      收藏:0      [点我收藏+]

在项目中往往使用解压缩公共类,解压缩之后的文件占用空间小,也可进行加密,往往可以用于客户端上传附件,打包输出主程序等,其中的好处就不多说了,最近着手的项目中多次使用到了解压缩方法,现较流行的就是ICSharpCode,稳定,高效,是一个不错的解压缩封装类。通过InterNET和个人的整理,现将该类分享出来,作为资源分享给大家,这样就可以不用在埋头苦脑的在InterNET上苦苦寻找了,废话不多说,上代码:

bubuko.com,布布扣
  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 }
bubuko.com,布布扣

该类能够满足基本常用解压缩的方法了,不过现比较流行的应该是7z压缩,这个也在研究中,以上代码若有不正确的地方,也请各位大牛指正。至于ICSharpCode的DLL文件,网上能够下载的地方也很多,我也就不在给出下载地址了。

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

今天就分享这么多吧。

 

版权声明:

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

《ICSharpCode快速解压缩帮助类》——即粘即用,布布扣,bubuko.com

《ICSharpCode快速解压缩帮助类》——即粘即用

原文:http://www.cnblogs.com/doNetTom/p/3638227.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!