跳過到頁腳內容
使用 IRONZIP

如何在 C# 中萃取 Zip 檔案

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 套件。 在 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 文件,無論您是想壓縮文件還是提取文件。

使用 C# 解壓縮 Zip 文件

以下程式碼範例將使用 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 壓縮包的內容。它接受兩個參數:原始檔和目標檔。

! csharp-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 ,因此 using 語句可以確保正確關閉壓縮包,並在程式碼區塊退出時釋放資源。

這樣,程式建立了一個名為"myPDFFiles.zip"的 ZIP 壓縮文件,並在其中新增了三個 PDF 文件。 IronZip 提供了建立和提取 zip 檔案非常有效率的方法。

! csharp-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 從名為"myPDFFiles.zip"的現有 ZIP 檔案中提取內容,並建立一個名為"new PDF Files.zip"的新 ZIP 檔案。在 using 程式碼區塊中,諸如"Image1.png"和"PDF3.pdf"之類的檔案被加入到新的 ZIP 壓縮包中。該程式碼使用 IronArchive 有效地將特定檔案從一個壓縮包中提取並添加到另一個壓縮包中。

C# 中的 PDF 檔案壓縮

在 C# 中,您可以使用任何第三方程式庫輕鬆壓縮 PDF 文件,而IronPDF是完成此任務最有效的工具之一。 它的壓縮演算法能夠縮小 PDF 文件的大小,同時保持其品質。

IronPDF簡介

IronPDF是一個流行的 C# 庫,它使開發人員能夠在 .NET 框架應用程式中無縫處理 PDF 文件。 除了壓縮功能外,它還提供多種 PDF 生成、編輯、轉換等功能。 這種靈活性使其成為處理各種 PDF 相關任務的寶貴工具。 無論是從頭開始建立 PDF、將資料從 HTML 轉換為 PDF,或是執行其他 PDF 操作,IronPDF 都能簡化整個流程,提高 C# 開發人員的生產力。

安裝 IronPDF NuGet 套件。

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

Install-Package IronZip

此命令簡化了安裝過程,並為您的專案添加了必要的依賴項,從而確保順利整合。

用 C# 編寫程式碼壓縮 PDF 文件

我們專注於提供的 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"的名義儲存到指定的資料夾位置。

! csharp-extract-zip-file-tutorial-3

此外,您可以使用System命名空間( using System ),因為它為 C# 應用程式的基本功能提供了必要的類別和方法。 使用System.IO.Compression命名空間中的 ZipArchive 類,您可以處理壓縮文件,因為它允許無縫提取和操作壓縮文件,從而確保高效處理壓縮資料。 在本 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 檔案操作授權?

若要在 C# 專案中使用 IronZIP 管理授權,請在匯入 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 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。