使用 IRONZIP C# ZIP 文件(使用 IronZIP 的开发人员教程) Curtis Chau 已更新:七月 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 ZIP is a popular method for efficiently compressing and consolidating multiple files and folders into a unified ZIP archive using FileStream. Working with these files in C# is made accessible using IronZIP namespace, which provides classes to create and extract existing ZIPs from a path or individual file contained and manipulate ZIP archives. Whether you're compressing files, dealing with password-protected ZIPs, or unzipping files, the ZipFile class within the IronZIP namespace becomes your invaluable ally. In this tutorial, we not only explore the fundamental processes of creating and extracting ZIP archives but also learn code samples to achieve it. The ZipArchive class seamlessly progresses through the string path, enabling you to navigate and manipulate ZIP files with unparalleled precision. Moreover, extending beyond ZIP manipulation, we'll also explore IronPDF, a tool for compressing PDF files without compromising document integrity. Together, the tandem of IronZIP and IronPDF equips developers with a robust set of tools to efficiently manage compressed files and streamline PDFs within the C# environment. Prerequisites Before you explore ZIP file manipulation with IronZIP and IronPDF, ensure the following prerequisites are met: Visual Studio: Install Visual Studio or any other C# Integrated Development Environment (IDE) of your choice. Basic C# knowledge: Familiarize yourself with fundamental concepts of the C# programming language. Install IronZIP Package To kick-start your journey with IronZIP, swiftly install the IronZIP NuGet Package in your project. Execute the following command in the NuGet Package Manager Console: Install-Package IronZip Alternatively, download the package directly from the official IronZIP NuGet website. Once the installation is complete, initiate your C# code by adding the using IronZip; statement at the top. Apply License Key Ensure you have a valid license or trial key for IronZIP. Apply the license key by assigning it to the LicenseKey property of the License class. Include the following code immediately after the import statement and before using any IronZIP methods: using IronZip; namespace YourNamespace { class Program { static void Main(string[] args) { // Apply the IronZIP license key IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01"; // Your ZIP manipulation code using IronZIP } } } using IronZip; namespace YourNamespace { class Program { static void Main(string[] args) { // Apply the IronZIP license key IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01"; // Your ZIP manipulation code using IronZIP } } } Imports IronZip Namespace YourNamespace Friend Class Program Shared Sub Main(ByVal args() As String) ' Apply the IronZIP license key IronZip.Licensing.License.LicenseKey = "IRONZIP.MYLICENSE.KEY.1EF01" ' Your ZIP manipulation code using IronZIP End Sub End Class End Namespace $vbLabelText $csharpLabel This step is crucial for unleashing the full potential of IronZIP in your project. Create a ZIP File Creating a ZIP file with IronZIP is simple. Utilize the IronArchive class to establish an empty ZIP archive and then add files to it. The following code snippet demonstrates the creation of a ZIP file named "Images.zip" and the addition of four image files to it: using IronZip; namespace CSharpZipArchive { internal class Program { static void Main(string[] args) { // Create an archive called Images.zip using (var archive = new IronArchive("Images.zip")) { // Add files to the ZIP archive.Add(@"E:\Datasets\1002-v1.png"); archive.Add(@"E:\Datasets\1002-v2.png"); archive.Add(@"E:\Datasets\1002-v3.png"); archive.Add(@"E:\Datasets\1002-v4.png"); } // The archive is automatically closed here } } } using IronZip; namespace CSharpZipArchive { internal class Program { static void Main(string[] args) { // Create an archive called Images.zip using (var archive = new IronArchive("Images.zip")) { // Add files to the ZIP archive.Add(@"E:\Datasets\1002-v1.png"); archive.Add(@"E:\Datasets\1002-v2.png"); archive.Add(@"E:\Datasets\1002-v3.png"); archive.Add(@"E:\Datasets\1002-v4.png"); } // The archive is automatically closed here } } } Imports IronZip Namespace CSharpZipArchive Friend Class Program Shared Sub Main(ByVal args() As String) ' Create an archive called Images.zip Using archive = New IronArchive("Images.zip") ' Add files to the ZIP archive.Add("E:\Datasets\1002-v1.png") archive.Add("E:\Datasets\1002-v2.png") archive.Add("E:\Datasets\1002-v3.png") archive.Add("E:\Datasets\1002-v4.png") End Using ' The archive is automatically closed here End Sub End Class End Namespace $vbLabelText $csharpLabel The using statement creates a scoped instance of the IronArchive class and associates it with the variable archive. The constructor of IronArchive takes a single argument: the name of a ZIP file ("Images.zip" in this case). The code adds four files to the ZIP archive represented by the archive variable. The files are specified by their full paths: E:\Datasets\1002-v1.png E:\Datasets\1002-v2.png E:\Datasets\1002-v3.png E:\Datasets\1002-v4.png Since the IronArchive class implements IDisposable, the using statement ensures the archive is closed correctly, and resources are released when the code block is exited. In this way, this program creates a ZIP archive named "Images.zip" and adds four image files to it. IronZip made this super easy, fast, and efficient. Extract ZIP File We can extract a ZIP file using a single line of code in C#. using IronZip; namespace YourNamespace { public class ExtractionExample { public static void Main(string[] args) { // Extract contents of the ZIP archive IronArchive.ExtractArchiveToDirectory("Images.zip", "Extracted Images"); } } } using IronZip; namespace YourNamespace { public class ExtractionExample { public static void Main(string[] args) { // Extract contents of the ZIP archive IronArchive.ExtractArchiveToDirectory("Images.zip", "Extracted Images"); } } } Imports IronZip Namespace YourNamespace Public Class ExtractionExample Public Shared Sub Main(ByVal args() As String) ' Extract contents of the ZIP archive IronArchive.ExtractArchiveToDirectory("Images.zip", "Extracted Images") End Sub End Class End Namespace $vbLabelText $csharpLabel The above code extracts the contents of the ZIP archive file named "Images.zip". The extracted files are placed in a directory named "Extracted Images". The IronArchive class provides the ExtractArchiveToDirectory method for this purpose, which makes extracting the content of a ZIP file very efficient. This one-liner efficiently handles the extraction process, simplifying file management. Add Files to an Existing ZIP file using C# In C# software applications, adding files to an existing ZIP file is valuable for dynamic content management. Imagine a file manager application where users can easily expand a compressed archive by adding new files. IronZIP provides seamless functionality to modify any existing ZIP file. using IronZip; namespace CSharpZipArchive { internal class Program { static void Main(string[] args) { // Open an existing ZIP file and create a new one using (var archive = IronArchive.FromFile("Images.zip", "NewImages.zip")) { // Add new files to the ZIP archive.Add(@"E:\Datasets\1011-v1.png"); archive.Add(@"E:\Datasets\1011-v2.png"); } // The new archive is automatically closed here } } } using IronZip; namespace CSharpZipArchive { internal class Program { static void Main(string[] args) { // Open an existing ZIP file and create a new one using (var archive = IronArchive.FromFile("Images.zip", "NewImages.zip")) { // Add new files to the ZIP archive.Add(@"E:\Datasets\1011-v1.png"); archive.Add(@"E:\Datasets\1011-v2.png"); } // The new archive is automatically closed here } } } Imports IronZip Namespace CSharpZipArchive Friend Class Program Shared Sub Main(ByVal args() As String) ' Open an existing ZIP file and create a new one Using archive = IronArchive.FromFile("Images.zip", "NewImages.zip") ' Add new files to the ZIP archive.Add("E:\Datasets\1011-v1.png") archive.Add("E:\Datasets\1011-v2.png") End Using ' The new archive is automatically closed here End Sub End Class End Namespace $vbLabelText $csharpLabel The IronArchive.FromFile("Images.zip", "NewImages.zip") method creates an archive object from an existing ZIP file named "Images.zip" and specifies that the new archive should be named "NewImages.zip". The two lines within the using block add files to the newly created archive: archive.Add(@"E:\Datasets\1011-v1.png"); archive.Add(@"E:\Datasets\1011-v2.png"); These lines add the specified image files to the "NewImages.zip" archive. Compress PDF File In addition to managing ZIPs, C# also offers the capability to compress PDF files seamlessly. This task is made achievable with the assistance of a third-party library: IronPDF. It stands out as a reliable solution for compressing PDFs while preserving their quality. Introducing IronPDF IronPDF is a versatile C# library designed to empower developers in efficiently handling PDF documents within their .NET applications. It provides many features, including PDF generation, manipulation, text and image extraction, forms handling, digital signatures, HTML to PDF conversion, PDF compression, and security enhancements. These capabilities make it an invaluable tool for tasks such as PDF creation, editing, report generation, and document management within the .NET framework, ensuring productivity and the ability to customize PDF operations. Install IronPDF To incorporate IronPDF into your project, execute the following command in the Package Manager Console: Install-Package IronZip Write a Code to Compress PDF File in C# The following code sample demonstrates PDF file compression while maintaining the archive quality. using IronPdf; namespace YourNamespace { internal class Program { static void Main(string[] args) { // Load the PDF document var pdf = new PdfDocument(@"E:\myPDF.pdf"); // Compress images in the PDF to 60% of original quality pdf.CompressImages(60); // Save the compressed PDF pdf.SaveAs(@"E:\myCompressedPDF.pdf"); } } } using IronPdf; namespace YourNamespace { internal class Program { static void Main(string[] args) { // Load the PDF document var pdf = new PdfDocument(@"E:\myPDF.pdf"); // Compress images in the PDF to 60% of original quality pdf.CompressImages(60); // Save the compressed PDF pdf.SaveAs(@"E:\myCompressedPDF.pdf"); } } } Imports IronPdf Namespace YourNamespace Friend Class Program Shared Sub Main(ByVal args() As String) ' Load the PDF document Dim pdf = New PdfDocument("E:\myPDF.pdf") ' Compress images in the PDF to 60% of original quality pdf.CompressImages(60) ' Save the compressed PDF pdf.SaveAs("E:\myCompressedPDF.pdf") End Sub End Class End Namespace $vbLabelText $csharpLabel In the above code, we open "myPDF.pdf" using IronPDF. The CompressImages method reduces image quality to 60%, and the compressed PDF is saved as "myCompressedPDF.pdf" in the specified location. Output Executing the above code yields a tangible reduction in file size, as evidenced by the output, where the compressed PDF's size is diminished to 254 KB from its original 0.9 MB size. This demonstrates the effectiveness of IronPDF in efficiently compressing PDF files without compromising their quality. Efficient File Management with IronZIP and IronPDF in C# In C# development, the combination of IronZIP and IronPDF presents a robust solution for comprehensive file management. IronZIP excels in efficiently compressing and manipulating ZIP files, offering developers a seamless experience for creating and extracting archives. Simultaneously, IronPDF serves as a powerful tool for handling PDF documents, enabling file compression while maintaining document integrity. This integration allows developers to navigate effortlessly between compressed archives and PDF files, enhancing the overall functionality and performance of their applications. Whether compressing diverse files into a ZIP archive or optimizing PDF document sizes, the combined power of IronZIP and IronPDF forms a flexible toolkit for developers, addressing a range of file-related challenges. Conclusion In conclusion, IronZIP for C# offers robust capabilities for working with compressed files, enabling developers to create ZIP files, extract, and manipulate archives efficiently. IronPDF is also a powerful tool for compressing PDF files, making it easy to reduce file sizes while maintaining document integrity. Developers can leverage these features to enhance their applications' performance and functionality, ensuring the seamless handling of archived and compressed files. Furthermore, it's worth noting that IronPDF offers a flexible licensing model, including a free trial, allowing developers to explore its capabilities and determine the best fit for their projects. This trial period provides a valuable opportunity for developers to evaluate IronZIP's and IronPDF's features before committing to licensing. 常见问题解答 如何在 C# 中创建一个 ZIP 文件? 要在 C# 中创建一个 ZIP 文件,您可以使用 IronZIP 库中的 IronArchive 类。该类允许您创建一个空的 ZIP 压缩包并添加文件,提供高效的方法来管理 ZIP 内容。 如何在 C# 中从 ZIP 压缩包中提取文件? 可以使用 IronZIP 库的 IronArchive 类中的 ExtractArchiveToDirectory 方法从 ZIP 压缩包中提取文件。该方法允许您指定要提取文件的目录。 我可以在 C# 中处理密码保护的 ZIP 文件吗? 是的,IronZIP 提供了管理密码保护的 ZIP 文件的功能,能够安全地压缩和提取敏感数据。 在 C# 中安装 ZIP 库包的步骤是什么? 要安装 IronZIP 库,请在 NuGet 包管理器控制台中使用命令 Install-Package IronZip,或直接从官方网站下载。 如何在 C# 中为 ZIP 库应用许可证密钥? 要为 IronZIP 应用许可证密钥,请在应用程序中将您的许可证密钥分配给 Licensing.License 类的 LicenseKey 属性。 如何在 C# 中压缩 PDF 文件? 要在 C# 中压缩 PDF 文件,可以使用 IronPDF。加载 PDF 文档并应用 CompressImages 方法以降低图像质量,然后将压缩的 PDF 保存到所需位置。 C# 的 PDF 库是否提供试用版? 是的,IronPDF 提供免费试用版,允许开发者在签署许可协议之前探索其功能。 在 C# 中处理 ZIP 和 PDF 文件的先决条件是什么? 在 C# 中处理 ZIP 和 PDF 文件,您需要安装 Visual Studio 或类似的 C# 集成开发环境,并具备 C# 编程概念的基本理解。您还需要 IronZIP 和 IronPDF 库。 一种用于处理 ZIP 文件的 C# 工具是什么? IronZIP 是一个 C# 库,设计用于高效地创建、操作和提取 ZIP 压缩包,是开发人员在 .NET 应用程序中处理 ZIP 文件的理想选择。 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 项目,理解文件解压过程都非常有价值。 阅读更多 如何在 C# 中创建 ZIP 归档文件如何在 C# 中提取 ZIP 文件