IRONSOFTWAREHOME
開發者更新

Sharpziplib 提取 ZIP C#(對於開發者的運行原理)

Jacob Mellor, Chief Technology Officer @ Team Iron
Jacob Mellor
Updated: 2026年4月21日

在當今的數位環境中,資料管理至關重要,擁有高效的壓縮和解壓縮工具是至關重要的。 在.NET生態系統中,一個突出的工具是SharpZipLib。 在這篇文章中,我們將深入探討SharpZipLib,探索其功能、應用以及如何將其整合到您的.NET專案中。

什麼是SharpZipLib?

SharpZipLib是一個功能豐富的開源壓縮程式庫,用於.NET,完全使用C#編寫。 它提供對各種壓縮格式的全面支持,包括ZIP、GZip和Tar。 由專注的社群開發,SharpZipLib提供廣泛的功能,能有效地壓縮和解壓縮文件。

功能和能力

  1. **支持多種壓縮格式:**SharpZipLib支持流行的壓縮格式,如ZIP、GZip和Tar,以滿足多樣化的使用案例和需求。
  2. **基於流的操作:**該程式庫在流上運作,使開發者能夠處理來自不同來源的資料,包括文件、記憶體流或網路流。 這種靈活性方便地整合到應用程式的不同部分中。
  3. **壓縮等級:**開發者可以根據其特定需求調節壓縮等級,以在壓縮比和處理速度之間取得平衡。
  4. **密碼保護:**SharpZipLib允許建立受密碼保護的ZIP檔案,通過以指定密碼加密內容確保資料安全。
  5. **錯誤處理和恢復:**強大的錯誤處理機制使開發者能夠優雅地處理壓縮和解壓縮操作過程中的異常。 此外,SharpZipLib支持從損壞的檔案中恢復,增強可靠性。

使用情境

  1. **文件壓縮和存檔:**SharpZipLib是需要壓縮和存檔文件的應用程式的理想選擇,如備份工具、文件管理工具或資料匯出功能。
  2. **網路服務和API:**處理文件傳輸或資料交換的網路服務經常從壓縮中獲益,以減少帶寬使用。 SharpZipLib可以無縫地整合到這些服務中,有效地壓縮傳出資料或解壓傳入資料。
  3. **桌面應用:**處理大型資料集中或資源文件的桌面應用可以利用SharpZipLib壓縮文件以供儲存或分發。 這對於軟體安裝程式或資料同步工具尤其有用。
  4. **資料備份和儲存:**需要定期備份或壓縮格式儲存資料的應用可以自動化備份過程並有效地保存儲存空間,使用SharpZipLib。

SharpZipLib的優勢

  1. **開源:**作為一個開源程式庫,SharpZipLib鼓勵協作和社群貢獻,確保持續改進並適應不斷變化的需求。
  2. **跨平台相容性:**SharpZipLib用C#編寫並以.NET框架為目標,能夠與包括Windows、Linux和macOS在內的各種平台相容,提升其靈活性。
  3. **輕量且高效:**SharpZipLib的設計旨在輕量且高效,最大限度地減少資源消耗,同時提供高效能壓縮和解壓縮能力。
  4. **詳細的文件與支援:**完備的文件和社群支援使開發者在使用SharpZipLib時更容易進行整合和排除問題。

建立C# Visual Studio 專案

  1. 打開Visual Studio並點選"建立新專案"選項。

  2. 根據您的需求選擇合適的專案範本(例如,控制台應用程式、Windows Forms應用程式)。

    Sharpziplib Extract ZIP C# (How It Works For Developers): Figure 1 - For the new project, select a "Console App" in C#.

  3. 指定專案名稱和位置,然後點擊"下一步"。

    Sharpziplib Extract ZIP C# (How It Works For Developers): 圖2 - 配置專案,指定專案名稱、位置和解決方案名稱。 接下來,選擇.NET框架並點擊建立。

  4. 從附加資訊中選擇最新的.NET框架。 點擊"建立"以建立專案。

安裝過程

要將SharpZipLib整合到您的.NET專案中:

  1. 在您的Visual Studio IDE C# 控制檯應用程式中,右鍵點擊解決方案總管中的專案,然後選擇"管理NuGet套件..."

  2. 在NuGet套件管理器窗口中,搜尋"SharpZipLib"。

    Sharpziplib Extract ZIP C# (How It Works For Developers): Figure 3 - Install SharpZipLib using the Manage NuGet Package for Solution by searching "sharpziplib" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

  3. 從搜索結果中選擇"SharpZipLib",然後點擊"安裝"按鈕。

  4. NuGet會自動下載並將必要的依賴項新增到您的專案中。

程式碼範例

這裡有一個簡化範例,演示了如何使用SharpZipLib壓縮和解壓縮文件:

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;

namespace SharpZipLibExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\SourceDirectory";     // Source directory containing files to compress
            string zipFilePath = @"C:\OutputDirectory\compressed.zip"; // Output path for the compressed ZIP file

            // Compress files from the source directory
            CompressDirectory(sourceDirectory, zipFilePath);
            Console.WriteLine("Files compressed successfully.");

            string extractPath = @"C:\OutputDirectory\extracted"; // Path to extract the decompressed files

            // Decompress files from the ZIP archive
            Decompress(zipFilePath, extractPath);
            Console.WriteLine("Files decompressed successfully.");
        }

        // Method to compress all files in a directory to a ZIP file
        static void CompressDirectory(string sourceDirectory, string zipFilePath)
        {
            using (var zipOutputStream = new ZipOutputStream(File.Create(zipFilePath)))
            {
                zipOutputStream.SetLevel(5); // Set compression level (0-9), 5 as a mid-range

                // Recursively add files in the source directory to the ZIP file
                AddDirectoryFilesToZip(sourceDirectory, zipOutputStream);

                zipOutputStream.Finish();
                zipOutputStream.Close();
            }
        }

        // Method to add files from a directory to a ZIP output stream
        static void AddDirectoryFilesToZip(string sourceDirectory, ZipOutputStream zipOutputStream)
        {
            // Get list of files in the directory
            string[] files = Directory.GetFiles(sourceDirectory);

            foreach (string file in files)
            {
                var entry = new ZipEntry(Path.GetFileName(file)); // Create a new entry for each file
                zipOutputStream.PutNextEntry(entry);

                using (var fileStream = File.OpenRead(file))
                {
                    // Buffer for reading files
                    byte[] buffer = new byte[4096];
                    int sourceBytes;

                    // Read file and write to ZIP stream
                    while ((sourceBytes = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        zipOutputStream.Write(buffer, 0, sourceBytes);
                    }
                }
            }

            // Handle subdirectories recursively
            string[] subdirectories = Directory.GetDirectories(sourceDirectory);
            foreach (string subdirectory in subdirectories)
            {
                AddDirectoryFilesToZip(subdirectory, zipOutputStream);
            }
        }

        // Method to decompress files from a ZIP file
        static void Decompress(string zipFilePath, string extractPath)
        {
            using (var zipInputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
            {
                ZipEntry entry;
                // Read entries from the ZIP archive
                while ((entry = zipInputStream.GetNextEntry()) != null)
                {
                    string entryPath = Path.Combine(extractPath, entry.Name);

                    // Process files
                    if (entry.IsFile)
                    {
                        string directoryName = Path.GetDirectoryName(entryPath);
                        if (!Directory.Exists(directoryName))
                            Directory.CreateDirectory(directoryName);

                        using (var fileStream = File.Create(entryPath))
                        {
                            // Buffer for reading entries
                            byte[] buffer = new byte[4096];
                            int bytesRead;
                            // Read from ZIP stream and write to file
                            while ((bytesRead = zipInputStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                fileStream.Write(buffer, 0, bytesRead);
                            }
                        }
                    }
                    else if (entry.IsDirectory) // Process directories
                    {
                        Directory.CreateDirectory(entryPath);
                    }
                }
            }
        }
    }
}

SharpZipLib在.NET語言開發社區中長期以來為工作壓縮檔案(如ZIP、GZip、Tar和BZip2)提供了基本功能。然而,隨著技術進步和開發者尋求更高級的解決方案,SharpZipLib的某些限制已變得顯而易見。

SharpZipLib的限制

  1. **複雜性:**SharpZipLib的API可能很繁瑣且冗長,要求開發者撰寫冗長的程式碼來執行簡單的任務,如建立或提取ZIP檔案。
  2. **缺乏現代化功能:**SharpZipLib缺乏對現代.NET功能和平台的支持,使其在當前開發環境中不那麼適合。
  3. **有限的文件:**雖然SharpZipLib已有較長時間,但其文件常常簡略且已過時,使開發者難以入門或排查問題。
  4. **效能:**SharpZipLib的效能有時可能無法滿足開發者的期望,尤其是在處理大型或複雜的存檔時。

IronZIP:跨越鴻溝

IronZIP文件Iron Software概述開發,是一個用於管理.NET應用中的ZIP檔案的現代高效解決方案。 憑藉其直觀的API,開發者可以輕鬆建立、讀取和操作ZIP文件。 IronZIP提供高級功能,如可自定的壓縮等級和密碼保護,確保靈活性和資料安全。 IronZIP與最新.NET版本相容並優化效能,輕鬆高效地簡化存檔管理任務。

Sharpziplib Extract ZIP C# (How It Works For Developers): 圖4 - IronZIP for .NET: C# Zip檔案程式庫

IronZIP特徵作為一個強大、現代的解決方案出現,應對SharpZipLib的缺點。 以下是IronZIP填補空白的方式:

  1. **高級API:**IronZIP提供一個直觀且開發者友好的API,簡化了存檔管理任務。 使用IronZIP,開發者能夠通過少量程式碼完成複雜運算,減少開發時間和精力。
  2. **完整.NET支持:**IronZIP完全支持最新的.NET版本,包括.NET Core、.NET Standard和.NET Framework,確保與現代開發環境和平台的相容性。
  3. **全面的文件:**IronZIP附帶全面的文件和範例,讓開發者能快速掌握其功能和能力。 廣泛的文件有助於簡化學習曲線,便利於快速整合到項目中。
  4. **壓縮等級控制:**IronZIP為開發者提供壓縮等級的控制,允許他們根據需求調整壓縮等級。 這個特點使開發者能在文件大小減小和壓縮速度間取得平衡。
  5. **密碼保護:**IronZIP支持對ZIP檔案的密碼保護,加強對敏感資料的安全性。 開發者可以輕鬆地使用傳統、AES128和AES256密碼加密ZIP檔案,確保只有授權使用者能夠存取檔案內容。
  6. **效能優化:**IronZIP經過效能優化,提供比SharpZipLib更快的壓縮和解壓速度。 這個優化確保開發者可以有效處理大批量的資料而不影響效能。

瀏覽IronZIP文件以獲取更多有關如何開始使用IronZIP的資訊。IronZIP程式碼範例幫助您輕鬆開始。

IronZIP安裝

以下是整合XDocument與IronPDF的步驟:

  • 開啟Visual Studio IDE或您首選的IDE。

  • 從工具選單中,導航至NuGet套件管理器控制台。

  • 執行以下命令安裝IronZIP套件:

    PM > Install-Package IronPdf

  • 或者,您可以從方案的NuGet套件管理器中安裝。

  • 從NuGet瀏覽選項卡中選擇IronZIP並點擊安裝:

Sharpziplib Extract ZIP C# (How It Works For Developers): Figure 5 - Install IronZIP using the Manage NuGet Package for Solution by searching "IronZIP" in the search bar of NuGet Package Manager, then select the project and click on the Install button.

程式碼範例

以下源程式碼顯示了如何使用IronZIP輕鬆地建立ZIP文件,並且只需幾行程式碼。 在這裡,您可以通過在指定的文件夾中提供文件名,將多個文件新增到密碼保護的ZIP檔案中。 在建立IronZipArchive對像時,您可以指定壓縮等級以減小輸出文件的空間大小。

using IronZip;
using IronZip.Enum;

class Program
{
    static void Main()
    {
        // Create an empty ZIP with the highest compression
        using (var archive = new IronZipArchive(9))
        {
            // Password protect the ZIP (Support AES128 & AES256)
            archive.SetPassword("P@ssw0rd", EncryptionMethods.Traditional);
            archive.AddArchiveEntry("./assets/file1.txt");
            archive.AddArchiveEntry("./assets/file2.txt");
            // Export the ZIP
            archive.SaveAs("output.zip");
        }
    }
}

輸出Zip文件

Sharpziplib Extract ZIP C# (How It Works For Developers): 圖6 - 輸出:使用IronZIP建立的密碼保護的Zip檔案

結論

SharpZipLib概覽作為.NET的一個強大壓縮程式庫,提供了一套豐富的功能及處理壓縮文件的能力。 無論是壓縮資料以便儲存,存檔文件還是優化網路服務中寬帶使用,SharpZipLib提供所需的工具以簡化壓縮和解壓縮運作。 憑藉其開源特性、跨平台相容性和強大的功能,SharpZipLib仍然是開發者尋求可靠壓縮解決方案的首選。

雖然SharpZipLib一直是.NET應用中壓縮檔案可靠的選擇,但其在當今開發環境中的限制愈加明顯。 探索IronZIP API以彌補SharpZipLib留下的空白,提供了一個現代化且功能豐富的替代方案,優先考慮易用性、效能和相容性。 使用IronZIP,開發者可以在管理存檔中解鎖新的可能性,並通過高級功能和直觀的API簡化其開發工作流程。

IronZIP提供了免費試用授權概覽。 從IronZIP 下載中下載程式庫並試用。

Jacob Mellor, Chief Technology Officer @ Team Iron
Chief Technology Officer

Jacob Mellor is Chief Technology Officer at Iron Software and a visionary engineer pioneering C# PDF technology. As the original developer behind Iron Software's core codebase, he has shaped the company's product architecture since its inception, transforming it alongside CEO Cameron Rimington into a 50+ person company serving NASA, Tesla, and global government agencies.

...
Read More

Related Articles

Key in blue circle

立即免費取得 30 天試用金鑰

bullet_checked無需信用卡或建立帳號
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge related to IronPDF Product Demo

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立