[C#.NET] 使用 SharpZipLib 處理壓縮檔案

[C#.NET] 使用 SharpZipLib 處理壓縮檔案

上篇 http://www.dotblogs.com.tw/yc421206/2013/09/05/116408 使用 DotNetZip 處理壓縮檔

本篇要介紹使用 SharpZipLib

http://www.icsharpcode.net/OpenSource/SharpZipLib/ 

它是老牌壓縮 SDK 也是蠻多人在使用的壓縮檔元件,不過它使用起來比較沒有那麼直覺,若對 Stream 處理不瞭解,可能會不容易上手

 

本篇章節

SDK 下載及解壓縮

範例

壓縮大檔案

Issue


SDK 下載及解壓縮

下載位置:http://sourceforge.net/projects/sharpdevelop/files/SharpZipLib/0.86/SharpZipLib_0860_SourceSamples.zip/download?use_mirror=nchc

解壓縮後,將 ICSharpCode.SharpZipLib.dll 加入參考

或是用 NuGet 取得 SDK

SNAGHTML80bbbbf

 

範例

下載位置:

http://sourceforge.net/projects/sharpdevelop/files/SharpZipLib/0.86/SharpZipLib_0860_SourceSamples.zip/download?use_mirror=nchc

https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples
https://github.com/icsharpcode/SharpZipLib/wiki/Code-Reference
壓縮到檔案
{
    var inputFile = "1.iso";
    var outputFile = "1.zip";
    byte[] buffer = new byte[4096];
    using (var output = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
    using (var input = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
    using (var zip = new ZipOutputStream(output))
    {
        ZipEntry entry = new ZipEntry(inputFile);
        entry.DateTime = DateTime.Now;
        zip.PutNextEntry(entry);
        int readLength;
        do
        {
            readLength = input.Read(buffer, 0, buffer.Length);
            if (readLength > 0)
            {
                zip.Write(buffer, 0, readLength);
            }
        } while (readLength > 0);
    }
}
 
解壓縮到檔案
{
    var inputFile = "1.zip";
    using (var input = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
    using (ZipInputStream zip = new ZipInputStream(input))
    {
        ZipEntry entry = zip.GetNextEntry();
        using (FileStream output = new FileStream(entry.Name, FileMode.Create, FileAccess.Write))
        {
            byte[] data = new byte[4096];
            var readLength = 0;
            while (true)
            {
                readLength = zip.Read(data, 0, data.Length);
                if (readLength > 0)
                    output.Write(data, 0, readLength);
                else
                    break;
            }
        }
    }
}
 
壓縮到 Stream
{
    var inputFile = "1.txt";

    byte[] buffer = new byte[4096];
    using (var output = new MemoryStream())
    using (var input = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
    using (var zip = new ZipOutputStream(output))
    {
        ZipEntry entry = new ZipEntry(inputFile);
        //entry.CompressionMethod = CompressionMethod.Deflate64;
        entry.DateTime = DateTime.Now;
        zip.PutNextEntry(entry);
        int readLength;
        do
        {
            readLength = input.Read(buffer, 0, buffer.Length);
            if (readLength > 0)
            {
                zip.Write(buffer, 0, readLength);
            }
        } while (readLength > 0);
    }
}

 

解壓縮到 Stream

{
    var inputFile = "1.zip";
    byte[] bufferArray = new byte[4096];

    using (var input = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
    using (var unzip = new ZipInputStream(input))
    {
        ZipEntry entry = unzip.GetNextEntry();
        using (var output = new MemoryStream())
        {
            var readLength = 0;
            while (true)
            {
                readLength = unzip.Read(bufferArray, 0, bufferArray.Length);
                if (readLength > 0)
                {
                    output.Write(bufferArray, 0, readLength);
                }
                else
                {
                    break;
                }
            }
        }
    }
}

 

壓縮大檔案

很順利的就壓好了,壓縮效果看來不錯

SNAGHTML7ce51f2

 

Issue

SharpZipLib 壓縮效能如下圖,CPU損耗跟 7-zip 差不多,但 IO 比 7-zip 高

SNAGHTML860988a

 

7-zip 壓縮效能如下圖:

SNAGHTML4f64593_thumb

 


文章出自:http://www.dotblogs.com.tw/yc421206/archive/2013/09/05/116442.aspx

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo