原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用。

一、压缩:

/// <summary>
/// 压缩
/// </summary>
/// <param name="sourceDirectory"></param>
/// <param name="targetZipName"></param>
/// <param name="recurse"></param>
/// <param name="filter"></param>
/// <returns></returns>
public static void CreateZip(string zipFileName, string sourceDirectory, bool recurse=true, string fileFilter="")
{
if (string.IsNullOrEmpty(sourceDirectory))
{
throw new ArgumentNullException("SourceZipDirectory");
}
if (string.IsNullOrEmpty(zipFileName))
{
throw new ArgumentNullException("TargetZipName");
}
if (!Directory.Exists(sourceDirectory))
{
throw new DirectoryNotFoundException("SourceDirecotry");
}
if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")
throw new ArgumentException("TargetZipName is not zip");
FastZip fastZip = new FastZip();
fastZip.CreateZip(zipFileName, sourceDirectory, recurse, fileFilter);
}

  

二、解压缩:

/// <summary>
/// 解压
/// </summary>
/// <param name="zipFileName"></param>
/// <param name="targetDirectory"></param>
/// <param name="fileFilter"></param>
public static void ExtractZip(string zipFileName, string targetDirectory, string fileFilter="")
{
if (string.IsNullOrEmpty(zipFileName))
{
throw new ArgumentNullException("ZIPFileName");
}
if (!File.Exists(zipFileName))
{
throw new FileNotFoundException("zipFileName");
}
if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")
{
throw new ArgumentException("ZipFileName is not Zip ");
}
FastZip fastZip = new FastZip();
fastZip.ExtractZip(zipFileName, targetDirectory, fileFilter);
}

三、添加文件至压缩文件中

 /// <summary>
/// 添加文件到压缩文件中
/// </summary>
/// <param name="zipFileName"></param>
/// <param name="filesNames"></param>
public static void AddFileToZip(string zipFileName, List<string> filesNames)
{
if (string.IsNullOrEmpty(zipFileName))
{
throw new ArgumentNullException("ZipName");
}
if (!File.Exists(zipFileName))
{
throw new FileNotFoundException("ZipName");
}
if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")
{
throw new ArgumentException("ZipFileName is not Zip ");
}
if(filesNames==null||filesNames.Count<)
return;
using (ZipFile zFile = new ZipFile(zipFileName))
{ zFile.BeginUpdate(); foreach (string fileName in filesNames)
{
zFile.Add(fileName);
} zFile.CommitUpdate();
} }

四、移除压缩文件中的文件

     /// <summary>
/// 移除压缩文件中的文件
/// </summary>
/// <param name="zipName"></param>
/// <param name="fileNames"></param>
public static void DeleteFileFromZip(string zipFileName, IList<string> fileNames)
{
if (string.IsNullOrEmpty(zipFileName))
{
throw new ArgumentNullException("ZipName");
}
if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")
{
throw new ArgumentException("ZipName");
}
if(fileNames==null||fileNames.Count<)
{
return ;
}
using (ZipFile zipFile = new ZipFile(zipFileName))
{
zipFile.BeginUpdate();
foreach(string fileName in fileNames)
{
zipFile.Delete(fileName);
}
zipFile.CommitUpdate();
}
}

以上是基于ICSharpCode.SharpZipLib.Zip的部分使用,当然还有许多地方需要学习的。ICSharpCode.SharpZipLib.Zip使用起来比较快速方便,不想GZip那样对文件进行压缩时,还要进行复杂的操作。

今天就写这么多吧。

基于ICSharpCode.SharpZipLib.Zip的压缩解压缩的更多相关文章

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

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

  2. C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类

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

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

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

  4. C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

    工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...

  5. C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压

    关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...

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

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

  7. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

  8. ICSharpCode.SharpZipLib.Zip

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

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

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

随机推荐

  1. symbol(s) not found for architecture i386

    此问题针对百度地图真机调试和模拟器.a文件的选取问题 "$(SRCROOT)/MobileYonyou/Third/BaiduMap_IOSSDK_v2.3.0_Lib/Release$(E ...

  2. php 用递归实现的无限级别分类

    <?php header("Content-type:text/html; charset=utf-8"); /**  *   * @category contry_cate ...

  3. nginx源代码分析--高性能server开发 常见的流程模型

    1.高性能server 对于高性能server对于.处理速度和占用空间小是典型特性.特别是当server经验C10K问题的时候(网络server在处理数以万计的client连接时.往往出现效率低下甚至 ...

  4. OCaml Language Sucks

    OCaml Language Sucks OCaml Language Sucks

  5. UVAlive 2519 Radar Installation (区间选点问题)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  6. B. 沙漠之旅(分组背包)

    B. 沙漠之旅 Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: % ...

  7. hdu1025(nlon(n)最长上升子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 大致思路:设置两个a,b数组,a数组存储数据,b数组存储最长不降序序列.此算法关键在于设计二分查 ...

  8. Node.js: What is the best "full stack web framework" (with scaffolding, MVC, ORM, etc.) based on Node.js / server-side JavaScript? - Quora

    Node.js: What is the best "full stack web framework" (with scaffolding, MVC, ORM, etc.) ba ...

  9. 怎样在Linux下通过ldapsearch查询活动文件夹的内容

    从Win2000開始.微软抛弃NT域而採用活动文件夹来管理Windows域.而活动文件夹就是微软基于遵守LDAP协议的文件夹服务.假设用扫描器扫描的话能够发现活动文件夹的389port是打开的.并且微 ...

  10. ubuntu Linux 安装和首次使用

    1.ubuntu Linux 安装后切换到root账户,在默认情况下,系统安装过程中需要创建一个用户,切换到root账号命令如下:$ sudo -s -H输入 当前账户密码就可以切换到root.2.u ...