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

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

     SharpCompress支持的格式:

Archive FormatCompression Format(s)Compress/DecompressArchive APIReader APIWriter API
RarRarDecompress(1)RarArchiveRarReaderN/A
Zip(2)None, DEFLATE, BZip2, LZMA/LZMA2, PPMdBothZipArchiveZipReaderZipWriter
TarNone, BZip2, GZipBothTarArchiveTarReaderTarWriter(3)
GZip (single file)GZipBothGZipArchiveGZipReaderGZipWriter
7Zip(4)LZMA, LZMA2, BZip2, PPMd, BCJ, BCJ2DecompressSevenZipArchiveN/AN/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。

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

CompressorCompress/Decompress
BZip2StreamBoth
GZipStreamBoth
DeflateStreamBoth
LZMAStreamBoth
PPMdStreamBoth

 

 

 

 

 

 

 

 

 

 

 

      使用也比较简单:

显示行号 复制代码 ?解压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,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值