C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类
最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下
using System;
using System.Linq;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Diagnostics;
using Microsoft.Win32; namespace ZipCommon
{
public class ZipHelper
{ #region 压缩多个文件 /// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="files">文件名</param>
/// <param name="ZipedFileName">压缩包文件名</param>
/// <param name="Password">解压码</param>
/// <returns></returns>
public static void Zip(string[] files, string ZipedFileName, string Password)
{
files = files.Where(f => File.Exists(f)).ToArray();
if (files.Length == ) throw new FileNotFoundException("未找到指定打包的文件");
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFileName));
s.SetLevel();
if (!string.IsNullOrEmpty(Password.Trim())) s.Password = Password.Trim();
ZipFileDictory(files, s);
s.Finish();
s.Close();
} /// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="files">文件名</param>
/// <param name="ZipedFileName">压缩包文件名</param>
/// <returns></returns>
public static void Zip(string[] files, string ZipedFileName)
{
Zip(files, ZipedFileName, string.Empty);
} private static void ZipFileDictory(string[] files, ZipOutputStream s)
{
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
//创建当前文件夹
entry = new ZipEntry("/"); //加上 “/” 才会当成是文件夹创建
s.PutNextEntry(entry);
s.Flush();
foreach (string file in files)
{
//打开压缩文件
fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
entry = new ZipEntry("/" + Path.GetFileName(file));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, , buffer.Length);
}
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
entry = null;
GC.Collect();
}
} #endregion 压缩多个文件 #region 解压文件 包括.rar 和zip /// <summary>
///解压文件
/// </summary>
/// <param name="fileFromUnZip">解压前的文件路径(绝对路径)</param>
/// <param name="fileToUnZip">解压后的文件目录(绝对路径)</param>
public static void UnpackFileRarOrZip(string fileFromUnZip, string fileToUnZip)
{
//获取压缩类型
string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + , ).ToLower(); switch (unType)
{
case "rar":
UnRar(fileFromUnZip, fileToUnZip);
break;
default:
UnZip(fileFromUnZip, fileToUnZip);
break; }
} #endregion #region 解压文件 .rar文件 /// <summary>
/// 解压
/// </summary>
/// <param name="unRarPatch"></param>
/// <param name="rarPatch"></param>
/// <param name="rarName"></param>
/// <returns></returns>
public static void UnRar(string fileFromUnZip, string fileToUnZip)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info; try
{
the_Reg = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
//the_rar = the_rar.Substring(1, the_rar.Length - 7); if (Directory.Exists(fileToUnZip) == false)
{
Directory.CreateDirectory(fileToUnZip);
}
the_Info = "x " + Path.GetFileName(fileFromUnZip) + " " + fileToUnZip + " -y"; ProcessStartInfo the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = Path.GetDirectoryName(fileFromUnZip);//获取压缩包路径 Process the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
throw ex;
}
//return unRarPatch;
} #endregion #region 解压文件 .zip文件 /// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder)
{
if (!File.Exists(FileToUpZip))
{
return;
} if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
} ICSharpCode.SharpZipLib.Zip.ZipInputStream s = null;
ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry = null; string fileName;
FileStream streamWriter = null;
try
{
s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(FileToUpZip));
while ((theEntry = s.GetNextEntry()) != null)
{ if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
///判断文件路径是否是文件夹 if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
} streamWriter = File.Create(fileName);
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length);
if (size > )
{
streamWriter.Write(data, , size);
}
else
{
break;
}
}
}
}
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (s != null)
{
s.Close();
s = null;
}
GC.Collect();
GC.Collect();
}
} #endregion
}
}
C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类的更多相关文章
- 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...
- 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩
原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...
- 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature
写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...
- 利用ICSharpCode.SharpZipLib.Zip进行文件压缩
官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...
- ICSharpCode.SharpZipLib实现压缩解压缩
最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...
- ICSharpCode.SharpZipLib.Zip
//压缩整个目录下载 var projectFolder = Request.Params["folder"] != null ? Request.Params["fol ...
- C# ICSharpCode.SharpZipLib.Zip 的使用
public static class ZipFileHelper { #region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件 ...
- ICSharpCode.SharpZipLib.Zip 压缩文件
public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...
- c# ICSharpCode.SharpZipLib.Zip实现文件的压缩
首先了解ZipOutPutStream和ZipEntry对象 ZipOutPutStream对象 如果要完成一个文件或文件夹的压缩,则要使用ZipOutputStream类.ZipOutputStre ...
随机推荐
- Linux运维入门到高级全套常用要点
Linux运维入门到高级全套常用要点 目 录 1. Linux 入门篇................................................................. ...
- WaitForMultipleObjects返回失败原因之一
上网搜了下 关于 WaitForMultipleObjects等待多个线程退出的状态失败的情况,也有人遇到类似的情况. 一次项目中我也遇到这么个情况.项目中创建线程都是用的 _beginthread ...
- C语言结构体位域
demo: typedef struct { int a:2; int b:2; int c:1; }test; int main() { test t; t.a=1; t.b=3; t.c=1; / ...
- Servlet执行流程和生命周期【慕课网搬】
Servlet执行流程(GET方式为例) 首先用户客户端浏览器发出Get方式(点击超链接方式)向浏览器发出请求. 服务器接收到客户端点击超链接,接收到GET请求之后,服务器到WEB.xml中<s ...
- Python更换国内源实现快速PIP安装
WINDOWS 安装pip 1.首先下载安装Python,并将python的安装目录添加进系统环境变量 2.复制这个文件保存为.py并执行 https://bootstrap.pypa.io/get- ...
- ubuntu12.04+Elasticsearch2.3.3伪分布式配置,集群状态分片调整
目录 [TOC] 1.什么是Elashticsearch 1.1 Elashticsearch介绍 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.能够快速搜索数 ...
- WebService 错误:无法加载协定为xxx的终结点配置部分,因为找到了该协定的多个终结点配置
当在vs 2008中添加服务引用后,如果“更新”服务引用,或“删除”该服务引用后再次加入后,在运行时会出现此错误.这是因为在“更新/删除”服务引用时,app.config文件并不会自动修改,在“更新” ...
- SOA 面向服务的体系结构
SOA:面向服务的体系结构(service-oriented architecture) 是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来. 接口是采 ...
- [I2C]I2C总线协议图解
转自:http://blog.csdn.net/w89436838/article/details/38660631 1 I2C总线物理拓扑结构 I2C 总线在物理连接上非常简单,分别由S ...
- flash flex 程序出现错误 Error #2032
解决思路参考: http://www.cnblogs.com/enjoyprogram/archive/2012/06/21/2557615.html 有可能是这种情况: 状况:在安装flshbuil ...