使用 IRONZIP 如何在 C# 中打开 ZIP 文件 Curtis Chau 已更新:六月 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 is an archive entry file system format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The ZIP file format permits several compression algorithms, though DEFLATE is the most common. ZIP format was then quickly supported by many software utilities. Top OS providers have included ZIP archive support for a long time. Microsoft has included ZIP file support from Windows 98 and the rest followed suit. In this blog, we are going to explore a modern, easy, and efficient way to open ZIP archive files or extracted files using IronZIP. We are going to learn about ZIP files in general and its advantages. Then we will see options available in the system namespace for working with ZIP file format. Then we will explore step-by-step instructions to open the ZIP file, extract ZIP file to a temp folder, create a new ZIP file, and add files to existing ZIP files. Advantages of using ZIP files in software applications Compression: This technique reduces the size of the archived files/folder using various compression algorithms like Implode, Deflate, Deflate64, bzip2, LZMA, WavPack, PPMd, etc. Reduced Transfer Time: Smaller file sizes mean faster transfer times, especially when sending files over the Internet. This is particularly advantageous for email attachments and uploading or downloading files from websites. Consolidation of Files: ZIP files enable you to consolidate multiple files into a single archive, reducing the number of files you need to manage. This is useful for organizing projects or distributing software that consists of multiple files. Password Protection: Many ZIP utilities provide the option to password-protect the archive, adding a layer of security to your files. This is useful when you want to restrict access to the contents of the ZIP file. Create ZIP Archives and Extract ZIP files using IronZIP Introduction to the IronZIP library and documentation can be found here. In C# applications, ZIP files can be created and extracted in various ways. IronZIP NuGet package has all the functionalities to archive files in different formats: ZIP, TAR, GZIP, and BZIP2. Below are the sample steps on how to use IronZIP in modern application programming to build next-gen apps to open ZIP files, extract ZIP files, create new ZIP files, etc. Step 1. Create .NET Core Console Application Create project .NET console app can be created using Visual Studio. Open Visual Studio and select Create project. Here you can see various templates to create a project. The easiest way to demonstrate or test the code is by creating a console application. We will select the Console App project template. Enter the project name In the below window, you can enter the project name, the location of the project to store in the file system and last one is the path to the solution folder. You can keep the solution and project folders the same or have them in different folders. Select .NET Framework version The next step is to select the .NET Framework version for the project. If you want to develop in a specific version, then specify the version you like. Otherwise, always select the latest stable version to create a project. The latest version can be downloaded from the Microsoft website. Then click Create to generate the console application. This will create the default project from the template and store the project and solution files in the specified directories. Once the project is created it will look similar to below. Sometimes the class is not used in the program.cs in the latest versions. // Import necessary namespaces using System; // Define the namespace namespace MyApp // Note: actual namespace depends on the project name. { // Define the Program class internal class Program { // Main method: Entry point of the application static void Main(string[] args) { // Print a welcome message Console.WriteLine("Hello, World!"); } } } // Import necessary namespaces using System; // Define the namespace namespace MyApp // Note: actual namespace depends on the project name. { // Define the Program class internal class Program { // Main method: Entry point of the application static void Main(string[] args) { // Print a welcome message Console.WriteLine("Hello, World!"); } } } ' Import necessary namespaces Imports System ' Define the namespace Namespace MyApp ' Note: actual namespace depends on the project name. ' Define the Program class Friend Class Program ' Main method: Entry point of the application Shared Sub Main(ByVal args() As String) ' Print a welcome message Console.WriteLine("Hello, World!") End Sub End Class End Namespace $vbLabelText $csharpLabel To create a new ZIP file and extract all the ZIP archive files, we can use System.IO.Compression from default libraries. The following code demonstrates how to use ZipFile.OpenRead and ZipFile.Open static methods to open ZIP files or extract ZIP files. // Import necessary namespaces using System; using System.IO; using System.IO.Compression; public class ZipExample { public static void Main() { Console.WriteLine("-----------Zip - Unzip-----------"); // Method to add a file entry to the ZIP archive static void AddEntry(string filePath, ZipArchive zipArchive) { // Get file name from the file path var file = Path.GetFileName(filePath); // Create a new entry in the ZIP archive for the file zipArchive.CreateEntryFromFile(filePath, file); } // Name of the ZIP file to be created var zip = "myFile.zip"; // Open or create the ZIP file for updating using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Update)) { // Add files to the archive AddEntry("file1.txt", archive); AddEntry("file2.txt", archive); } // Directory where we want to extract the ZIP files var dirToExtract = "extract"; // Create the directory if it does not exist if (!Directory.Exists(dirToExtract)) { Directory.CreateDirectory(dirToExtract); } // Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract); // Indicate that extraction is complete Console.WriteLine("Files extracted to: " + dirToExtract); } } // Import necessary namespaces using System; using System.IO; using System.IO.Compression; public class ZipExample { public static void Main() { Console.WriteLine("-----------Zip - Unzip-----------"); // Method to add a file entry to the ZIP archive static void AddEntry(string filePath, ZipArchive zipArchive) { // Get file name from the file path var file = Path.GetFileName(filePath); // Create a new entry in the ZIP archive for the file zipArchive.CreateEntryFromFile(filePath, file); } // Name of the ZIP file to be created var zip = "myFile.zip"; // Open or create the ZIP file for updating using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Update)) { // Add files to the archive AddEntry("file1.txt", archive); AddEntry("file2.txt", archive); } // Directory where we want to extract the ZIP files var dirToExtract = "extract"; // Create the directory if it does not exist if (!Directory.Exists(dirToExtract)) { Directory.CreateDirectory(dirToExtract); } // Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract); // Indicate that extraction is complete Console.WriteLine("Files extracted to: " + dirToExtract); } } ' Import necessary namespaces Imports System Imports System.IO Imports System.IO.Compression Public Class ZipExample Public Shared Sub Main() Console.WriteLine("-----------Zip - Unzip-----------") ' Method to add a file entry to the ZIP archive 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' static void AddEntry(string filePath, ZipArchive zipArchive) ' { ' ' Get file name from the file path ' var file = Path.GetFileName(filePath); ' ' ' Create a new entry in the ZIP archive for the file ' zipArchive.CreateEntryFromFile(filePath, file); ' } ' Name of the ZIP file to be created Dim zip = "myFile.zip" ' Open or create the ZIP file for updating Using archive As ZipArchive = ZipFile.Open(zip, ZipArchiveMode.Update) ' Add files to the archive AddEntry("file1.txt", archive) AddEntry("file2.txt", archive) End Using ' Directory where we want to extract the ZIP files Dim dirToExtract = "extract" ' Create the directory if it does not exist If Not Directory.Exists(dirToExtract) Then Directory.CreateDirectory(dirToExtract) End If ' Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract) ' Indicate that extraction is complete Console.WriteLine("Files extracted to: " & dirToExtract) End Sub End Class $vbLabelText $csharpLabel In the above code, a ZIP file named myFile.zip is used with the system namespace. The Open method is used to open the ZIP file in a specified mode. This can also be used to create new ZIP archive files. Once opened, we can add a new archive entry using the method AddEntry, and then ExtractToDirectory extracts ZIP archive files to the specified directory. If the directory doesn't exist, it is created using Directory.CreateDirectory. Step 2. Installing IronZIP using NuGet Package Manager Open Project Manager from Visual Studio and search for the IronZIP package. Then select the latest version and click Install. You can change the version to be installed from the drop-down. Then click Install. Create a ZIP Archive File and add files using IronZIP IronZIP is an archive compression and decompression library developed by Iron Software. In addition to the widely used ZIP format, it can also handle TAR, GZIP, and BZIP2. IronZIP is a C# ZIP Archive Library that prioritizes accuracy, ease of use, and speed. Its user-friendly API enables developers to easily add archive functionality to modern .NET projects in minutes. IronZIP offers many advantages compared to the System.IO.Compression library. You can specify the compression ratio you want during compression and also use different compression algorithms such as ZIP, TAR, GZIP, BZIP2. It also supports mobile, web, and desktop platforms and various .NET versions. // Setup: Specify the path for the new ZIP archive var archivePath = "ironZip.zip"; // Check if the archive already exists, and delete it if so if (File.Exists(archivePath)) { File.Delete(archivePath); } // Use IronZIP library to create a new ZIP archive using (var archive = new IronZipArchive(9)) // Compression level: 9 for maximum compression { // Add files to the ZIP archive archive.Add("file1.txt"); archive.Add("file2.txt"); // Save the archive to the specified path archive.SaveAs(archivePath); } // Setup: Specify the path for the new ZIP archive var archivePath = "ironZip.zip"; // Check if the archive already exists, and delete it if so if (File.Exists(archivePath)) { File.Delete(archivePath); } // Use IronZIP library to create a new ZIP archive using (var archive = new IronZipArchive(9)) // Compression level: 9 for maximum compression { // Add files to the ZIP archive archive.Add("file1.txt"); archive.Add("file2.txt"); // Save the archive to the specified path archive.SaveAs(archivePath); } ' Setup: Specify the path for the new ZIP archive Dim archivePath = "ironZip.zip" ' Check if the archive already exists, and delete it if so If File.Exists(archivePath) Then File.Delete(archivePath) End If ' Use IronZIP library to create a new ZIP archive Using archive = New IronZipArchive(9) ' Compression level: 9 for maximum compression ' Add files to the ZIP archive archive.Add("file1.txt") archive.Add("file2.txt") ' Save the archive to the specified path archive.SaveAs(archivePath) End Using $vbLabelText $csharpLabel The initial source code sets up by specifying the ZIP archive file name and checking if the specified directory exists. Then we archive the files to create a ZIP file using the Add method. The second parameter in the compression parameter is 1 for lower and 9 for higher. Text files can be compressed to max using 9 without loss, and image files can use lower compression to avoid data loss. Open the ZIP Archive File Using IronZIP IronArchive class can also be used to extract the files from the ZIP archive file. All the files are extracted using IronArchive.ExtractArchiveToDirectory, which helps extract all the files to specific directories like below. // Directory to extract all files from the ZIP archive var extractionPath = "IronZipFiles"; // Check if the directory exists; if not, create it if (!Directory.Exists(extractionPath)) { Directory.CreateDirectory(extractionPath); } // Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath); // Directory to extract all files from the ZIP archive var extractionPath = "IronZipFiles"; // Check if the directory exists; if not, create it if (!Directory.Exists(extractionPath)) { Directory.CreateDirectory(extractionPath); } // Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath); ' Directory to extract all files from the ZIP archive Dim extractionPath = "IronZipFiles" ' Check if the directory exists; if not, create it If Not Directory.Exists(extractionPath) Then Directory.CreateDirectory(extractionPath) End If ' Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath) $vbLabelText $csharpLabel The above code extracts ZIP files to a directory. The code checks if the directory exists, and if not, the ZIP archive file is extracted to the specified directory. Licensing (Free Trial Available) For the above code to work, a license key is required. This key needs to be placed in appsettings.json. { "IronZip.LicenseKey": "your license key" } A trial license is available for developers upon registering here, and yes, no credit card is required for a trial license. One can provide the email ID and register for a free trial. Add Files to an Existing ZIP Archive File Use the static FromFile method from IronZIP to open an existing archive. This method also requires specifying the file name of the new archive that will be created as output. // Path to a new file to be added to the existing ZIP archive const string file3 = ".\\image3.png"; var archivePlusPath = "ironZipPlus.zip"; // Open the existing ZIP archive and add a new file using (var file = IronArchive.FromFile("ironZip.zip", archivePlusPath)) { // Add additional files to the existing archive file.Add(file3); } // Path to a new file to be added to the existing ZIP archive const string file3 = ".\\image3.png"; var archivePlusPath = "ironZipPlus.zip"; // Open the existing ZIP archive and add a new file using (var file = IronArchive.FromFile("ironZip.zip", archivePlusPath)) { // Add additional files to the existing archive file.Add(file3); } ' Path to a new file to be added to the existing ZIP archive Const file3 As String = ".\image3.png" Dim archivePlusPath = "ironZipPlus.zip" ' Open the existing ZIP archive and add a new file Using file = IronArchive.FromFile("ironZip.zip", archivePlusPath) ' Add additional files to the existing archive file.Add(file3) End Using $vbLabelText $csharpLabel The code uses the IronArchive.FromFile static method to open the existing ZIP file and then add the new file as an archive entry. Once the file object is disposed, the updated archive is successfully saved. Updates The IronZIP library is constantly updated based on feedback from customers/users, and all the updates can be found here. Conclusion In conclusion, ZIP file programming is an essential skill to build in modern application development where storage and data transfer costs are charged by cloud host providers. Knowing this skill can help programmers reduce the cost of the application and improve the performance of the application. By following the installation steps and exploring the provided code examples, developers can quickly leverage the power of IronZIP to handle ZIP tasks with ease. As more and more applications are modernized and moved to the cloud, having a reliable ZIP library like IronZIP equips C# developers with the tools necessary to meet the demands of modern application development. So, embrace the power of IronZIP and unlock new possibilities for working with ZIP files in your C# applications. IronZIP offers extensive support for its developers. To know more about how IronZIP for C# works visit here. IronZIP offers a free trial license which is a great opportunity to know IronZIP and its features. Iron Software has various other libraries, explore them to gain more knowledge and update your skills to program/develop modern applications. 常见问题解答 如何在 C# 中打开一个 ZIP 文件? 您可以使用 System.IO.Compression 库在 C# 中打开 ZIP 文件。或者,IronZIP 提供了处理 ZIP 文件的高级功能,提供了一种更简单、更高效的管理您的存档方式。 如何使用 C# 从 ZIP 存档中提取文件? 使用 IronZIP,您可以使用 IronArchive.ExtractArchiveToDirectory 方法从 ZIP 存档中提取文件。此方法要求您指定 ZIP 文件和提取的目标目录。 ZIP 文件在应用程序开发中的好处是什么? ZIP 文件可减少文件大小,从而实现更快的传输时间,并能够将多个文件合并到一个存档中。IronZIP 增强了这些好处,具有诸如指定压缩比和支持多种格式的附加功能。 如何在 C# 中向现有 ZIP 存档添加文件? 要使用 IronZIP 向现有 ZIP 存档添加文件,请利用 IronArchive.FromFile 方法打开存档,然后使用 Add 方法包含新文件。保存更新后的存档以完成过程。 我可以使用 IronZIP 创建一个新的 ZIP 文件并向其添加文件吗? 是的,使用 IronZIP,您可以通过指定存档路径和使用 Add 方法添加文件来创建一个新的 ZIP 文件。然后使用 SaveAs 方法保存存档。 如何使用 NuGet 程序包管理器安装 IronZIP? 要通过 NuGet 程序包管理器安装 IronZIP,请打开 Visual Studio 中的项目管理器,搜索 IronZIP,选择最新版本,然后点击安装。这将把 IronZIP 添加到您的项目中,从而启用 ZIP 文件管理。 IronZIP 是否支持多种压缩格式? 是的,IronZIP 支持多种压缩格式,包括 ZIP、TAR、GZIP 和 BZIP2,为各种应用需求提供灵活性。 IronZIP 是否提供免费试用版? 是的,IronZIP 为开发人员提供免费试用。您可以在 Iron Software 网站上注册以访问试用版,无需信用卡。 IronZIP 是现代应用程序开发的合适选择吗? IronZIP 以其易用性、速度和跨平台兼容性而闻名。这些功能,以及其先进的功能,使其成为现代应用程序开发的理想选择。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新六月 22, 2025 如何在 C# 中将 ZIP 归档文件提取到目录 ZIP 文件是将多个文件和目录捆绑成单一归档的便捷方式。 阅读更多 已更新七月 28, 2025 如何在 C# 中创建带密码的 ZIP 文件 在本文中,我们将探索如何使用 C# 和 IronZIP 库创建一个密码保护的 ZIP 文件。 阅读更多 已更新七月 28, 2025 如何在 C# 中将文件解压到目录 无论您是在开发 Windows 应用程序还是 .NET 项目,理解文件解压过程都非常有价值。 阅读更多 如何在 .NET Core 中解压缩文件如何在 C# .NET Core 中创建 ZI...