前言; f, { ^3 U0 a. P& F% G
在我们项目开发中,经常需要解压缩功能,用来减少网络带宽、磁盘空间等,但是如果自己开发,需要掌握诸多知识,比如:压缩算法、兼容性等。给大家推荐一个解压缩开源库,让你轻松完成解压缩文件的功能。
4 v9 b8 M/ ?9 Z f项目简介8 O( a& P3 x6 g& r5 a1 N8 t" ?
这个一个完全由C#开发的、具备压缩和解压缩的功能,支持Zip,GZip,Tar和BZip2等格式,方便你集成到各种.NET项目中。
' v+ [# P; f% ~) z& {" t0 Z项目特点
& g0 V* U O' c5 S1、完全开源和免费:也可以根据自己的需求,自行编译源码;+ B' p8 s" e" X* T
2、跨平台:支持Windows、Linux和Mac OS r7 O, P- g. J4 z! z
3、灵活且易于使用:提供了多种压缩和解压缩的功能,包括读取和解压zip文件、写入zip文件、解压GZip文件、读取和解压tar文件等: V8 T0 B) B3 g9 Y
4、高性能:性能接近于系统的压缩工具,但提供了更为方便的API。
4 T. j, V4 E+ e1 Y# h! Q2 [示例代码
h3 [% \+ v5 qusing 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);
}
}
} 效果
: n5 }* x4 p( S9 t- h
# B/ {: J# A% [( W
项目地址7 {4 f/ c4 B' S& V" g6 e6 P4 ?
https://github.com/icsharpcode/SharpZipLib |