跳至頁尾內容
USING IRONZIP

如何在 C# 中提取 Zip 文件

Zip 檔案是一種流行的方法,用於將多個檔案或目錄壓縮到單個 Zip 存檔格式中,提取它們是在許多軟體應用程式中的基本操作。 在 C# 的世界中,使用 IronZIP 命名空間可以輕鬆地處理 Zip 存檔。 本文將探討如何使用 C# 提取 Zip 檔案,並檢視可用的工具和技術。

在檔案系統中,資料的組織和儲存至關重要,無縫解壓檔案的能力成為一項關鍵技能。 有效使用系統來管理指定目錄中的所有檔案對於簡化操作至關重要。 在這種背景中,一個強大的工具是 ZipArchive 類,這是 C# 中的一個強大功能,能夠有效地提取壓縮檔案。 本文將指導您如何利用 ZipArchive 類,闡明如壓縮檔案的本地檔案標頭等基本概念。

先決條件

在您使用 IronZIP 和 IronPDF 操作 ZIP 檔案之前,請確保符合以下前提條件:

  1. Visual Studio:安裝 Visual Studio 或任何您選擇的其他 C# 整合開發環境(IDE)。
  2. 基礎 C# 知識:熟悉 C# 程式設計語言的基本概念。

安裝 IronZIP 套件

要開始使用 IronZIP,請在您的專案中快速安裝 IronZIP NuGet Package。 在 NuGet 套件管理器主控台中執行以下命令:

Install-Package IronZip

或者,直接從 官方 IronZIP NuGet 網站下載套件。

安裝完成後,在開始您的 C# 程式碼時,在頂部新增 using IronZIP 語句。

應用授權金鑰

確保您擁有有效的 IronZIP 授權或試用金鑰。通過將授權金鑰分配給 License 類的 LicenseKey 屬性來應用授權金鑰。 在導入聲明後立即包含以下程式碼,並在使用任何 IronZIP 方法之前:

IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01";
IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01";
IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01"
$vbLabelText   $csharpLabel

這個步驟對於在您的專案中發揮 IronZIP 的全部潛力至關重要。

使用 C# 提取或壓縮 Zip 文件

以下程式碼範例演示了如何在 C# 中處理 Zip 文件,無論您是想壓縮還是提取檔案。

Extract a Zip file using C

以下程式碼範例將使用 IronZIP 將文件解壓到新目錄中。

using IronZIP;

namespace CS_ZipArchive
{
    internal class Program
    {
        public static void Main(string[] args)
        {       
            // Extract the contents of "QRCode.zip" into the "Extracted QRCode" directory
            IronArchive.ExtractArchiveToDirectory("QRCode.zip", "Extracted QRCode");
        }
     }
}
using IronZIP;

namespace CS_ZipArchive
{
    internal class Program
    {
        public static void Main(string[] args)
        {       
            // Extract the contents of "QRCode.zip" into the "Extracted QRCode" directory
            IronArchive.ExtractArchiveToDirectory("QRCode.zip", "Extracted QRCode");
        }
     }
}
Imports IronZIP

Namespace CS_ZipArchive
	Friend Class Program
		Public Shared Sub Main(ByVal args() As String)
			' Extract the contents of "QRCode.zip" into the "Extracted QRCode" directory
			IronArchive.ExtractArchiveToDirectory("QRCode.zip", "Extracted QRCode")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

上述程式碼使用了 IronZIP 程式庫,它提供了在 C# 中處理 ZIP 存檔的功能。 此行旨在提取名為 "QRCode.zip" 的 ZIP 存檔文件的內容,並將其保存到名為 "Extracted QRCode" 的目錄中。 ExtractArchiveToDirectory() 方法負責提取 ZIP 存檔的內容,它接收兩個參數:來源檔案和目的地。

C#-extract-zip-file-tutorial-1

建立存檔檔案

在 C# 中建立 ZIP 文件,我們可以利用 IronZIP 命名空間中的 IronArchive 類。 此類使建立 ZIP 存檔和在其中包含文件變得簡單。 通過使用 IronArchive,開發人員可以輕鬆地在他們的 C# 程式中處理建立 ZIP 檔案的任務,提高效率並簡化文件管理過程。

using IronZIP;

namespace CS_ZipArchive
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new ZIP file named "myPDFFiles.zip"
            using (var archive = new IronArchive("myPDFFiles.zip"))
            {
                // Add files to the ZIP
                archive.Add(@"E:\Files\file1.pdf");
                archive.Add(@"E:\Files\file2.pdf");
                archive.Add(@"D:\Invoices\Invoice.pdf");
            }
        }
    }
}
using IronZIP;

namespace CS_ZipArchive
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new ZIP file named "myPDFFiles.zip"
            using (var archive = new IronArchive("myPDFFiles.zip"))
            {
                // Add files to the ZIP
                archive.Add(@"E:\Files\file1.pdf");
                archive.Add(@"E:\Files\file2.pdf");
                archive.Add(@"D:\Invoices\Invoice.pdf");
            }
        }
    }
}
Imports IronZIP

Namespace CS_ZipArchive
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Create a new ZIP file named "myPDFFiles.zip"
			Using archive = New IronArchive("myPDFFiles.zip")
				' Add files to the ZIP
				archive.Add("E:\Files\file1.pdf")
				archive.Add("E:\Files\file2.pdf")
				archive.Add("D:\Invoices\Invoice.pdf")
			End Using
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

using 語句建立了一個與 IronArchive 類實例關聯的範圍資源。 IronArchive 構造函式以 "myPDFFiles.zip" 作為參數調用,該名稱指定了要建立的新 ZIP 存檔的名稱。 在 using 塊內,三行程式碼將文件新增到新建立的存檔中:

  • archive.Add(@"E:\Files\file1.pdf");
  • archive.Add(@"E:\Files\file2.pdf");
  • archive.Add(@"D:\Invoices\Invoice.pdf");

這些行將指定的 PDF 文件新增到 "myPDFFiles.zip" 存檔。由於 IronArchive 類實現了 IDisposable,使用語句確保在程式碼塊退出時正確關閉存檔並釋放資源。

以這種方式,該程式建立了一個名為 "myPDFFiles.zip" 的 ZIP 存檔,並向其中新增了三個 PDF 文件。 IronZIP 提供了非常高效的方法來建立和提取 zip 文件。

C#-extract-zip-file-tutorial-2

從現有文件系統建立新的 Zip 存檔

我們可以從指定的 zip 文件建立新的 zip 存檔。如下面所示,我們可以新增多個具有不同格式的文件,例如圖片和 PDF 文件。

using IronZIP;

namespace CS_ZipArchive
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new ZIP file named "new PDF Files.zip" by extracting content from "myPDFFiles.zip"
            using (var archive = IronArchive.FromFile("myPDFFiles.zip", "new PDF Files.zip"))
            {
                // Add files to the archive
                archive.Add(@"D:\Invoices\Image1.png");
                archive.Add(@"D:\Invoices\PDF3.pdf");
            }
        }
    }
}
using IronZIP;

namespace CS_ZipArchive
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new ZIP file named "new PDF Files.zip" by extracting content from "myPDFFiles.zip"
            using (var archive = IronArchive.FromFile("myPDFFiles.zip", "new PDF Files.zip"))
            {
                // Add files to the archive
                archive.Add(@"D:\Invoices\Image1.png");
                archive.Add(@"D:\Invoices\PDF3.pdf");
            }
        }
    }
}
Imports IronZIP

Namespace CS_ZipArchive
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Create a new ZIP file named "new PDF Files.zip" by extracting content from "myPDFFiles.zip"
			Using archive = IronArchive.FromFile("myPDFFiles.zip", "new PDF Files.zip")
				' Add files to the archive
				archive.Add("D:\Invoices\Image1.png")
				archive.Add("D:\Invoices\PDF3.pdf")
			End Using
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

此 C# 程式碼片段利用 IronArchive 從現有的 ZIP 文件 "myPDFFiles.zip" 提取內容來建立一個名為 "new PDF Files.zip" 的新 ZIP 文件。在 using 塊內,像 "Image1.png" 和 "PDF3.pdf" 這樣的文件被新增到新 ZIP 存檔中。程式碼利用 IronArchive 高效地從一個存檔中提取並新增特定文件到另一個存檔。

Compressing PDF Files in C

在 C# 中,您可以輕鬆地使用任何第三方庫壓縮 PDF 檔案,其中最有效的工具之一是 IronPDF。 其壓縮算法使您能夠在保持質量的同時縮小 PDF 文件的大小。

介紹 IronPDF

IronPDF 是一個受歡迎的 C# 庫,可以在 .NET Framework應用程式中無縫地處理 PDF 檔案。 除了壓縮功能之外,它還提供多元化的 PDF 生成、操作、轉換等功能。 這種靈活性使其成為執行各種 PDF 相關任務的寶貴工具。 無論是從頭建立 PDF,還是將資料從 HTML 轉換為 PDF,亦或參與其他 PDF 操作,IronPDF 簡化了整個過程,提高了 C# 開發人員的生產力。

安裝 IronPDF NuGet 套件

要將 IronPDF 合併到您的專案中,執行以下命令以安裝 IronPDF。

Install-Package IronZip

此命令簡化了安裝過程,並將必要的依賴項新增到您的專案中,以確保平滑整合。

Write Code to Compress PDF File in C

我們正在專注研究提供的 C# 程式碼片段中的 PDF 壓縮。

using IronPdf;

public static void CompressPdf()
{
    // Open the PDF document located at D:\SamplePDFFile.pdf
    var pdf = new PdfDocument(@"D:\SamplePDFFile.pdf");

    // Compress the images in the PDF document to 60% of their original quality
    pdf.CompressImages(60);

    // Save the compressed PDF as a new file
    pdf.SaveAs(@"D:\CompressedPDF.pdf");
}
using IronPdf;

