.NET中zip的压缩和解压——SharpCompress

简介:

  使用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);
    }
    
    
    本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/p/8178274.html,如需转载请自行联系原作者
相关文章
|
.NET 数据格式
asp.net zip 压缩传输
原文:asp.net zip 压缩传输 在实际生产中,比如使用xml json 等传输大量数据的时候,有时候会出现等待时间过长,这里分享一个压缩传输的方法 首先到网上去下载一个 ICSharpCode.
906 0
|
Windows Memcache
Memcached1.4.13(32、64位windows)在.Net中使用.zip
创建自动启动windows服务命令: sc create "Memcached Server" binpath= "E:\Memcached\memcached.
938 0
|
算法 数据安全/隐私保护
.NET开源压缩组件介绍与入门
原文 http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 1.前言 作为吉日嘎拉权限管理系统最早的一批学习版用户,学了不少东西,在群里面也结识了很多朋友,更重要的是闲余时间,大家都发布很多可靠的外包工作。
1272 0
.net 实现GZip页面压缩
private void Application_BeginRequest(object sender, EventArgs e) { var context = HttpContext.
774 0
|
13天前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
67 0