博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET中zip的压缩和解压——SharpCompress
阅读量:6256 次
发布时间:2019-06-22

本文共 2902 字,大约阅读时间需要 9 分钟。

使用Packaging无法实现通用的zip(使用其他工具压缩)的解压,只支持通过Packaging压缩包zip的解压,而SharpZipLib是基于“GPL”开源方式,风险比较大。在codeplex找到一个更强大的压缩和解压开源库,,和DotNetZip一样都是“MS-PL”开源方式。

     SharpCompress支持的格式:

Archive Format Compression Format(s) Compress/Decompress Archive API Reader API Writer API
Rar Rar Decompress(1) RarArchive RarReader N/A
Zip(2) None, DEFLATE, BZip2, LZMA/LZMA2, PPMd Both ZipArchive ZipReader ZipWriter
Tar None, BZip2, GZip Both TarArchive TarReader TarWriter(3)
GZip (single file) GZip Both GZipArchive GZipReader GZipWriter
7Zip(4) LZMA, LZMA2, BZip2, PPMd, BCJ, BCJ2 Decompress SevenZipArchive N/A N/A

(1) SOLID Rars are only supported in the RarReader API. 

(2) Zip format supports pkware and WinzipAES encryption. However, encrypted LZMA is not supported. 
(3) The Tar format requires a file size in the header. If no size is specified to the TarWriter and the stream is not seekable, then an exception will be thrown. 
(4) The 7Zip format doesn't allow for reading as a forward-only stream so 7Zip is only supported through the Archive API。

    也支持流方式的压缩和解压:

Compressor Compress/Decompress
BZip2Stream Both
GZipStream Both
DeflateStream Both
LZMAStream Both
PPMdStream Both

 

 

 

 

 

 

 

 

 

 

 

      使用也比较简单:

显示行号 复制代码 ?解压Rar文件
  1. using (Stream stream = File.OpenRead(@"C:\Code\sharpcompress.rar")){    var reader = ReaderFactory.Open(stream);    while (reader.MoveToNextEntry())    {        if (!reader.Entry.IsDirectory)        {            Console.WriteLine(reader.Entry.FilePath);            reader.WriteEntryToDirectory(@"C:\temp", ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);        }    }}
显示行号 复制代码 ?解压zip文件
  1. var archive = ArchiveFactory.Open(@"C:\Code\sharpcompress\TestArchives\sharpcompress.zip");           foreach (var entry in archive.Entries)           {               if (!entry.IsDirectory)               {                   Console.WriteLine(entry.FilePath);                   entry.WriteToDirectory(@"C:\temp", ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);               }           }
显示行号 复制代码 ?压缩成zip文件
  1. using (var archive = ZipArchive.Create()){    archive.AddAllFromDirectoryEntry(@"C:\\source");    archive.SaveTo("@C:\\new.zip");}
using (Stream stream = File.OpenWrite(tarPath))
using (var writer = WriterFactory.Open(ArchiveType.Tar, stream))
{
writer.WriteAll(filesPath, "*", SearchOption.AllDirectories);
}
using (Stream stream = File.OpenWrite(tarbz2Path))
using (var writer = WriterFactory.Open(ArchiveType.BZip2, stream))
{
writer.Write("Tar.tar", tarPath);
}
 

 

显示行号 复制代码 ?创建Tar文件
  1. using (Stream stream = File.OpenWrite(tarPath))using (var writer = WriterFactory.Open(ArchiveType.Tar, stream)){    writer.WriteAll(filesPath, "*", SearchOption.AllDirectories);}using (Stream stream = File.OpenWrite(tarbz2Path))using (var writer = WriterFactory.Open(ArchiveType.BZip2, stream)){    writer.Write("Tar.tar", tarPath);}
     
  2. 本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/p/8178274.html,如需转载请自行联系原作者
你可能感兴趣的文章
【mongdb主从复制和同步】
查看>>
下载文件downloadFile
查看>>
cf-Round542-Div2-B(贪心)
查看>>
日志挖掘(logminer)
查看>>
LaTeX技巧005:定制自己炫酷的章节样式实例
查看>>
1_NAT模式和桥接模式下的网络配置
查看>>
EF架构~为EF DbContext生成的实体添加注释(T5模板应用)
查看>>
【转】VLAN原理详解
查看>>
python --- json模块和pickle模块详解
查看>>
idea中artifacts、facets、modules是什么意思?
查看>>
FUCKED-BUG之临时对象的生死
查看>>
SP2 PRIME1 - Prime Generator
查看>>
创建和编辑 crontab 文件
查看>>
钉钉发消息
查看>>
20172309_《程序设计与数据结构(下)》_课堂测试修改报告。
查看>>
(二十九)方法调用之解析
查看>>
Springboot文件上传与下载
查看>>
Activity与Fragment数据传递之Fragment从Activity获取数据 分类: ...
查看>>
记一次小的51CTO聚会
查看>>
架構設計_案例說明_by 高老師
查看>>