public static void CompressPdf()
{
    // Open the PDF document located at D:\SamplePDFFile.pdf
    var pdf = new PdfDocument(@"D:\SamplePDFFile.pdf");

    // Compress the images in the PDF document to 60% of their original quality
    pdf.CompressImages(60);

    // Save the compressed PDF as a new file
    pdf.SaveAs(@"D:\CompressedPDF.pdf");
}
Imports IronPdf

Public Shared Sub CompressPdf()
	' Open the PDF document located at D:\SamplePDFFile.pdf
	Dim pdf = New PdfDocument("D:\SamplePDFFile.pdf")

	' Compress the images in the PDF document to 60% of their original quality
	pdf.CompressImages(60)

	' Save the compressed PDF as a new file
	pdf.SaveAs("D:\CompressedPDF.pdf")
End Sub
$vbLabelText   $csharpLabel

在上述 C# 程式碼中,我們打開了一個名為 "SamplePDFFile.pdf" 的 PDF 文件; 其圖像被壓縮到原始質量的 60%。 然後將結果壓縮的 PDF 文件保存為 "CompressedPDF.pdf" 到指定的文件夾位置。

C#-extract-zip-file-tutorial-3

此外,您可以使用 System 命名空間 (using System),因為它為 C# 應用程式中的基本功能提供了必要的類和方法。 使用 ZipArchive 類,作為 System.IO.Compression 命名空間的一部分,您可以處理壓縮文件,因為它允許無縫提取和操作壓縮文件,確保有效處理壓縮資料。 在這個 PDF 壓縮範例的背景下,理解和利用 System 命名空間和 IronPDF 程式庫展示了 C# 在處理多種文件格式(如壓縮檔案、gz 檔案或 PDF 檔案)方面的多樣性和強大能力。

結論

總之,透過 IronZIPIronPDF 程式庫的強大功能,導航 C# 中的 Zip 文件操作和 PDF 壓縮成為無縫過程。 本文提供了如何從 zip 存檔中提取文件、建立新存檔以及壓縮 PDF 文件的見解,展現了這些程式庫在 C# 開發中的多樣性和效率。 通過遵循所述程式並合併 IronZIP 和 IronPDF 套件,開發人員可以透過流式文件管理、動態 Zip 存檔建立和有效的 PDF 壓縮來提升他們的應用程式。 這些程式庫作為有價值的資產,賦予開發人員輕鬆高效地處理複雜任務的能力,最終提高 C# 應用程式在文件處理和壓縮世界中的整體功能。 Iron Software 提供的30 天試用為探索其功能提供了無風險的機會,使其易於確定其對特定專案的適用性。 在探索 IronZIP 和 IronPDF 的所有功能後,您可以購買授權

常見問題

如何使用 C# 提取 ZIP 檔案?

您可以使用 C# 中的 IronZIP 程式庫提取 ZIP 檔案。利用 ExtractArchiveToDirectory() 方法指定來源 ZIP 檔案及解壓縮的目標目錄。

使用 C# 處理 IronZIP 需要什麼條件?

要使用 IronZIP,您需要先安裝 Visual Studio 或其他 C# IDE,具備 C# 程式設計的基本知識,以及透過 NuGet 使用指令 Install-Package IronZip 安裝 IronZIP 程式庫。

如何在 C# 中建立一個 ZIP 歸檔?

在 C# 中,您可以使用 IronZIP 程式庫中的 IronArchive 類別來建立 ZIP 歸檔。使用 Add() 方法可將文件加入歸檔中,並按需指定歸檔名稱。

如何在 C# 專案中管理 ZIP 檔案操作授權?

在使用 IronZIP 的 C# 專案中管理授權時,請在匯入 IronZIP 後及使用任何其方法之前,將您的授權金鑰指派給 'License' 類別中的 LicenseKey 屬性。

我可以使用 ZIP 程式庫壓縮 PDF 文件嗎?

雖然 ZIP 程式庫專注於文件壓縮,但對於專門的 PDF 壓縮和操作,您應使用 IronPDF,該工具提供了處理 C# 中 PDF 文件的全面功能。

使用 IronZIP 管理 ZIP 文件有什麼優勢?

IronZIP 在 C# 中簡化了 ZIP 文件管理,提供了簡單的方法進行 ZIP 歸檔的提取和建立。它提高了軟體應用中的文件操作效率和組織性。

如何在 C# 中將多個文件壓縮到一個 ZIP 歸檔?

要在 C# 中將多個文件壓縮到一個 ZIP 歸檔,您可以使用 IronZIP 程式庫的 IronArchive 類別。它提供了新增多個文件和目錄到單一壓縮歸檔的功能。

IronPDF 在 C# 中提供了什麼用於 PDF 操作的功能?

IronPDF 提供了廣泛的功能,包括 PDF 生成、轉換、操作和壓縮,讓開發者能有效地在 C# 應用程式中處理 PDF 文件。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話