在项目中往往使用解压缩公共类,解压缩之后的文件占用空间小,也可进行加密,往往可以用于客户端上传附件,打包输出主程序等,其中的好处就不多说了,最近着手的项目中多次使用到了解压缩方法,现较流行的就是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 = ;
using (ZipFile zipFile = new ZipFile(fileName))
{
PathFilter localFileFilter = new PathFilter(fileFilter);
PathFilter localDirFilter = new PathFilter(directoryFilter); if (zipFile.Count == )
{
return ;
}
for (int i = ; 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 >> ;
returnStr += Convert.ToString(j, );
j = ((i << ) & 0x00ff) >> ;
returnStr += Convert.ToString(j, );
}
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, , size);
streamWriter.Write(buffer, , size);
streamWriter.Close();
}
}
ZipStream.Close();
}
}
}

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

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

今天就分享这么多吧。

版权声明:

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

《ICSharpCode快速解压缩帮助类》——即粘即用的更多相关文章

  1. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper

    在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间.压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式.压缩与解压缩的方法也很多,在.NET 2.0开始,在S ...

  2. PclZip:强大的PHP压缩与解压缩zip类

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...

  3. GZIP压缩、解压缩工具类

    GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...

  4. ZIP解压缩工具类

    import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Expan ...

  5. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  6. mybatis-generator-core快速生成实体类和Mapper

    日常使用Mybatis少不了和实体类和 Mapper 打交道.除了我们手写来实现,还可以使用 mybatis-generator-core 来快速生成 实体类和 Mapper. 步骤如下: 1.下载 ...

  7. eclipse快速定位当前类所在位置

    如何快速的找到一个类并且定位它所在的位置呢?这里以搜索Menu类为例说明. 可以通过CTRL + SHIFT +R的组合键,输入Menu 双击Menu.java即可跳转到对应的类上,但此时还不知道此类 ...

  8. es6 快速入门 系列 —— 类 (class)

    其他章节请看: es6 快速入门 系列 类 类(class)是 javascript 新特性的一个重要组成部分,这一特性提供了一种更简洁的语法和更好的功能,可以让你通过一个安全.一致的方式来自定义对象 ...

  9. [压缩解压缩] SharpZip--压缩、解压缩帮助类

    里面有三个类都是用于压缩和解压缩的.大家看下图片 看下面代码吧 /// <summary> /// 类说明:SharpZip /// 编 码 人:苏飞 /// 联系方式:361983679 ...

随机推荐

  1. iOS代码实现:创建按钮,绑定按钮事件,读取控件值

    // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...

  2. CSS3随笔系列之transform(一)—— transform-origin

    transform-origin属性平时似乎用得很少,它决定了变换时依赖的原点.基本的属性特性可以参考CSS手册. 如果在H5动画项目中,用到旋转的话,它还是不能小觑的. 假如我们做一个秋千效果 其实 ...

  3. MATLAB中的结构数组

    MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...

  4. 解决Tomcat无法加载css和js等静态资源文件

    解决思路有两个 一是,你使用了Apache服务器,html不交给Tomcat处理,所以你找不到Html等静态资源,所以你先停掉阿帕奇,然后只用Tomcat猫试试. 二是,像我一样,使用了Jetty开发 ...

  5. 创新高性能移动 UI 框架-Canvas UI 框架

    WebView 里无法获得的能力虽然是「体验增强」与「端基本能力」,但现都基本上有成熟解决方法.但后期的 UI 和 Layout 的性能反而是目前 Web 技术欠缺的.所以,无论是 Titanium ...

  6. Silicon Labs电容式触摸感应按键技术原理及应用

    市场上的消费电子产品已经开始逐步采用触摸感应按键,以取代传统的机械式按键.针对此趋势,Silicon Labs公司推出了内置微控制器(MCU)功能的电容式触摸感应按键(Capacitive Touch ...

  7. ConcurrentHashMap的get、put、size

    ConcurrentHashMap的get操作 get操作的高效之处在于整个get过程不需要加锁,get方法里将要使用的共享变量都定义成volatile. ConcurrentHashMap的Put操 ...

  8. struts1 工作原理

    struts1的原理和工作流程 struts1的工作原理或者说工作流程: 1.在web应用程序启动就会加载ActionServlet,ActionServlet从配置文件struts-config.x ...

  9. 为什么Nhibernate中属性和方法必须Virtual的

    如果你曾经用过NHibernate 2.0或者更高的版本,那您一定碰到过下面的错误:NHibernate.InvalidProxyTypeException: The following types ...

  10. eclipse报错 com/genuitec/eclipse/j2eedt/core/J2EEProjectUtil 转

    今天eclipse突然报了com/genuitec/eclipse/j2eedt/core/J2EEProjectUtil 错误,并且工程文件打不开了,在网上找了一下资料,然后按照方法操作了一遍,好了 ...