最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下

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 实现解压缩功能公用类的更多相关文章

  1. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

  2. 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

    原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...

  3. 使用NPOI读取Excel报错ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature

    写了一个小程序利用NPOI来读取Excel,弹出这样的报错: ICSharpCode.SharpZipLib.Zip.ZipException:Wrong Local header signature ...

  4. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  5. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...

  6. ICSharpCode.SharpZipLib.Zip

    //压缩整个目录下载 var projectFolder = Request.Params["folder"] != null ? Request.Params["fol ...

  7. C# ICSharpCode.SharpZipLib.Zip 的使用

    public static class ZipFileHelper { #region 加压解压方法 /// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件 ...

  8. ICSharpCode.SharpZipLib.Zip 压缩文件

    public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...

  9. c# ICSharpCode.SharpZipLib.Zip实现文件的压缩

    首先了解ZipOutPutStream和ZipEntry对象 ZipOutPutStream对象 如果要完成一个文件或文件夹的压缩,则要使用ZipOutputStream类.ZipOutputStre ...

随机推荐

  1. .lib文件 .h文件 .dll文件

    .lib代表的是静态数据连接库,在windows系统中起到链接程序和函数的作用,存放的是函数的是函数调用的信息,是obj文件的集合.相当于linux中的.a或.0. .so文件.lib文件是不对外公开 ...

  2. error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用

    MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用 Debug ...

  3. JPanel

    class NewPanel extends JPanel{ public NewPanel() {} public void paintComponent(Graphics g) { int x=0 ...

  4. 【转】android 属性动画之 ObjectAnimator

    原文网址:http://blog.csdn.net/feiduclear_up/article/details/39255083 前面一篇博客讲解了 android 简单动画之 animtion,这里 ...

  5. #uwp# XMAL

    类型转换 在xaml中对属性赋值时,会将填入的字符串转换成对应的属性类型.比如: <Button Visibility="Visible" /> 会将Visible这个 ...

  6. 使用jQuery Autocomplete(自动完成)插件

    jQuery 的Autocomplete(自动完成.自动填充)插件有不少,但比较下来我感觉,还是bassistance.de 的比较强大,我们就来写一些代码感受一下. 最简单的Autocomplete ...

  7. 简述cookie

    1.Cookie的概述 * Cookie是客户端的技术(默认把Cookie保存在每个用户的浏览器上) * 程序把每个用户的数据以cookie的形式写给用户各自的浏览器 * 当用户使用浏览器再去访问服务 ...

  8. Selenium2入门(三)WebDriver API之Get

    在上面的例子中,我们看到了WebDriver的一些方法,今天列举WebDriver的Get方法: import java.util.Set; import org.openqa.selenium.We ...

  9. IOS开发-影院选座算法 限制产生孤座

    众所周知目前影院选座是不允许随便选的,我们不可以任性的挑三拣四,最后留下N个单独的座位,目的就是要留下至少2个连着的座位: 另外有些影院的座位摆放并不是规则的,有些座位被过道或者特殊座位分割开,产生了 ...

  10. uml和模式01

    // */ // ]]> uml和模式01 1. UML 2. 用例图 3. 用例和类的关系 4. 类图 1 UML 模型语言(Modeling Language 检查ML)是一种设计语言,人们 ...