使用 IRONZIP .NET ZipArchive (開發者教程) 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 In the ever-growing field of software development, efficient file management is of utmost importance while sending and receiving files in large numbers. The need to compress, bundle, and extract large files is a common requirement, and ZIP archives serve as a versatile solution. Microsoft .NET simplifies working with ZipArchive files through its dedicated class. For a comprehensive guide on utilizing this functionality, refer to the official Microsoft documentation on the ZipArchive Class. In this article, we will take you beyond the .NET ecosystem and explore how IronZIP, as a robust C# ZIP archive library, simplifies archive management and provides developers with a user-friendly API for creating, reading, and extracting archives seamlessly. Quick Overview on Managing a ZipArchive File in C# Create a C# Console Application in Visual Studio. Install the ZIP Library from NuGet Package Manager. Create an Empty Zip File using IronArchive. Add Files to Zip Archive using the Add method. Extract Files from ZipArchive using the ExtractArchiveToDirectory method. Introducing IronZIP IronZIP stands out as a leading C# ZIP archive library designed to meet the demands of .NET developers. Prioritizing accuracy, ease of use, and speed, IronZIP offers a comprehensive set of features that make it a go-to solution for archive-related tasks. Whether you're working on desktop, mobile, or cloud applications, IronZIP's cross-platform support ensures consistent performance across various environments. Key Features of IronZIP: Cross-Platform Support: IronZIP seamlessly runs on Windows, Linux, Mac, iOS, Android, Docker, Azure, and AWS, catering to diverse development environments. .NET Compatibility: Fully supporting .NET 7, 6, Core, Standard, and Framework, IronZIP adapts to the latest technologies in the .NET ecosystem. File Format Support: IronZIP handles ZIP, TAR, GZIP, and BZIP2 archives, offering versatility in dealing with different archive formats. Versatility: Designed for C#, F#, and VB.NET, IronZIP supports various project types, including web, mobile, desktop, and console applications. Easy Integration: IronZIP effortlessly integrates into popular IDEs such as Microsoft Visual Studio and JetBrains ReSharper & Rider, enhancing the developer experience. Compression Level: IronZIP provides a compression level feature that can be set from scale 0 to 9 to compress files and generate storage-efficient Zip files. Prerequisites Before moving on to the implementation, ensure the following prerequisites are in place: Visual Studio: Install Visual Studio, the comprehensive integrated development environment for building .NET C# applications. If not installed, you can download it from its official website. IronZIP Package: Use NuGet Package Manager to install the IronZIP library for your project. Steps to Create a ZipArchive File System in .NET Step 1: Create a C# Console Project in Visual Studio Open Visual Studio and create a new C# console project. Configure the project name and location. Choose the appropriate .NET version based on your project requirements. IronZIP supports the latest version and older versions of .NET and .NET Core, so you can choose any version from the available list. Step 2: Installing IronZIP using NuGet Package Manager Integrating IronZIP into the project is a very straightforward process. You can install the IronZIP library as follows: Open the Manage NuGet Packages for Solution by right-clicking the Solution Explorer. In the NuGet browse tab, search for IronZIP and click install. Alternatively, you can use the NuGet Package Manager Console and run the command: Install-Package IronZip That's it! We have set up everything, and we can proceed to create Zip files in our application. Step 3: Creating a .NET ZipArchive File Stream 1. Import IronZIP Namespace First, we need to include the IronZIP namespace in our source code main file by using the statement: using IronZIP; using IronZIP; Imports IronZIP $vbLabelText $csharpLabel 2. Create a Zip File Creating a zip file becomes remarkably easy with IronZIP, thanks to its user-friendly API and straightforward syntax. Follow these steps to create a C# zip archive file system. Code Example: class Program { public static void Main(string[] args) { var file = "output.zip"; // Create an empty ZIP archive using (var archive = new IronArchive(file)) { // Add files to the ZIP archive.Add("./assets/image1.jpg"); archive.Add("./assets/image2.jpg"); archive.Add("./assets/image3.jpg"); } Console.WriteLine("Zip file generated successfully!"); } } class Program { public static void Main(string[] args) { var file = "output.zip"; // Create an empty ZIP archive using (var archive = new IronArchive(file)) { // Add files to the ZIP archive.Add("./assets/image1.jpg"); archive.Add("./assets/image2.jpg"); archive.Add("./assets/image3.jpg"); } Console.WriteLine("Zip file generated successfully!"); } } Friend Class Program Public Shared Sub Main(ByVal args() As String) Dim file = "output.zip" ' Create an empty ZIP archive Using archive = New IronArchive(file) ' Add files to the ZIP archive.Add("./assets/image1.jpg") archive.Add("./assets/image2.jpg") archive.Add("./assets/image3.jpg") End Using Console.WriteLine("Zip file generated successfully!") End Sub End Class $vbLabelText $csharpLabel Explanation: The using statement ensures proper management of the IronArchive object, releasing resources when the block is exited. The IronArchive constructor takes in a string parameter, which represents the path and name of the ZIP file to be created. The Add method is employed to include three image files as entries in the zip archive. Adjust file paths based on your file locations. 3. Extract Zip File Content Extracting the archive contents from zip files with an application is another handy feature to have. IronZIP with its one-line code helps to extract data from the ZipArchive archive file system effortlessly. Write the sample code to achieve this task: using IronZIP; // Extract ZIP IronArchive.ExtractArchiveToDirectory("output.zip", "extracted"); using IronZIP; // Extract ZIP IronArchive.ExtractArchiveToDirectory("output.zip", "extracted"); Imports IronZIP ' Extract ZIP IronArchive.ExtractArchiveToDirectory("output.zip", "extracted") $vbLabelText $csharpLabel That's it! Zip file extracted. IronZIP also offers aid with adding files to the existing file system of ZipArchive. For more detailed information, please visit this tutorial on create, read, and extract Zip on the IronZIP website. Step 4: Execute the Program to Get the New ZipArchive Build and run the Zip archive application. After successful execution, check your project directory with the specified entry name in the program for the resulting Zip file: "output.zip" containing the specified image files. Here is the output Zip file content-disposition: Conclusion In conclusion, IronZIP emerges as a powerful and versatile solution for handling zip archives in C#. Its cross-platform support, compatibility with various .NET versions, and straightforward integration make it a valuable tool for developers. IronZIP simplifies the creation, reading, and extraction of zip archives in your C# projects and enhances the efficiency of file management processes. To learn more about IronZIP and its capabilities, please visit this documentation page. IronZIP is a commercial product, and for use in commercial projects, a license is required. The lite package starts from $799, and detailed licensing information is available on the IronZIP license page. A trial version of IronZIP is also available, allowing developers to explore its features before making a purchase decision. Download the IronZIP library from here. 常見問題解答 如何在 C# 應用程式中創建 ZIP 文件? 要在 C# 應用程式中使用 IronZIP 創建 ZIP 文件,可以使用 IronArchive 類創建一個空的 ZIP 檔案,並使用 Add 方法來添加文件,如下所示:using (var archive = new IronArchive('output.zip')) { archive.Add('path/to/file'); } 在 C# 中從 ZIP 檔案中提取文件的過程是什麼? 您可以使用 IronZIP 中的 ExtractArchiveToDirectory 方法從 ZIP 檔案中提取文件。例如:IronArchive.ExtractArchiveToDirectory('output.zip', 'destination/directory'); C# ZIP 庫支持哪些文件格式? IronZIP 支持多种文件格式,包括 ZIP、TAR、GZIP 和 BZIP2,提供靈活的不同檔案處理選擇。 IronZIP 可以在不同的操作系統上使用嗎? 是的,IronZIP 提供跨平台支持,可以在各種操作系統上使用,如 Windows、Linux、Mac,以及 iOS、Android、Docker、Azure 和 AWS 等環境。 如何在 C# 中創建 ZIP 文件時調整壓縮級別? IronZIP 允許您在創建 ZIP 文件時設置0到9的壓縮級別,使您可以在文件大小和壓縮速度之間取得平衡。 在商業項目中使用 IronZIP 是否需要許可證? 是的,IronZIP 是一個商業產品,在商業項目中使用需要許可證。提供試用版供評估,可在 IronZIP 網站上找到詳細的許可證信息。 如何將 IronZIP 整合到我的 Visual Studio 項目中? 您可以通過 NuGet 包管理器將 IronZIP 整合到 Visual Studio 項目中。簡單地在 NuGet 包管理器中搜尋 'IronZIP' 並點擊 'Install'。或者,您可以使用 NuGet 包管理器控制台並運行命令:Install-Package IronZIP。 IronZIP 支持哪些 .NET 版本? IronZIP 支持多個 .NET 版本,包括 .NET 7、6、Core、Standard 和 Framework,使其與最新的 .NET 技術兼容。 在哪裡可以找到使用 IronZIP 的文檔? 您可以在 IronZIP 網站上找到使用 IronZIP 的全面文檔,其中包括教程、API 參考和範例代碼片段。 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# 中將文件解壓到目錄 無論你在開發Windows 應用程序還是 .NET 項目,了解解壓文件的過程都是非常有價值的 閱讀更多 如何在 C# 中解壓縮 Zip 文件.NET MAUI 數據提取 SDK 與 IronZIP