与其他组件比较 C# 开发人员使用 IronXL 的 Zip 存档教程 Curtis Chau 已更新:六月 22, 2025 Download IronXL NuGet 下载 DLL 下载 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 Introduction to ZipArchive Whenever you think of sending a file over in a compressed format, the first thing that comes to mind is usually a Zip Archive. A zip archive is a popular format for compressing and packing a single file or an entire collection in a single archive. However, when there are large volumes of files to zip, working with them might become frustrating as you would have to archive and format them one by one. But it doesn't have to be like that. Large-volume tasks need automation to be done programmatically. We can use IronZip, a simple yet intuitive library, to achieve all that and more. In this article, we'll briefly discuss IronZip's core features and functionalities and how it can help you better automate your workflow, increase efficiency, and eliminate error-prone manual tasks. Creating a new zip file from a folder or files Below is an example that takes in multiple files, zips them into one archive, and exports them after the operation. All examples in this article are encased in the static void Main() method to avoid repeated code blocks. using IronZip; class Program { static void Main(string[] args) { // Create an empty ZIP archive using (var archive = new IronZipArchive()) { // Add files to the ZIP archive archive.Add("./assets/image1.jpg"); archive.Add("./assets/image2.jpg"); archive.Add("./assets/image3.jpg"); // Export the ZIP archive to a file archive.SaveAs("output.zip"); } } } using IronZip; class Program { static void Main(string[] args) { // Create an empty ZIP archive using (var archive = new IronZipArchive()) { // Add files to the ZIP archive archive.Add("./assets/image1.jpg"); archive.Add("./assets/image2.jpg"); archive.Add("./assets/image3.jpg"); // Export the ZIP archive to a file archive.SaveAs("output.zip"); } } } Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Create an empty ZIP archive Using archive = New IronZipArchive() ' Add files to the ZIP archive archive.Add("./assets/image1.jpg") archive.Add("./assets/image2.jpg") archive.Add("./assets/image3.jpg") ' Export the ZIP archive to a file archive.SaveAs("output.zip") End Using End Sub End Class $vbLabelText $csharpLabel We first import IronZip. Then, using IronZip, we create an archive. We call archive.Add() to add multiple files to the archive. Remember that path names must be absolute paths, or the operation will fail to find the corresponding files. Finally, we call archive.SaveAs() to export the zip archive as output.zip. Updating an existing zip file with new files or modifications Let's go over another example; this time, we will edit the existing zip archive with new files to showcase the functionality of IronZip. using IronZip; class Program { static void Main(string[] args) { // Open an existing ZIP archive using (var archive = IronZipArchive.FromFile("existing.zip")) { // Add additional files to the existing ZIP archive archive.Add("./assets/image3.png"); archive.Add("./assets/image4.png"); // Export the updated ZIP archive to a new file archive.SaveAs("result.zip"); } } } using IronZip; class Program { static void Main(string[] args) { // Open an existing ZIP archive using (var archive = IronZipArchive.FromFile("existing.zip")) { // Add additional files to the existing ZIP archive archive.Add("./assets/image3.png"); archive.Add("./assets/image4.png"); // Export the updated ZIP archive to a new file archive.SaveAs("result.zip"); } } } Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Open an existing ZIP archive Using archive = IronZipArchive.FromFile("existing.zip") ' Add additional files to the existing ZIP archive archive.Add("./assets/image3.png") archive.Add("./assets/image4.png") ' Export the updated ZIP archive to a new file archive.SaveAs("result.zip") End Using End Sub End Class $vbLabelText $csharpLabel We first import IronZip. Using IronZip, we create an archive, but this time, we import the existing zip file with IronZipArchive.FromFile(). We call archive.Add() to add the desired files to the archive. Remember that path names must be absolute paths, or the operation will fail to find the existing files. Finally, we call archive.SaveAs() to export the updated zip archive as result.zip. As you can see from the code above, the operation and format are similar to those used to create and add files to a Zip file. The main difference is that we import the zip file instead, showcasing IronZip's simple yet intuitive functionality. Extracting files from a Zip archive We can also use IronZip to extract files from a Zip archive or Zip package. Let's review this in another example. using IronZip; class Program { static void Main(string[] args) { // Extract the ZIP archive content to a specified directory IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted"); } } using IronZip; class Program { static void Main(string[] args) { // Extract the ZIP archive content to a specified directory IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted"); } } Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Extract the ZIP archive content to a specified directory IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted") End Sub End Class $vbLabelText $csharpLabel We import IronZip and call IronZipArchive.ExtractArchiveToDirectory(). This method extracts the contents of the existing zip archive to the specified target directory. Furthermore, in cases where you need to deal with password-protected zip archives, we can use another method to extract the archives. using IronZip; class Program { static void Main(string[] args) { // Extract the protected ZIP archive content to a specified directory IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd"); } } using IronZip; class Program { static void Main(string[] args) { // Extract the protected ZIP archive content to a specified directory IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd"); } } Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Extract the protected ZIP archive content to a specified directory IronZipArchive.ExtractArchiveToDirectory("output.zip", "extracted", "P@ssw0rd") End Sub End Class $vbLabelText $csharpLabel Ultimately, we pass in another parameter: the password for the protected zip archive. The rest of the operation is the same as shown above. Advanced Topics and Best Practices The code examples for adding, extracting, and creating archives discussed above are the most common when dealing with zip archives. However, for more advanced usage, such as extracting other formats or simply viewing the archive's contents, IronZIP has you covered and more. Advanced Features of IronZIP Cross-compatibility: IronZIP is compatible with a wide range of .NET versions, including .NET Core (3.1+), Standard (2.0+), and .NET Framework (4.6.2+). The library also works on the Web (Blazor), Mobile (MAUI), Desktop (WPF), and Console. This allows developers to transcend platform and version limitations. Generating Archives: Beyond the ZIP format, IronZIP supports creating TAR, GZIP, and BZIP2 archives. Extracting Archives: Extract archives and decompress files easily with IronZIP using a single block of code, as demonstrated above. Adding Files and File Systems: IronZIP assists in manipulating zip archives by adding images, text files, and documents (such as PDFs, DOCX, and XLSX), as well as audio files like MP3 and WAV. It can even compress entire file systems or individual text files into a ZipArchive archive. Export and Create: You can password-protect archives with AES128 and AES256 standards. Additionally, you can generate and export formats like TAR, GZIP, and BZIP2. Custom Compression Levels: IronZIP allows developers to adjust compression settings to fine-tune the algorithm to their needs. Editing Archives: Easily add, extract, or delete file entries in an archive using IronZIP as a comprehensive solution for editing-related operations. File Entry Properties: IronZIP provides the ability to set optional archive comments and retrieve file names within the archive without extracting them, aiding in leaving specific comments for each file. Licensing Choices: IronZIP offers adaptable licensing choices, including complimentary trial versions, allowing developers to select what suits their requirements best. Conclusion Dealing with compressed files and archives is a daily task that developers often face, but managing a large volume of them can be stressful and prone to human error. Although there are options like System.IO.Compression, using IronZIP allows you to achieve more and quickly resolve issues when dealing with compressed archives. In this article, we discussed the advanced features of IronZIP and common operations like creating zip archives, deleting entries, or adding files. Understanding how to perform these tasks programmatically with IronZIP leads to increased efficiency, scalability, and automation in handling archives. 请注意System.IO.Compression is a registered trademark of its respective owner. This site is not affiliated with, endorsed by, or sponsored by System.IO.Compression. All product names, logos, and brands are property of their respective owners. Comparisons are for informational purposes only and reflect publicly available information at the time of writing. 常见问题解答 如何在C#中从文件夹创建ZIP文件? 要在C#中从文件夹创建ZIP文件,您可以使用IronZip库。首先,导入IronZip库,创建一个IronZipArchive实例,使用archive.AddFolder()添加文件夹,然后用archive.SaveAs()保存。 在C#中从ZIP档案中提取数据的过程是怎样的? 您可以使用IronZip在C#中通过调用IronZipArchive.ExtractArchiveToDirectory()方法从ZIP档案中提取数据。此方法允许您指定一个目录以提取内容。 如何在C#中以编程方式更新ZIP档案的内容? 要在C#中以编程方式更新ZIP档案的内容,使用IronZip加载现有档案IronZipArchive.FromFile(),然后使用archive.Add()添加文件或使用archive.Remove()删除文件,再用archive.SaveAs()保存更改。 IronZip能处理密码保护的ZIP文件吗? 是的,IronZip能处理密码保护的ZIP文件。您可以在使用IronZipArchive.ExtractArchiveToDirectory()方法提取文件时指定密码,将密码作为额外参数提供。 使用IronZip代替System.IO.Compression进行ZIP文件处理的好处是什么? IronZip提供了比System.IO.Compression更高级的功能,例如支持多种档案格式、密码保护、自定义压缩级别,以及无需提取就可以通过添加或删除文件来编辑档案。 IronZip如何确保与不同.NET版本的跨兼容性? IronZip通过支持多个.NET版本,确保跨兼容性,允许开发人员将ZIP文件处理功能集成到运行在各种平台和框架上的应用程序中。 IronZip为处理ZIP档案提供哪些高级功能? IronZip提供高级功能,如生成和提取多种档案格式(ZIP,TAR,GZIP,BZIP2)、密码保护、自定义压缩级别,以及无需提取文件的全面编辑能力。 是否可以使用IronZip管理ZIP档案中的文件属性? 是的,IronZip允许在无需先提取文件的情况下管理ZIP档案中的文件属性(如评论),提高在档案管理中的灵活性和效率。 如何在C#中自动化ZIP文件的创建和提取? 您可以在C#中通过在static void Main()方法中使用IronZip来自动化ZIP文件的创建和提取。这使您能够自动化添加文件到档案、提取数据以及以编程方式更新现有档案的过程。 IronZip支持处理整个文件系统吗? 是的,IronZip支持整个文件系统的压缩和提取,适合高效处理大数据量。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新七月 28, 2025 比较三个开源 C# Excel 库 本文将探讨三个 C# 开源 Excel 库,旨在简化 .NET 环境中的 Excel 文件操作 阅读更多 已更新八月 4, 2025 EPPlus 读取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一个强大的开源库,用于在 C# 中创建和操作 Excel 文件。它提供了一个简单直观的 API,使开发人员能够以编程方式生成、读取和修改 Excel 电子表格。 阅读更多 已更新七月 28, 2025 IronXL 和 FastExcel 的比较 - .NET 在本文中,我们将对比 IronXL 和 FastExcel v4,重点介绍每个库的优缺点,并提供如何在 C# 中使用它们的示例。 阅读更多 对比 IronXL 和 GemBox.SpreadsheetEPPlus 读取 Excel 到 DataTable C...
已更新八月 4, 2025 EPPlus 读取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一个强大的开源库,用于在 C# 中创建和操作 Excel 文件。它提供了一个简单直观的 API,使开发人员能够以编程方式生成、读取和修改 Excel 电子表格。 阅读更多
已更新七月 28, 2025 IronXL 和 FastExcel 的比较 - .NET 在本文中,我们将对比 IronXL 和 FastExcel v4,重点介绍每个库的优缺点,并提供如何在 C# 中使用它们的示例。 阅读更多