使用 IRONZIP 如何在 C# 中將文件解壓到目錄 Curtis Chau 更新日期:7月 28, 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 Unzipping files programmatically is a common task in software development, especially when dealing with compressed archives. In this article, we’ll explore how to extract files from ZIP archives in C# using the IronZIP library. Whether you’re working on a Windows application or a .NET project, understanding the process of unzipping files can be invaluable. Let’s dive in and learn how to efficiently handle ZIP files within your C# codebase. What this Article will Explain: The installation process of the IronZIP Library How to extract a ZIP archive How to extract a password-protected ZIP archive How to create a ZIP file How to create password-protected ZIP files Why Would You Unzip Files? ZIP files, also known as ZIP archives, are bundles of multiple files into a single compressed container. Unzipping these files involves extracting the compressed data and restoring it to its original state. This process is essential when you need to access or manipulate data within a C# application. IronZIP streamlines this task by providing simple methods for managing compressed data. What is IronZIP? IronZIP is a powerful C# ZIP archive library that facilitates creating, reading, and extracting archives in .NET applications. Whether you need to compress files, extract data from existing archives, or manage your ZIP files, IronZIP provides a user-friendly API for achieving these tasks efficiently. IronZIP simplifies working with ZIP archives in your .NET applications. It supports various archive formats, including ZIP, TAR, GZIP, and BZIP2. IronZIP is cross-platform, compatible with .NET Core, .NET Standard, and .NET Framework. And it offers advanced features like password-protected archives, customizable compression levels, and file entry management. How to Extract Files to a Directory in C# Now, I will discuss some examples for extracting from ZIP files in C#. Start by creating a new C# project in Visual Studio. Choose the appropriate project type (e.g., Console Application, Windows Forms, ASP.NET Core, etc.). Next, we need to install the IronZIP NuGet Package. Installing IronZIP via the NuGet Package Manager To install the IronZIP NuGet package, you can use the following command in the Package Manager Console: Install-Package IronZip Alternatively, you can use the Package Manager UI in Visual Studio to search for and install the IronZIP package. In your C# code, import the IronZIP namespace to access its classes and methods. using IronZip; using IronZip; Imports IronZip $vbLabelText $csharpLabel Code Example to Extract ZIP files in C# The following code will unzip files to the specified directory in C#. // This line extracts the "Images.zip" archive to the "Extracted Images" directory IronZipArchive.ExtractArchiveToDirectory(@"E:\Images.zip", "Extracted Images"); // This line extracts the "Images.zip" archive to the "Extracted Images" directory IronZipArchive.ExtractArchiveToDirectory(@"E:\Images.zip", "Extracted Images"); ' This line extracts the "Images.zip" archive to the "Extracted Images" directory IronZipArchive.ExtractArchiveToDirectory("E:\Images.zip", "Extracted Images") $vbLabelText $csharpLabel The above code reads the specified ZIP file ("E:\Images.zip"), decompresses its contents, and places the extracted files into the directory named "Extracted Images". Explanation IronZipArchive refers to the class provided by the IronZIP library. It’s the main entry point for working with ZIP archives in your C# code. ExtractArchiveToDirectory(...) is a method (or function) that the IronZipArchive class provides. It allows you to extract the contents of a ZIP archive (in this case, the file "E:\Images.zip") to a specified directory (in this case, the directory named "Extracted Images"). The method takes two parameters: The first parameter (@"E:\Images.zip") is the path to the ZIP file you want to extract. The second parameter ("Extracted Images") is the directory where you want to extract the files from the ZIP archive. Output: How to Extract a Password-Protected ZIP File IronZIP provides a simple method to extract encrypted ZIP files. The following code demonstrates how to extract all files from a password-protected ZIP file. // This line extracts the "EncryptedImages.zip" archive to the "ExtractedImages" directory with the given password IronZipArchive.ExtractArchiveToDirectory(@"EncryptedImages.zip", "ExtractedImages", "zipP@55w0rd"); // This line extracts the "EncryptedImages.zip" archive to the "ExtractedImages" directory with the given password IronZipArchive.ExtractArchiveToDirectory(@"EncryptedImages.zip", "ExtractedImages", "zipP@55w0rd"); ' This line extracts the "EncryptedImages.zip" archive to the "ExtractedImages" directory with the given password IronZipArchive.ExtractArchiveToDirectory("EncryptedImages.zip", "ExtractedImages", "zipP@55w0rd") $vbLabelText $csharpLabel The above code extracts the contents of the "EncryptedImages.zip" archive to the "ExtractedImages" directory using IronZIP. It also specifies the password "zipP@55w0rd" for decryption, indicating that the ZIP file is encrypted and requires the provided password for extraction. It will overwrite existing files. Output: In C# with IronZIP, unzipping a file to a directory involves leveraging the IronZIP library's capabilities to their fullest. By employing functions like IronZipArchive.ExtractArchiveToDirectory, developers can easily extract compressed files, providing a straightforward mechanism for handling the local file header. It provides an efficient way to update zip files and RAR files in their applications. IronZIP also provides methods to create ZIP files in C# and .NET applications. Let's create a ZIP archive in C#. How to Create a ZIP Archive The following code example will create a ZIP archive. static void Main(string[] args) { // Create a new ZIP archive with a high compression level (9) using (var archive = new IronZipArchive(9)) { // Add image files to the archive archive.Add(@"E:\Images\image1.png"); archive.Add(@"E:\Images\image2.png"); archive.Add(@"E:\Images\image3.png"); archive.Add(@"E:\Images\image4.png"); // Export the ZIP archive to the file "MyImages.zip" archive.SaveAs("MyImages.zip"); } } static void Main(string[] args) { // Create a new ZIP archive with a high compression level (9) using (var archive = new IronZipArchive(9)) { // Add image files to the archive archive.Add(@"E:\Images\image1.png"); archive.Add(@"E:\Images\image2.png"); archive.Add(@"E:\Images\image3.png"); archive.Add(@"E:\Images\image4.png"); // Export the ZIP archive to the file "MyImages.zip" archive.SaveAs("MyImages.zip"); } } Shared Sub Main(ByVal args() As String) ' Create a new ZIP archive with a high compression level (9) Using archive = New IronZipArchive(9) ' Add image files to the archive archive.Add("E:\Images\image1.png") archive.Add("E:\Images\image2.png") archive.Add("E:\Images\image3.png") archive.Add("E:\Images\image4.png") ' Export the ZIP archive to the file "MyImages.zip" archive.SaveAs("MyImages.zip") End Using End Sub $vbLabelText $csharpLabel The above code demonstrates the usage of IronZIP to create a new ZIP archive (with compression level 9) and add multiple entries, each representing an image file (image1.png, image2.png, image3.png, and image4.png) from the specified file paths. Finally, the SaveAs method is used to export the created ZIP archive as "MyImages.zip" in the specified directory. The compression level 9 indicates maximum compression, resulting in smaller file sizes but potentially longer processing times. How to Create a Password-Protected ZIP Archive The following code will create a password-protected ZIP file. // Create a new ZIP archive with a high compression level (9) using (var archive = new IronZipArchive(9)) { // Password protect the ZIP archive (Support AES128 & AES256) archive.Encrypt("miPassw0rd", EncryptionMethods.AES256); // Add image files to the archive archive.Add(@"E:\Images\image1.png"); archive.Add(@"E:\Images\image2.png"); // Export the ZIP archive as "PasswordProtectedImages.zip" archive.SaveAs("PasswordProtectedImages.zip"); } // Create a new ZIP archive with a high compression level (9) using (var archive = new IronZipArchive(9)) { // Password protect the ZIP archive (Support AES128 & AES256) archive.Encrypt("miPassw0rd", EncryptionMethods.AES256); // Add image files to the archive archive.Add(@"E:\Images\image1.png"); archive.Add(@"E:\Images\image2.png"); // Export the ZIP archive as "PasswordProtectedImages.zip" archive.SaveAs("PasswordProtectedImages.zip"); } ' Create a new ZIP archive with a high compression level (9) Using archive = New IronZipArchive(9) ' Password protect the ZIP archive (Support AES128 & AES256) archive.Encrypt("miPassw0rd", EncryptionMethods.AES256) ' Add image files to the archive archive.Add("E:\Images\image1.png") archive.Add("E:\Images\image2.png") ' Export the ZIP archive as "PasswordProtectedImages.zip" archive.SaveAs("PasswordProtectedImages.zip") End Using $vbLabelText $csharpLabel The above code demonstrates the use of IronZIP to create a password-protected ZIP archive with a specified encryption method (AES256 in this case). The password "miPassw0rd" is set to secure the ZIP file. The code then adds two image files (image1.png and image2.png) from the specified file paths to the archive. Finally, the SaveAs method is utilized to export the password-protected ZIP archive as "PasswordProtectedImages.zip." This ensures the security of the contents with the specified password and encryption method. IronZIP supports AES128, AES256, and traditional methods. Conclusion In summary, this article has covered the essential aspects of programmatically handling compressed files in C# using the IronZIP library. From the installation process to practical examples of extracting, creating, and securing ZIP archives, developers have been provided with valuable insights. IronZIP's user-friendly API, cross-platform compatibility, and support for encryption methods make it a versatile solution for efficiently managing ZIP files in various C# applications. Whether updating existing files or working with password-protected archives, IronZIP streamlines the process, offering a robust toolset for developers seeking effective file manipulation capabilities in their C# projects. IronZIP offers flexible licensing options, including a free trial for long-term usage. Developers can choose the licensing model that best fits their project requirements, ensuring both versatility and convenience in implementing IronZIP in their C# applications. 常見問題解答 如何在 C# 中從 ZIP 壓縮包中提取檔案? 您可以使用 IronZIP 在 C# 中從 ZIP 壓縮包中提取檔案。`IronZipArchive.ExtractArchiveToDirectory` 方法允許您指定 ZIP 文件的路徑和目標提取目錄。 在 .NET 項目中安裝 IronZIP 的步驟是什麼? 要在 .NET 項目中安裝 IronZIP,您可以在 Visual Studio 中使用 NuGet 套件管理器。在套件管理器控制台中運行命令 `Install-Package IronZIP` 或在 NuGet 套件管理器 UI 中搜尋 IronZIP 並添加到您的項目中。 我可以使用 IronZIP 提取受密碼保護的 ZIP 檔案嗎? 可以,IronZIP 支持提取受密碼保護的 ZIP 檔案。您需要在 `ExtractArchiveToDirectory` 方法中提供密碼作為參數以訪問和提取檔案。 如何在 C# 中以程式化方式創建 ZIP 文件? 您可以在 C# 中使用 IronZIP 創建 ZIP 文件,通過實例化新的 `IronZipArchive`,使用 `Add` 方法添加檔案,並使用 `SaveAs` 方法保存壓縮包。 IronZIP 支持多種壓縮格式嗎? 是的,IronZIP 支持多種壓縮格式,包括 ZIP、TAR、GZIP 和 BZIP2,允許在 C# 應用程序中靈活處理不同的壓縮文件類型。 IronZIP 是否兼容跨平台的 .NET 應用程序? IronZIP 兼容跨平台的 .NET 應用,因為它支持 .NET Core、.NET Standard 和 .NET Framework,適用於各種開發環境。 IronZIP 提供哪些加密方法? IronZIP 提供用於創建受密碼保護 ZIP 壓縮包的加密方法。您可以使用 `Encrypt` 方法設置密碼來保護壓縮包。 如何在購買前評估 IronZIP? IronZIP 提供免費試用版及靈活的授權選項,允許開發者在購買前評估庫的功能和性能。 IronZIP 為在 C# 中處理 ZIP 文件提供了哪些好處? IronZIP 提供了用戶友好的 API,支持多種壓縮格式和加密方法,簡化了創建、讀取和提取 ZIP 壓縮包的過程,對於 C# 開發者而言是一個高效的工具。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 6月 22, 2025 如何在 C# 中將 Zip 存檔提取到目錄 ZIP 文件是一種將多個文件和目錄打包成單個存檔的方便方式。 閱讀更多 更新日期 7月 28, 2025 如何在 C# 中使用密碼壓縮文件 在本文中,我們將探討如何使用 C# 和 IronZIP 庫創建受密碼保護的 ZIP 文件 閱讀更多 更新日期 7月 28, 2025 如何使用 C# 壓縮文件夾中的文件 ZIP 文件是使用 ZIP 格式包含一個或多個壓縮文件或文件夾的文件。這是一種常見的壓縮和封存多個文件或文件夾到單一文件的方法。 閱讀更多 如何在 C# 中使用密碼壓縮文件如何在 C# 中從多個文件創...
更新日期 7月 28, 2025 如何使用 C# 壓縮文件夾中的文件 ZIP 文件是使用 ZIP 格式包含一個或多個壓縮文件或文件夾的文件。這是一種常見的壓縮和封存多個文件或文件夾到單一文件的方法。 閱讀更多