我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib,

就可以很容易的实现压缩和解压缩功能.

压缩文件:

 /// <summary>  /// 压缩指定文件生成ZIP文件  /// </summary>  /// <param name="topDirName">顶层文件夹名称</param>  /// <param name="fileNamesToZip">待压缩文件列表</param>  /// <param name="ZipedFileName">ZIP文件</param>  /// <param name="CompressionLevel">压缩比</param>  /// <param name="password">密码</param>  /// <param name="comment">压缩文件注释文字</param>  public static void ZipFile(string topDirName,   string fileNameToZip,   string ZipedFileName,    int CompressionLevel,   string password,     string comment    ){var ls = new List<string> { fileNameToZip };ZipFile(topDirName, ls.ToArray(), ZipedFileName, CompressionLevel, password, comment);}/// <summary>  /// 压缩指定文件生成ZIP文件  /// </summary>  /// <param name="topDirName">顶层文件夹名称</param>  /// <param name="fileNamesToZip">待压缩文件列表</param>  /// <param name="ZipedFileName">ZIP文件</param>  /// <param name="CompressionLevel">压缩比</param>  /// <param name="password">密码</param>  /// <param name="comment">压缩文件注释文字</param>  public static void ZipFile(string topDirName,   string[] fileNamesToZip,   string ZipedFileName,    int CompressionLevel,    string password,     string comment    ){using (var s = new ZipOutputStream(File.Open(ZipedFileName, FileMode.Create))){if (password != null && password.Length > 0)s.Password = password;if (comment != null && comment.Length > 0)s.SetComment(comment);s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression  foreach (string file in fileNamesToZip){using (FileStream fs = File.OpenRead(topDirName + file)){   //打开待压缩文件  byte[] buffer = new byte[fs.Length];fs.Read(buffer, 0, buffer.Length);      //读取文件流  ZipEntry entry = new ZipEntry(file);    //新建实例   entry.DateTime = DateTime.Now;entry.Size = fs.Length;s.PutNextEntry(entry);s.Write(buffer, 0, buffer.Length);}}s.Finish();}}

解压文件:

 /// <summary>  /// 解压缩ZIP文件到指定文件夹  /// </summary>  /// <param name="zipfileName">ZIP文件</param>  /// <param name="UnZipDir">解压文件夹</param>  /// <param name="password">压缩文件密码</param>  public static void UnZipFile(string zipfileName, string UnZipDir, string password){using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName))){if (!string.IsNullOrWhiteSpace(password))s.Password = password;try{ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(UnZipDir);string pathname = Path.GetDirectoryName(theEntry.Name);string fileName = Path.GetFileName(theEntry.Name);//生成解压目录   pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  directoryName = directoryName + "\\" + pathname;Directory.CreateDirectory(directoryName);if (fileName != String.Empty){//解压文件到指定的目录  using (var streamWriter = File.Create(directoryName + "\\" + fileName)){int size = 2048;byte[] data = new byte[2048];while (true){size = s.Read(data, 0, data.Length);if (size > 0){streamWriter.Write(data, 0, size);}else{break;}}streamWriter.Flush();}}}}catch (Exception eu){throw eu;}}}

  

本文引用:http://xqblog.top/Article.aspx?id=ART2018030900002

转载于:https://www.cnblogs.com/xqaizx/p/8533324.html

C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件相关推荐

  1. C#利用ICSharpCode.SharpZipLib.dll压缩文件和解压文件

    /// <summary>  /// 压缩文件  /// </summary> using System; using System.IO; using ICSharpCode ...

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

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

  3. 在C#中利用SharpZipLib进行文件的压缩和解压缩

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  4. zip (ICSharpCode.SharpZipLib.dll文件需要下载)

    ZipClass zc=new ZipClass (); zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩 ...

  5. 利用WinRar压缩和解压缩文件

    今天的rar shell只是一个简单应用,rar.exe和winrar.exe语法都是一样的. 对rar而言,用rar.exe最好,不需要判断winrar在哪里,而且非常小,因为没有界面,所有压缩选项 ...

  6. 使用ICSharpCode.SharpZipLib.dll实现在线解压缩

    ICSharpCode.SharpZipLib.dll 是一个基于GNU的免费库文件,他的功能很强大. 下载地址:http://www.icsharpcode.net/OpenSource/Sharp ...

  7. ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用

    MyZip.dll : 有BUG,会把子目录的文件解压到根目录.. ICSharpCode.SharpZipLib.dll: 把ICSharpCode.SharpZipLib.dll复制一份,重命名为 ...

  8. asp.net在线压缩和解压缩的实现

    我们经常会遇到批量上传的问题,也会遇到将某个目录下所有文件都上传到服务器上的问题.那么,如何解决此类问题呢?以前的技术一般采用ActiveX等方式,这里我采用SharpZlib来实现,听说VS2005 ...

  9. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

最新文章

  1. python培训班时间 费用-python培训班费用在多少?
  2. 使用junit做其他事情
  3. composer安装laravel-u-editor及其使用
  4. 常用的linux文件权限
  5. 题解【luogu2045 方格取数游戏加强版】
  6. EJB3.0高速入门项目开发步骤
  7. 基于Cache的Fibonacci数列的计算
  8. postfix只能发邮件,不能接收从其他邮箱发送的邮件 451 4.3.5 Server conf
  9. linux cp 复制目录下文件到另一个目录下
  10. Js字符串与十六进制的相互转换 【转】
  11. 中文字体的FontMetrics解析
  12. NitroShare 是一个局域网文件传输工具,支持 Windows、OS X 和 Linux。基于 Qt 开发。
  13. RANSAC算法简介
  14. 2021年数学建模国赛A题优秀论文(Word)(FAST”工作抛物面的优化设计)
  15. 通过windows启动流程分析rootkit的潜在位置
  16. c语言句子后移两位加密,用C语言实现对输入的引英文句子进行加密
  17. lol人物模型提取(七)
  18. 正则匹配英文括号( 中文括号【 里面的内容 封装工具类
  19. 用Redis存取两个人的共同好友名单
  20. 【CF833D】Red-Black Cobweb(点分治)

热门文章

  1. JAVA使用bean配置ftp_FTP文件上传(By)java
  2. python搭建django环境_在BAE上搭建python,django环境小记
  3. c++位运算_最全位运算总结
  4. python会议室系统预定_会议室预定系统
  5. python colorama_Python常用模块—— Colorama模块
  6. 多元线性回归模型中多重共线性问题处理方法
  7. 基于增量更新的协同过滤
  8. envi栅格TIF数据进行分割_栅格数据镶嵌
  9. C语言随机生成26个字母,菜鸟求助,写一个随机输出26个英文字母的程序
  10. 三维计算机视觉(八)--点云配准