使用 IRONZIP 如何在 C# 中將 Zip 存檔提取到目錄 Curtis Chau 更新日期:6月 22, 2025 Download IronZIP NuGet 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article ZIP files serve as a convenient way to bundle multiple files and directories into a single archive. Whether you’re managing backups, distributing software, or sharing files, efficient extraction from ZIP archives is crucial. IronZIP, a powerful .NET library, simplifies this process by providing simple methods. How to Zip Archive Extract To Directory in C# Install the IronZIP library in your project. Use the ExtractArchiveToDirectory() method provided by IronZIP. Specify the path to your ZIP file (e.g., "output.zip") and the target extraction directory (e.g., "extracted"). What is IronZIP? IronZIP is a versatile C# library that handles ZIP files within .NET applications. Whether you’re building desktop software, web applications, or automation tools, IronZIP simplifies creating, reading, and extracting ZIP archives. Key Features High Performance: IronZIP has a powerful engine for compressing and decompressing files, making things fast and using fewer resources. Ease of Use: Developers can integrate IronZIP seamlessly into their projects. The library provides simple methods for working with ZIP files. NuGet Integration: Install IronZIP via the NuGet Package Manager, making it accessible to your C# ziparchive extract to directory solution. Cross-Platform Compatibility: IronZIP supports various platforms, including Windows, Linux, and macOS. Documentation and Tutorials: Explore the official documentation and tutorials to learn how to leverage IronZIP effectively. IronZIP empowers developers with a reliable and efficient solution whether you’re zipping up files for distribution, managing backups, or handling data archives. Extracting a ZIP Archive to a Directory Before we start, we need to install the IronZIP Library in our Project. We can add it by using NuGet Package Manager Console with the following command: Install-Package IronZip The above command will install the IronZIP Library along with the required dependencies in our project. Alternatively, you may also download it from NuGet Package Manager for Solution by browsing "IronZIP". Include the necessary using directives in your code: using IronZip; using IronZip.Enum; using IronZip; using IronZip.Enum; Imports IronZip Imports IronZip.Enum $vbLabelText $csharpLabel Extract the ZIP File to the Specified Directory IronZIP simplifies the process of extracting a zip archive to a directory. We can extract files using just one line of code. The following code will extract all the files in the provided path. internal class Program { static void Main(string[] args) { IronZipArchive.ExtractArchiveToDirectory("Contracts.zip", "Contracts"); } } internal class Program { static void Main(string[] args) { IronZipArchive.ExtractArchiveToDirectory("Contracts.zip", "Contracts"); } } Friend Class Program Shared Sub Main(ByVal args() As String) IronZipArchive.ExtractArchiveToDirectory("Contracts.zip", "Contracts") End Sub End Class $vbLabelText $csharpLabel The above line of code extracts the contents of the “Contracts.zip” file and places them in a destination folder named “Contracts.” It’s a convenient way to handle ZIP archives programmatically in C# applications. Explanation IronZipArchive: This is a class from the IronZIP library. It provides methods and properties for working with ZIP files. .ExtractArchiveToDirectory("Contracts.zip", "Contracts"): The method ExtractArchiveToDirectory is called on IronZipArchive. The first argument, "Contracts.zip", represents the path to the ZIP file you want to extract. The second argument, "Contracts", specifies the target directory where the extracted files will be placed. If the files already exist in a specified directory, it will overwrite existing files. The extracted file is as follows: Extract Password-Protected ZIP Archive IronZIP also provides a method to extract password-protected compressed files. The following example will extract the files from a password-protected ZIP file. IronZipArchive.ExtractArchiveToDirectory("PasswordProtectedContracts.zip", "Contracts", "Pa55w0r2Zip"); IronZipArchive.ExtractArchiveToDirectory("PasswordProtectedContracts.zip", "Contracts", "Pa55w0r2Zip"); IronZipArchive.ExtractArchiveToDirectory("PasswordProtectedContracts.zip", "Contracts", "Pa55w0r2Zip") $vbLabelText $csharpLabel The above code extracts the contents of a password-protected ZIP file named “PasswordProtectedContracts.zip” and places them in a directory called “Contracts.” The specified password ensures secure extraction of the protected ZIP file. Access Existing Archive IronZIP allows access to existing ZIP files without extracting them from a specified directory. This functionality is important for scenarios such as extracting files from compressed archives, validating the existence of specific entries, or even dynamically inspecting the contents of ZIP files in response to user interactions. View Entries of the ZIP Archive The following code example will access the existing ZIP file and print its entries to the console. using (var zipArchive = new IronZipArchive("Contracts.zip")) { // Get Entries list List<string> names = zipArchive.GetArchiveEntryNames(); foreach (string name in names) { Console.WriteLine(name); // Print the name of each entry } } using (var zipArchive = new IronZipArchive("Contracts.zip")) { // Get Entries list List<string> names = zipArchive.GetArchiveEntryNames(); foreach (string name in names) { Console.WriteLine(name); // Print the name of each entry } } Using zipArchive = New IronZipArchive("Contracts.zip") ' Get Entries list Dim names As List(Of String) = zipArchive.GetArchiveEntryNames() For Each name As String In names Console.WriteLine(name) ' Print the name of each entry Next name End Using $vbLabelText $csharpLabel The above code uses IronZIP to check out what's inside a ZIP file named "Contracts.zip." It creates an IronZipArchive object to grab a list of entry names (like file names) from the archive using the GetArchiveEntryNames method. If the ZIP file is password-protected, you can include the password when making the IronZipArchive object. The code then prints out each file name to the console. This way, we can work with specified entries within a compressed archive. The output is as follows: Check if Specified Entry Exists in ZIP File We can also check if the specified entry exists in the specified ZIP file. The following code shows the demonstration. using (var zipArchive = new IronZipArchive("Contracts.zip")) { if (zipArchive.CheckArchiveEntryExist("Contract1.pdf")) { Console.WriteLine("File Exist"); } } using (var zipArchive = new IronZipArchive("Contracts.zip")) { if (zipArchive.CheckArchiveEntryExist("Contract1.pdf")) { Console.WriteLine("File Exist"); } } Using zipArchive = New IronZipArchive("Contracts.zip") If zipArchive.CheckArchiveEntryExist("Contract1.pdf") Then Console.WriteLine("File Exist") End If End Using $vbLabelText $csharpLabel The above code uses the IronZIP library to check if a file named "Contracts.zip" contains an entry named "Contract1.pdf." It does this by creating an IronZipArchive object for the ZIP file. Inside the code block, there's an "if" statement checking if "Contract1.pdf" exists in the ZIP file. If it does, it prints "File Exist" to the console. In simpler terms, this code helps you confirm if a specific file is inside a ZIP file, and if it is, it lets you know. Delete Entry from Existing ZIP File IronZIP provides a method to delete entries from a ZIP file. The following code will delete a file from an existing ZIP. using (var zipArchive = new IronZipArchive("Contracts.zip")) { zipArchive.DeleteArchiveEntry("Contract1.pdf"); } using (var zipArchive = new IronZipArchive("Contracts.zip")) { zipArchive.DeleteArchiveEntry("Contract1.pdf"); } Using zipArchive = New IronZipArchive("Contracts.zip") zipArchive.DeleteArchiveEntry("Contract1.pdf") End Using $vbLabelText $csharpLabel The above code demonstrates how to use IronZIP to delete an entry from a ZIP archive named “Contracts.zip.” Within the using block, the specified file named “Contract1.pdf” is removed from the archive. This operation ensures efficient management of ZIP files in C# applications. Conclusion In conclusion, IronZIP emerges as a valuable tool for C# developers, simplifying the handling of ZIP files in various scenarios whether it's extracting files for business use. 常見問題解答 如何在 C# 中將 ZIP 檔案解壓縮到目錄? 您可以使用 IronZIP 庫的 ExtractArchiveToDirectory() 方法將 ZIP 檔案解壓縮到特定目錄。 只需指定您的 ZIP 檔案路徑和目標解壓縮目錄。 我可以使用 C# 庫提取受密碼保護的 ZIP 檔案嗎? 是的, 使用 IronZIP, 您可以通過將密碼作為附加參數提供給 ExtractArchiveToDirectory() 方法, 來提取受密碼保護的 ZIP 檔案。 如何在 C# 專案中安裝 ZIP 檔案管理的庫? 您可以使用命令 Install-Package IronZIP 通過 NuGet 套件管理器控制台安裝 IronZIP 庫或在 NuGet 套件管理器中瀏覽 'IronZIP'。 我可以使用哪些方法來查看 ZIP 條目而不提取它們? IronZIP 允許您通過使用 GetArchiveEntryNames() 方法查看 ZIP 檔案的條目, 這會列出檔案中的所有條目。 使用 C# 可以檢查 ZIP 檔案中的特定檔案嗎? 是的, 您可以使用 IronZIP 的 CheckArchiveEntryExist() 方法檢查 ZIP 檔案中是否存在特定條目。 如何在 C# 中從 ZIP 檔案中刪除檔案? 要使用 IronZIP 從 ZIP 檔案中刪除檔案, 請對指定檔案的 IronZipArchive 對象使用 DeleteArchiveEntry() 方法。 哪些平台支持使用此 C# 庫進行 ZIP 檔案管理? IronZIP 在多個平台上支持 ZIP 檔案管理, 包括 Windows、Linux 和 macOS, 確保跨平台兼容性。 使用 IronZIP 庫進行 ZIP 檔案管理有什麼好處? IronZIP 提供高性能、易用性、跨平台兼容性和無縫的 NuGet 集成, 提供面向開發者的完整文檔和教程。 我在哪裡可以找到用於管理 C# 中 ZIP 檔案的教程? IronZIP 的官方文檔和教程可以在其網站上找到, 提供有關使用庫進行 ZIP 檔案管理的詳細指引。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 7月 28, 2025 如何在 C# 中使用密碼壓縮文件 在本文中,我們將探討如何使用 C# 和 IronZIP 庫創建受密碼保護的 ZIP 文件 閱讀更多 更新日期 7月 28, 2025 如何在 C# 中將文件解壓到目錄 無論你在開發Windows 應用程序還是 .NET 項目,了解解壓文件的過程都是非常有價值的 閱讀更多 更新日期 7月 28, 2025 如何使用 C# 壓縮文件夾中的文件 ZIP 文件是使用 ZIP 格式包含一個或多個壓縮文件或文件夾的文件。這是一種常見的壓縮和封存多個文件或文件夾到單一文件的方法。 閱讀更多 如何使用 C# 壓縮文件夾中...
更新日期 7月 28, 2025 如何使用 C# 壓縮文件夾中的文件 ZIP 文件是使用 ZIP 格式包含一個或多個壓縮文件或文件夾的文件。這是一種常見的壓縮和封存多個文件或文件夾到單一文件的方法。 閱讀更多