使用 IRONZIP 如何在 C# 中使用密碼壓縮文件 Curtis Chau 更新日期:7月 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 files are widely used for compressing and archiving data, making it easier to transfer and store large sets of files. However, there are scenarios where additional security is essential, leading to the importance of password-protected zip files. Password protection ensures that only authorized individuals can access and extract the contents of the ZIP archive, adding an extra layer of security to sensitive data. In this article, we will explore how to create a password-protected ZIP file using C# and the IronZIP library. IronZIP is a powerful C# ZIP archive library that simplifies the process of working with ZIP files in .NET applications. How to Create a C# ZIP File with Password Protection Create a C# project in Visual Studio Install IronZIP Library from NuGet Package Manager Create an empty ZIP archive object using the IronZipArchive Class Add password protection using the Encrypt method Add files to the archive object using the Add method Export the ZIP archive using the SaveAs method Introduction to IronZIP Library IronZIP is a leading C# ZIP archive library designed for creating, reading, and extracting archives in .NET. It offers a user-friendly API that allows developers to easily incorporate archive management functionality into their .NET projects. With support for various archive formats, including ZIP, TAR, GZIP, and BZIP2, IronZIP provides a comprehensive solution for handling zip files with ease. Detailed Features of IronZIP Compatibility Supports .NET 8, 7, 6, 5, Core, Standard, and Framework. Compatible with C#, VB.NET, and F# languages. Cross-platform support for Windows, Linux, Mac, iOS, Android, Docker, Azure, and AWS. Integration with popular IDEs like Microsoft Visual Studio and JetBrains ReSharper & Rider. Archive Generation and Editing Supports ZIP, TAR, GZIP, and BZIP2 archive formats. Create, import, and export ZIP files. Password protection for ZIP files using traditional, AES128, or AES256 encryption settings. Custom compression with 9 levels. Provides the best reduction in size. Manage file entries within archives, including adding, extracting, and deleting. Installation Quick and easy installation via NuGet Package Manager or Package Manager Console. Integration with DigiCert Signed Binaries for secure binary certification. Steps to Create a C# Console Project in Visual Studio Let's walk through the steps to create a C# console project in Visual Studio and use IronZIP to password-protect a zip file. Open Visual Studio. Create a new C# Console Application project. Name your project and choose a location. From Additional Information, select the latest version of the .NET Framework. IronZIP supports the latest 8.0 .NET Framework. Click "Create" to generate the project. Installing IronZIP To use IronZIP in your project, you need to install the library. You can do this using either the NuGet Package Manager or the Package Manager Console. Using NuGet Package Manager Right-click on your project in Solution Explorer. Select "Manage NuGet Packages..." Search for "IronZip" and click "Install." Using Package Manager Console Open the Package Manager Console. Run the following command: Install-Package IronZip Steps to Password Protect a Zip File Now that IronZIP is installed, you can proceed to password-protect a zip file using the library. Importing Required Libraries using IronZip; using IronZip.Enum; using IronZip; using IronZip.Enum; Imports IronZip Imports IronZip.Enum $vbLabelText $csharpLabel These lines import the necessary namespaces from the IronZIP library: IronZip contains the main classes and functionality, while IronZip.Enum includes enums used in the library. Main Program Class class Program { static void Main() { // Code execution starts here } } class Program { static void Main() { // Code execution starts here } } Friend Class Program Shared Sub Main() ' Code execution starts here End Sub End Class $vbLabelText $csharpLabel This is the main class of the program with the Main method where the code execution begins. Creating an Empty ZIP Archive using (var archive = new IronZipArchive(9)) { // Code within the 'using' block } using (var archive = new IronZipArchive(9)) { // Code within the 'using' block } Using archive = New IronZipArchive(9) ' Code within the 'using' block End Using $vbLabelText $csharpLabel The using statement ensures that the IronZipArchive object is disposed of properly after its use. It creates a new instance of IronZipArchive with the highest compression level (9). Password Protecting the ZIP Archive The following single line of code adds password protection to the ZIP archive: archive.Encrypt("P@ssw0rd", EncryptionMethods.Traditional); archive.Encrypt("P@ssw0rd", EncryptionMethods.Traditional); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel The Encrypt method is called on the archive object to password-protect the ZIP file. It takes two parameters: the password string ("P@ssw0rd") and the encryption method (EncryptionMethods.Traditional). IronZIP also provides AES128 and AES256 advanced password protection which is more secure and prevents manipulation of ZIP files. Adding Files to the ZIP Archive archive.Add("./assets/file1.txt"); archive.Add("./assets/image1.png"); archive.Add("./assets/file1.txt"); archive.Add("./assets/image1.png"); archive.Add("./assets/file1.txt") archive.Add("./assets/image1.png") $vbLabelText $csharpLabel The Add method is used to add files to the ZIP archive. In this example, one text file and one image file (file1.txt and image1.png) located in the "./assets/" directory are added to the archive. These are the files to be added: Exporting the ZIP Archive archive.SaveAs("output.zip"); archive.SaveAs("output.zip"); archive.SaveAs("output.zip") $vbLabelText $csharpLabel The SaveAs method is called to export the ZIP archive. It specifies the output filename as "output.zip". This creates the password-protected ZIP file with the specified content and password. Visit the code examples page to learn more about how to create, read, extract, and perform other ZIP file-related operations in C# using IronZIP. Here's the complete source code with separated string paths and a password property for better control: using IronZip; using IronZip.Enum; class Program { static void Main() { // Define password and file paths for the ZIP archive string password = "P@ssw0rd"; string filename = "./assets/file1.txt"; string imagename = "./assets/image1.png"; // Create a new ZIPArchive with the highest compression level using (var archive = new IronZipArchive(9)) { // Add Password to protect the ZIP (Support AES128 & AES256) archive.Encrypt(password, EncryptionMethods.Traditional); // Add files to the archive archive.Add(filename); archive.Add(imagename); // Export the Encrypted ZIP file archive archive.SaveAs("output.zip"); } } } using IronZip; using IronZip.Enum; class Program { static void Main() { // Define password and file paths for the ZIP archive string password = "P@ssw0rd"; string filename = "./assets/file1.txt"; string imagename = "./assets/image1.png"; // Create a new ZIPArchive with the highest compression level using (var archive = new IronZipArchive(9)) { // Add Password to protect the ZIP (Support AES128 & AES256) archive.Encrypt(password, EncryptionMethods.Traditional); // Add files to the archive archive.Add(filename); archive.Add(imagename); // Export the Encrypted ZIP file archive archive.SaveAs("output.zip"); } } } Imports IronZip Imports IronZip.Enum Friend Class Program Shared Sub Main() ' Define password and file paths for the ZIP archive Dim password As String = "P@ssw0rd" Dim filename As String = "./assets/file1.txt" Dim imagename As String = "./assets/image1.png" ' Create a new ZIPArchive with the highest compression level Using archive = New IronZipArchive(9) ' Add Password to protect the ZIP (Support AES128 & AES256) archive.Encrypt(password, EncryptionMethods.Traditional) ' Add files to the archive archive.Add(filename) archive.Add(imagename) ' Export the Encrypted ZIP file archive archive.SaveAs("output.zip") End Using End Sub End Class $vbLabelText $csharpLabel Output After running the program, you will have a password-protected single file named "output.zip" in your project directory, containing the specified files. Conclusion In this article, we explored the importance of password-protected ZIP files and introduced the IronZIP library as a powerful solution for handling ZIP archives in C# projects. We covered the detailed features of IronZIP, including compatibility, archive generation, editing capabilities, and easy installation steps. The library supports traditional and advanced encryption methods to protect the files from tampering. Finally, we walked through the steps to create a C# console project in Visual Studio, install IronZIP, and password-protect a ZIP file. IronZIP simplifies the process of working with ZIP files in C# applications, providing developers with a robust toolset for archive management and security. Incorporating IronZIP into your projects allows you to enhance data protection when dealing with sensitive information in ZIP archives. For more detailed information on IronZIP and its capabilities, please visit the official documentation page. IronZIP offers a free trial for prolonged usage. Its lite package starts from $799. 常見問題解答 如何在C#中創建密碼保護的ZIP文件? 您可以使用IronZIP庫在C#中創建密碼保護的ZIP文件。首先,通過NuGet安裝庫,然後創建一個IronZipArchive對象,使用Encrypt方法添加密碼,將文件添加到存檔中,並使用SaveAs保存存檔。 有哪些加密選項可用於保護ZIP文件? IronZIP提供傳統、AES128和AES256加密方法來保護ZIP文件。這些選項提供不同級別的安全性來保護ZIP存檔中的敏感數據。 IronZIP與多個.NET版本兼容嗎? 是的,IronZIP與.NET 8、7、6、5、Core、Standard和Framework兼容,使其成為開發者在不同.NET環境中工作的多用途選擇。 如何在我的項目中安裝IronZIP? 您可以使用Visual Studio中的NuGet包管理器安裝IronZIP。在包管理器中搜尋「IronZip」並將其添加到您的項目中以開始管理ZIP文件。 IronZIP可以與C#以外的程式語言一起使用嗎? 是的,IronZIP除了與C#兼容外,也兼容VB.NET和F#,允許開發者在各種.NET語言應用程序中使用。 設置C#控制台應用程序以進行ZIP文件管理需要哪些步驟? 要使用IronZIP設置C#控制台應用程序以進行ZIP文件管理,在Visual Studio中創建一個新的控制台項目,通過NuGet安裝IronZIP,並遵循庫的文檔以添加ZIP文件功能。 使用IronZIP處理ZIP文件的主要好處是什麼? IronZIP通過提供易於使用的API、跨平台支持以及密碼保護和多種存檔格式支持等功能來簡化ZIP文件的管理,同時增強功能性和數據安全性。 密碼保護如何增強ZIP文件的安全性? 密碼保護確保只有授權人員才能訪問ZIP文件的內容,為存儲在存檔中的敏感數據添加了一個關鍵的安全層。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 6月 22, 2025 如何在 C# 中將 Zip 存檔提取到目錄 ZIP 文件是一種將多個文件和目錄打包成單個存檔的方便方式。 閱讀更多 更新日期 7月 28, 2025 如何在 C# 中將文件解壓到目錄 無論你在開發Windows 應用程序還是 .NET 項目,了解解壓文件的過程都是非常有價值的 閱讀更多 更新日期 7月 28, 2025 如何使用 C# 壓縮文件夾中的文件 ZIP 文件是使用 ZIP 格式包含一個或多個壓縮文件或文件夾的文件。這是一種常見的壓縮和封存多個文件或文件夾到單一文件的方法。 閱讀更多 如何在 VB.NET 中提取 Zip 文件如何在 C# 中將文件解壓到目錄
更新日期 7月 28, 2025 如何使用 C# 壓縮文件夾中的文件 ZIP 文件是使用 ZIP 格式包含一個或多個壓縮文件或文件夾的文件。這是一種常見的壓縮和封存多個文件或文件夾到單一文件的方法。 閱讀更多