黃聰:.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);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM