IronZip 入門指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronZIP:您的 .NET 全方位壓縮程式庫

IronZIP 是由 Iron Software 開發的檔案壓縮與解壓縮程式庫。 除了廣泛使用的 ZIP 格式外,它也能處理 TAR、GZIP 和 BZIP2 格式。

C# 檔案壓縮與解壓縮函式庫

  1. 下載用於檔案壓縮與解壓縮的 C# 函式庫
  2. 支援 ZIP、TAR、GZIP 及 BZIP2 格式
  3. 自訂壓縮等級,範圍為 0 至 9
  4. 從壓縮檔案中提取內容
  5. 將檔案附加至現有 ZIP 壓縮檔並產生新的 ZIP 檔案

相容性

IronZip 支援以下平台的相容性:

.NET 版本支援:

  • C#VB.NETF#
  • .NET 7、6、5 及 .NET Core 3.1+
  • .NET Standard (2.0+)
  • .NET Framework (4.6.2+)

支援的作業系統與環境:

  • Windows (10 以上、Server 2016 以上)
  • Linux(Ubuntu、Debian、CentOS 等)
  • macOS (10+)
  • iOS (12+)
  • Android API 21+ (v5 "Lollipop")
  • Docker (Windows、Linux、Azure)
  • Azure(VPS、WebApp、Function)
  • AWS (EC2, Lambda)

.NET 專案類型支援:

  • Web (Blazor 與 WebForms)
  • 行動裝置 (Xamarin 與 MAUI)
  • 桌面版 (WPF 與 MAUI)
  • 控制台(應用程式與函式庫)

安裝

IronZIP程式庫

若要安裝 IronZIP 套件,請在終端機或套件管理員主控台中執行以下指令:

Install-Package IronZip

您亦可直接從 IronZIP 官方 NuGet 網站下載。

安裝完成後,您只需在 C# 程式碼開頭加入 using IronZip; 即可開始使用。

套用授權金鑰

接著,透過將授權金鑰指派給 License 類別的 LicenseKey 屬性,為 IronZip 套用有效的授權金鑰或試用金鑰。 請在 import 語句之後、使用任何 IronZIP 方法之前,加入以下程式碼:

using IronZip;

// Apply your license key here
IronZip.License.LicenseKey = "YOUR_LICENSE_KEY";
using IronZip;

// Apply your license key here
IronZip.License.LicenseKey = "YOUR_LICENSE_KEY";
Imports IronZip

' Apply your license key here
IronZip.License.LicenseKey = "YOUR_LICENSE_KEY"
$vbLabelText   $csharpLabel

程式碼範例

建立檔案範例

請使用 using 語法建立 ZIP 檔案。 在 using 區塊內,請使用 AddArchiveEntry 方法將檔案匯入 ZIP 檔案。最後,使用 SaveAs 方法匯出 ZIP 檔案。

using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Create a new ZIP file
        using (var archive = new ZipArchive())
        {
            // Add a file to the archive
            archive.AddArchiveEntry("example.txt", File.ReadAllBytes("path/to/example.txt"));

            // Save the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Create a new ZIP file
        using (var archive = new ZipArchive())
        {
            // Add a file to the archive
            archive.AddArchiveEntry("example.txt", File.ReadAllBytes("path/to/example.txt"));

            // Save the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
Imports IronZip
Imports System.IO

Friend Class Program
	Shared Sub Main()
		' Create a new ZIP file
		Using archive = New ZipArchive()
			' Add a file to the archive
			archive.AddArchiveEntry("example.txt", File.ReadAllBytes("path/to/example.txt"))

			' Save the ZIP archive
			archive.SaveAs("archive.zip")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

將壓縮檔解壓縮至資料夾

請使用 ExtractArchiveToDirectory 方法從 ZIP 檔案中提取內容。 請指定目標 ZIP 檔案的路徑及解壓縮目錄。

using IronZip;

class Program
{
    static void Main()
    {
        // path to the ZIP file and extraction directory
        string zipPath = "archive.zip";
        string extractPath = "extracted/";

        // Extract all files in the ZIP archive to the specified directory
        using (var archive = new ZipArchive(zipPath))
        {
            archive.ExtractArchiveToDirectory(extractPath);
        }
    }
}
using IronZip;

class Program
{
    static void Main()
    {
        // path to the ZIP file and extraction directory
        string zipPath = "archive.zip";
        string extractPath = "extracted/";

        // Extract all files in the ZIP archive to the specified directory
        using (var archive = new ZipArchive(zipPath))
        {
            archive.ExtractArchiveToDirectory(extractPath);
        }
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main()
		' path to the ZIP file and extraction directory
		Dim zipPath As String = "archive.zip"
		Dim extractPath As String = "extracted/"

		' Extract all files in the ZIP archive to the specified directory
		Using archive = New ZipArchive(zipPath)
			archive.ExtractArchiveToDirectory(extractPath)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

將檔案新增至現有壓縮檔

將 ZIP 檔案路徑傳遞給建構函式即可開啟 ZIP 檔案。請使用相同的 AddArchiveEntry 方法將檔案新增至已開啟的 ZIP 檔案中,並透過 SaveAs 方法將其匯出。

using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Open an existing ZIP file
        using (var archive = new ZipArchive("archive.zip"))
        {
            // Add more files to the archive
            archive.AddArchiveEntry("anotherfile.txt", File.ReadAllBytes("path/to/anotherfile.txt"));

            // Save updates to the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Open an existing ZIP file
        using (var archive = new ZipArchive("archive.zip"))
        {
            // Add more files to the archive
            archive.AddArchiveEntry("anotherfile.txt", File.ReadAllBytes("path/to/anotherfile.txt"));

            // Save updates to the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
Imports IronZip
Imports System.IO

Friend Class Program
	Shared Sub Main()
		' Open an existing ZIP file
		Using archive = New ZipArchive("archive.zip")
			' Add more files to the archive
			archive.AddArchiveEntry("anotherfile.txt", File.ReadAllBytes("path/to/anotherfile.txt"))

			' Save updates to the ZIP archive
			archive.SaveAs("archive.zip")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

提供授權與技術支援

IronZIP 是一款付費程式庫; 不過,您亦可在此取得免費試用授權。

如需更多關於 Iron Software 的資訊,請造訪我們的網站: https://ironsoftware.com/ 如需更多支援或有任何疑問,請聯繫我們的團隊

Iron Software 提供支援

如需一般支援或技術諮詢,請透過電子郵件聯絡我們: support@ironsoftware.com/support@ironsoftware.com

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 21,060 | 版本: 2026.6 just released
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronZip
執行範例 觀看您的檔案變成存檔。