C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件
我们 开发时经常会遇到需要压缩文件的需求,利用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
C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件的更多相关文章
- C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩
ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...
- Zip压缩和解压缩
这个功能完全依靠一个第三方的类,ICSharpCode.SharpZipLib.dll,只是在网上搜了大半天,都没有关于这个类的详细解释,搜索的demo也是各种错误,感觉作者完全没有跑过,就那么贸贸然 ...
- C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)
我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...
- C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用
工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...
- 利用ICSharpCode进行压缩和解压缩
说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程 1:组件下载地址 参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 2: 文件类 // ...
- 在C#中利用SharpZipLib进行文件的压缩和解压缩收藏
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net(http://www.icsharpcode.net/OpenSource/SharpZipL ...
- 利用SharpZipLib进行字符串的压缩和解压缩
http://www.izhangheng.com/sharpziplib-string-compression-decompression/ 今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用 ...
- C#利用SharpZipLib进行文件的压缩和解压缩
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...
随机推荐
- 移动端调试利器-vConsole
现在移动端开发越来越火,随之而来的问题也越来越多,今天给大家介绍一款移动端调试神器,vconsole. 一.先引用文件,可以从https://www.bootcdn.cn/vConsole/下载,也可 ...
- 原生js替换jQuery各种方法-中文版
原文https://github.com/nefe/You-D... 原生JS与jQuery操作DOM对比 You Don't Need jQuery 前端发展很快,现代浏览器原生 API 已经足够好 ...
- php同一个用户同时只能登陆一个, 后登陆者踢掉前登陆者(排他登陆)
通常用户登陆,如果没有特别的限定, 同一个用户可以同时登陆, 今天搞了一个东西限定一个用户不能同时登陆到一个系统上, 后登陆者会把前面登陆的踢出来.(有点像QQ,同个帐号不能在多个地方同时在线, 后面 ...
- JS — 实现简单的数字时钟
js实现简单的数字时钟 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...
- [netty4][netty-common]FastThreadLocal及其相关类系列
FastThreadLocal 概述: ThreadLocal的一个特定变种改善,有更好的存取性能. 内部采用一个数组来代替ThreadLocal内部的hash表来存放变量.虽然这看起来是微不足道的, ...
- Shell脚本编程
1.linux中的变量 linux中的变量分为环境变量和普通变量,其中环境变量可以理解为全局变量,在所有shell的子程序中都可以引用,普通变量只能在自己的shell程序中使用,程序结束后变量无法保留 ...
- [oldboy-django][2深入django]django 官方中文文档 --扩展User
https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#extending-the-existing-user-model # 另 ...
- [报错处理]Could not find a version that satisfies the requirement xml (from versions)
安装xml库发生报错 pip3 install xml Collecting xml Could not find a version that satisfies the requirement x ...
- 关于mybitis的异常总结
由于原项目中系统登录用户表中新添加了字段来关联其他表,但原来的mapper和bean就得重新再逆向出来,逆向后,就参照着原来你mapper来添加一些自定义在mapper的方法,那么接下来就爆出异常了 ...