前言
( W$ _, L4 r2 e# M3 \在我们项目开发中,经常需要解压缩功能,用来减少网络带宽、磁盘空间等,但是如果自己开发,需要掌握诸多知识,比如:压缩算法、兼容性等。给大家推荐一个解压缩开源库,让你轻松完成解压缩文件的功能。: ]# |" i" {* M2 y- p# N! Q* I* T
项目简介7 V- ~. F& C- S2 z2 ]; n% T* B
这个一个完全由C#开发的、具备压缩和解压缩的功能,支持Zip,GZip,Tar和BZip2等格式,方便你集成到各种.NET项目中。. t' ^! a3 M" z" W" L
项目特点
8 K& y9 b4 T, d" n j) M$ k6 f) ~1、完全开源和免费:也可以根据自己的需求,自行编译源码;- h* u0 a% v% m
2、跨平台:支持Windows、Linux和Mac OS
) F6 f2 v6 @* E; w4 L& U' c; D4 _! c3、灵活且易于使用:提供了多种压缩和解压缩的功能,包括读取和解压zip文件、写入zip文件、解压GZip文件、读取和解压tar文件等9 s0 w. d& Q. D2 b _+ _
4、高性能:性能接近于系统的压缩工具,但提供了更为方便的API。
+ u9 N: k4 f( \+ F示例代码
. C3 p$ D: P f! E" w& cusing ICSharpCode.SharpZipLib.Zip;
//设置密码
var password = "123456";
//压缩后的文件名称
string zipFileName = "1.zip";
//要压缩的目录
string sourceDirectory = "E:\\file";
//是否压缩子目录
bool recurse = true;
//过滤文件
string fileFilter = "";
//压缩文件
FastZip fastZip = new FastZip();
fastZip.Password = password;
fastZip.CreateEmptyDirectories = true;
fastZip.CreateZip(zipFileName, sourceDirectory, recurse, fileFilter);
//查看压缩后的大小
using (ZipFile zFile = new ZipFile(zipFileName))
{
Console.WriteLine("查看文件 : " + zFile.Name);
Console.WriteLine("");
Console.WriteLine("原始大小 大小 日期 时间 文件名");
Console.WriteLine("-------- -------- -------- ------ ---------");
foreach (ZipEntry e in zFile)
{
if (e.IsFile)
{
DateTime d = e.DateTime;
Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
d.ToString("dd-MM-yy"), d.ToString("HH:mm"),
e.Name);
}
}
} 效果
$ c4 O' l4 n$ Q; c! Q' c' J: `1 ^
- @; ~2 _& ~9 y2 D: w6 {
项目地址
8 Q5 g: k( \. i" e u7 w# ihttps://github.com/icsharpcode/SharpZipLib |