使用Packaging无法实现通用的zip(使用其他工具压缩)的解压,只支持通过Packaging压缩包zip的解压,而SharpZipLib是基于“GPL”开源方式,风险比较大。在codeplex找到一个更强大的压缩和解压开源库,SharpCompress,和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);
    }

黄聪:.NET中zip的压缩和解压——SharpCompress的更多相关文章

  1. .NET中zip的压缩和解压

    在.NET可以通过多种方式实现zip的压缩和解压:1.使用System.IO.Packaging:2.使用第三方类库:3.通过 System.IO.Compression 命名空间中新增的ZipArc ...

  2. zip的压缩和解压命令

    以下命令均在/home目录下操作cd /home #进入/home目录 1.把/home目录下面的data目录压缩为data.zip zip -r data.zip data #压缩data目录   ...

  3. python zip文件压缩和解压

    压缩 import shutil zipOutputName = "1234" # 输出1234.zip fileType = "zip" # 文件类型zip ...

  4. ZIP文件压缩和解压

    最近要做一个文件交互,上传和下载, 都是zip压缩文件,所以研究了下,写了如下的示例 注意引用  ICSharpCode.SharpZipLib.dll 文件 该dll文件可以到官方网站去下载, 我这 ...

  5. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

  6. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

  7. java中ant包中的org.apache.tools.zip实现压缩和解压缩

    其实apache中的ant包(请自行GOOGLE之ant.jar)中有一个更好的类,已经支持中文了,我们就不重复制造轮子了,拿来用吧,这里最主要的功能是实现了 可以指定多个文件 到同一个压缩包的功能 ...

  8. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...

  9. Ionic.Zip.dll文件压缩和解压

    Ionic.Zip.dll文件压缩和解压 下载地址: http://download.csdn.net/detail/yfz19890410/5578515 1.下载Ionic.Zip.dll组件,添 ...

随机推荐

  1. 查询表Or列的注释信息

    需求:开发人员需要DBA支持,查询表的注释说明,用于明确表的用途. 1.测试 session 1 创建测试表SQL> create table a_emp as select * from sc ...

  2. Python网络爬虫之requests模块(2)

    session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 有些时候,我们在使用爬 ...

  3. [LeetCode&Python] Problem 492. Construct the Rectangle

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

  4. quartz定时任务及时间设置

    quartz 定时任务时间设置1.这些星号由左到右按顺序代表 :     *    *     *     *    *     *   *                               ...

  5. PAT-1084(外观数列 ) && PAT-1085 (PAT单位排行)

    1084  利用字符串string的可加性 #include <bits/stdc++.h> using namespace std; int main () { int x,n; cin ...

  6. READ–IT: Assessing Readability of Italian Texts with a View to Text Simplification-paper

    https://aclanthology.info/pdf/W/W11/W11-2308.pdf 2 background2000年以前 ----传统可读性准则局限于表面的文本特征,例如the Fle ...

  7. 20155208 实验四 Android开发基础

    20155208 实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握An ...

  8. hdu4553 约会安排 线段树

    寒假来了,又到了小明和女神们约会的季节. 小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会.与此同时,也有很多基友找他开黑,由于数量实 ...

  9. js获取元素得几种情况

    HTML代码 <div class="divClass" name="myClass"> <input type="password ...

  10. Why service collaboration needs choreography AND orchestration

    转自:https://blog.bernd-ruecker.com/why-service-collaboration-needs-choreography-and-orchestration-239 ...