使用 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 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文件管理的库? 你可以通过NuGet包管理器控制台使用命令Install-Package IronZIP安装IronZIP库,或者在NuGet包管理器中浏览'IronZIP'以供解决方案使用。 我可以用什么方法在不提取的情况下查看ZIP条目? IronZIP允许你通过使用GetArchiveEntryNames()方法在不提取的情况下查看ZIP存档的条目,该方法列出了存档中的所有条目。 是否可以使用C#检查ZIP存档中特定文件? 是的,你可以使用IronZIP的CheckArchiveEntryExist()方法检查ZIP文件中特定条目是否存在。 如何在C#中从ZIP存档中删除文件? 要使用IronZIP从ZIP存档中删除文件,请在指定文件的IronZipArchive对象上使用DeleteArchiveEntry()方法。 该C#库支持哪些平台进行ZIP文件管理? IronZIP支持在包括Windows、Linux和macOS在内的各种平台上进行ZIP文件管理,确保跨平台兼容性。 使用IronZIP库进行ZIP文件管理有什么好处? IronZIP提供高性能、易用性、跨平台兼容性、以及无缝的NuGet集成,并为开发人员提供全面的文档和教程。 我在哪里可以找到使用C#管理ZIP文件的教程? IronZIP的官方网站上可找到官方文档和教程,提供关于使用该库进行ZIP文件管理的详细指导。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新七月 28, 2025 如何在 C# 中创建带密码的 ZIP 文件 在本文中,我们将探索如何使用 C# 和 IronZIP 库创建一个密码保护的 ZIP 文件。 阅读更多 已更新七月 28, 2025 如何在 C# 中将文件解压到目录 无论您是在开发 Windows 应用程序还是 .NET 项目,理解文件解压过程都非常有价值。 阅读更多 已更新七月 28, 2025 如何使用 C# 压缩文件夹中的文件 ZIP 文件即包含使用 ZIP 格式压缩的一个或多个文件或文件夹的文件。这是一种将多个文件或文件夹压缩和归档为一个文件的常见方法。 阅读更多 如何使用 C# 压缩文件夹中...
已更新七月 28, 2025 如何使用 C# 压缩文件夹中的文件 ZIP 文件即包含使用 ZIP 格式压缩的一个或多个文件或文件夹的文件。这是一种将多个文件或文件夹压缩和归档为一个文件的常见方法。 阅读更多