使用 IRONZIP 如何使用 C# 压缩文件夹中的文件 Curtis Chau 已更新:七月 28, 2025 下载 IronZIP NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 ZIP 文件是使用 ZIP 格式包含一个或多个压缩文件或文件夹的文件。 它是一种常见的方法,可以将多个文件或文件夹压缩并归档为单个文件。 它们可以减少数据的大小,节省磁盘空间,并且使通过互联网传输文件变得更容易。 在本文章中,您将学习如何使用 ZIP 文件以及 C# 使用 IronZIP 库。 您将看到如何以编程方式创建、读取、提取和更新 ZIP 文件,以及如何使用 IronZIP 的各种功能,例如加密、密码保护和压缩级别。 到本文结束时,您将能够使用 IronZIP 轻松处理 C# 应用程序中的 ZIP 文件。 我们将在本文中介绍的内容 在我们的项目中安装 IronZIP 创建 ZIP 文件 创建密码保护的 ZIP 文件 提取 ZIP 文件 提取密码保护的 ZIP 文件 访问现有的 ZIP 归档 什么是 IronZIP? IronZIP 是一个强大而多功能的 C# ZIP 归档库,允许您以编程方式创建、读取和提取 ZIP 文件。 它支持各种归档格式,如 ZIP、TAR、GZIP 和 BZIP2。 它还支持 密码保护、加密和压缩级别。 IronZIP 兼容 .NET 8、7、6、Core、Standard 和 Framework。 IronZIP 可以帮助您处理使用 ZIP 文件的各种用例和好处,例如: 创建备份系统:您可以使用 IronZIP 将重要的文件和文件夹压缩并加密为 ZIP 存档,并将它们存储在安全的位置。 通过这种方式,您可以节约磁盘空间,并防止未经授权的访问。 发送电子邮件附件:您可以使用 IronZIP 将电子邮件附件压缩成 ZIP 文件,以减少其大小。 这可以帮助您避免超出文件大小限制并加快传输过程。 从网络下载文件:您可以使用 IronZIP 从网络下载和解压缩 ZIP 文件,例如软件包、文档、图像,及其他类型的文件。 这可以帮助您节省带宽和时间,并轻松访问所需的文件。 IronZIP 入门 在编写代码之前,您需要在 C# 项目中安装 IronZIP NuGet 包。 IronZIP 是一个通过 NuGet 提供的流行压缩库。 安装 IronZIP 库 要安装 IronZIP,您可以使用 Visual Studio 中的 NuGet 包管理器控制台。 只需运行以下命令: Install-Package IronZip 另外,您可以直接从官方 IronZIP 网站下载该软件包。安装后,您可以通过将以下命名空间添加到 C# 代码的顶部来开始。 using IronZip; using IronZip; Imports IronZip $vbLabelText $csharpLabel 在文件夹中创建 C# ZIP 文件 您可以使用 IronZIP 轻松在文件夹中创建 ZIP 文件。 以下代码将对指定目录中的所有文件进行压缩。 using System; using System.IO; using IronZip; class Program { static void Main(string[] args) { // Get all files from the specified directory string[] fileArray = Directory.GetFiles(@"D:\Docs\"); // Create a new ZIP archive using (var archive = new IronZipArchive()) { // Iterate through each file and add it to the archive foreach (var file in fileArray) { archive.Add(file); // Add files to the ZIP } // Save the archive to a file archive.SaveAs("myZipFile.zip"); } } } using System; using System.IO; using IronZip; class Program { static void Main(string[] args) { // Get all files from the specified directory string[] fileArray = Directory.GetFiles(@"D:\Docs\"); // Create a new ZIP archive using (var archive = new IronZipArchive()) { // Iterate through each file and add it to the archive foreach (var file in fileArray) { archive.Add(file); // Add files to the ZIP } // Save the archive to a file archive.SaveAs("myZipFile.zip"); } } } Imports System Imports System.IO Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Get all files from the specified directory Dim fileArray() As String = Directory.GetFiles("D:\Docs\") ' Create a new ZIP archive Using archive = New IronZipArchive() ' Iterate through each file and add it to the archive For Each file In fileArray archive.Add(file) ' Add files to the ZIP Next file ' Save the archive to a file archive.SaveAs("myZipFile.zip") End Using End Sub End Class $vbLabelText $csharpLabel 上述 C# 代码使用 IronZIP 库将所有文件压缩为一个 ZIP 文件。 代码执行以下操作: 声明一个名为 fileArray 的字符串数组,并将其赋值为 Directory.GetFiles 方法的结果,将目录路径("D:\Docs")作为参数传递。 此方法返回一个字符串数组,包含指定目录中所有文件的全名。 创建 IronZipArchive 类的新实例,它代表内存中的一个 ZIP 归档。 该实例被分配给名为 archive 的变量,并包装在 using 语句中,确保当代码块结束时处理该 ZIP 归档。 使用 foreach 循环遍历 fileArray 数组,并为每个文件调用 archive 对象的 Add 方法,将文件名作为参数传递。 此方法向 ZIP 归档中添加一个新条目,名称和内容与文件相同。 调用 archive 对象的 SaveAs 方法,将 ZIP 文件的名称("myZipFile.zip")作为参数传递。 此方法将 ZIP 归档保存为当前文件系统中的文件。 通过这种方式,您可以使用少量代码轻松创建新的 ZIP 归档。 输出 输出如下: 创建密码保护的 ZIP 文件 IronZIP 提供了一种简单的方法来创建密码保护的 ZIP 文件。 以下代码示例将压缩文件,并创建一个具有密码的新的 ZIP 文件。 using System; using System.IO; using IronZip; class Program { static void Main(string[] args) { // Get all files from the specified directory string[] fileArray = Directory.GetFiles(@"D:\Docs\"); // Create a new ZIP archive using (var archive = new IronZipArchive()) { // Iterate through each file and add it to the archive foreach (var file in fileArray) { archive.Add(file); // Add files to the ZIP } // Set Password and Encryption Method archive.Encrypt("myPa55word", EncryptionMethods.AES256); // Save the archive to a file archive.SaveAs("myZipFile.zip"); } } } using System; using System.IO; using IronZip; class Program { static void Main(string[] args) { // Get all files from the specified directory string[] fileArray = Directory.GetFiles(@"D:\Docs\"); // Create a new ZIP archive using (var archive = new IronZipArchive()) { // Iterate through each file and add it to the archive foreach (var file in fileArray) { archive.Add(file); // Add files to the ZIP } // Set Password and Encryption Method archive.Encrypt("myPa55word", EncryptionMethods.AES256); // Save the archive to a file archive.SaveAs("myZipFile.zip"); } } } Imports System Imports System.IO Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Get all files from the specified directory Dim fileArray() As String = Directory.GetFiles("D:\Docs\") ' Create a new ZIP archive Using archive = New IronZipArchive() ' Iterate through each file and add it to the archive For Each file In fileArray archive.Add(file) ' Add files to the ZIP Next file ' Set Password and Encryption Method archive.Encrypt("myPa55word", EncryptionMethods.AES256) ' Save the archive to a file archive.SaveAs("myZipFile.zip") End Using End Sub End Class $vbLabelText $csharpLabel 行 archive.Encrypt("myPa55word", EncryptionMethods.AES256); 为一个 ZIP 归档设置一个密码("myPa55word"),使用 IronZIP。通过对归档应用 AES-256 加密,增强安全性,确保只有具有正确密码的用户才能访问其内容。 此功能对于在 C# 应用程序中存储或传输时保护敏感数据非常有价值。 你需要在第二个参数中传递该加密算法的指定模式。 文件加密如下所示。 输出 我们已经看到了通过遍历指定路径的目录来创建 ZIP 文件的演示。 现在,让我们朝解压缩文件的例子前进。 从 ZIP 归档中提取文件 IronZIP 提供了一种方法来从 C# 中的 ZIP 归档中提取文件。 以下代码示例将从 ZIP 归档中提取压缩文件。 using IronZip; class Program { static void Main(string[] args) { // Extract all files from the ZIP archive to the specified directory IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles"); } } using IronZip; class Program { static void Main(string[] args) { // Extract all files from the ZIP archive to the specified directory IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles"); } } Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Extract all files from the ZIP archive to the specified directory IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles") End Sub End Class $vbLabelText $csharpLabel 代码 IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles"); 使用 IronZIP 从 "myZipFile.zip" 提取所有文件并将其放入 "myExtractedFiles" 目录。 这个简洁的方法简化了 C# 中提取 ZIP 归档的过程,提供了一种便利的文件提取解决方案。 输出 输出如下: 如何从密码保护的 ZIP 文件中提取 IronZIP 还提供了一种从密码保护的 ZIP 文件中提取的方法。 以下代码将使用 IronZIP 方法提取来自指定 ZIP 文件的所有现有文件和目录。 using IronZip; class Program { static void Main(string[] args) { // Extract all files from the password-protected ZIP archive to the specified directory IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word"); } } using IronZip; class Program { static void Main(string[] args) { // Extract all files from the password-protected ZIP archive to the specified directory IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word"); } } Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Extract all files from the password-protected ZIP archive to the specified directory IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word") End Sub End Class $vbLabelText $csharpLabel ExtractArchiveToDirectory 方法从 IronZipArchive 类将所有条目提取到指定目录的 ZIP 归档。 它为该方法传递三个参数:ZIP 文件的路径 ('myZipFile.zip')、目标目录的路径 ('myExtractedFiles') 和 ZIP 文件的密码 ('myPa55word')。 通过这种方式,您可以轻松地提取密码保护的 ZIP 文件。 如何访问现有归档 IronZIP 提供方法 访问现有归档 并查看文件中存在的所有条目。 using System; using System.Collections.Generic; using IronZip; class Program { static void Main(string[] args) { // Open an existing ZIP archive with a password using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word")) { // Get entries list List<string> names = archive.GetArchiveEntryNames(); // Iterate through each entry name and print it foreach (string name in names) { Console.WriteLine(name); } } } } using System; using System.Collections.Generic; using IronZip; class Program { static void Main(string[] args) { // Open an existing ZIP archive with a password using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word")) { // Get entries list List<string> names = archive.GetArchiveEntryNames(); // Iterate through each entry name and print it foreach (string name in names) { Console.WriteLine(name); } } } } Imports System Imports System.Collections.Generic Imports IronZip Friend Class Program Shared Sub Main(ByVal args() As String) ' Open an existing ZIP archive with a password Using archive = New IronZipArchive("myZipFile.zip", "myPa55word") ' Get entries list Dim names As List(Of String) = archive.GetArchiveEntryNames() ' Iterate through each entry name and print it For Each name As String In names Console.WriteLine(name) Next name End Using End Sub End Class $vbLabelText $csharpLabel 提供的 C# 代码使用 IronZIP 创建一个安全的 IronZipArchive 实例,通过加载名为 "myZipFile.zip" 的 ZIP 文件,密码为 "myPa55word"。 如果文件未加密,不要传递密码参数。 然后,它会检索并打印加密 ZIP 归档中的条目名称(文件和文件夹名称)列表。 GetArchiveEntryNames 方法收集条目名称,foreach 循环将每个名称输出到控制台。 这演示了 IronZIP 如何在简洁的方式下实现安全访问和检索密码保护的 ZIP 归档中的条目信息。 输出 结论 综上所述,IronZIP 证明是一个强大且多功能的 C# 库,用于处理 ZIP 文件。 它的功能超越了基本的 压缩 和 提取,提供了如 密码保护、加密和与各种归档格式兼容等功能。 无论您是在创建备份系统,管理电子邮件附件,还是从网络下载文件,IronZIP 都以简单和高效的方式简化了这些任务。 通过将 IronZIP 集成到您的 C# 应用程序中,您可以获得一个强大的工具来处理 ZIP 文件,提高数据安全性和优化文件传输流程。 您可以根据需要享受免费的 试用。 常见问题解答 如何在 C# 中从文件夹创建 ZIP 文件? 您可以使用 IronZIP 通过遍历目录中的文件并将它们添加到新的 ZIP 存档来从文件夹创建 ZIP 文件。使用 IronZipArchive 类并调用 SaveAs 方法保存 ZIP 文件。 如何在 C# 项目中安装 IronZIP? 通过使用 Visual Studio 中的 NuGet 包管理器在 C# 项目中安装 IronZIP。在程序包管理器控制台中运行命令 Install-Package IronZip,或从 IronZIP 官方网站下载。 IronZIP 支持哪些格式的 ZIP 文件处理? IronZIP 支持包括 ZIP、TAR、GZIP 和 BZIP2 在内的各种存档格式,为不同的压缩和存档需求提供了灵活性。 我可以在 C# 中加密创建的 ZIP 文件吗? 是的,您可以通过使用 AES-256 加密与 IronZIP 的 Encrypt 方法来加密 ZIP 文件,以确保存档中的数据安全。 如何在 C# 中从 ZIP 文件提取文件? 使用 IronZIP 从 ZIP 存档中提取文件时,使用 ExtractArchiveToDirectory 方法,指定源 ZIP 文件和目标目录。 是否可以在 C# 中处理有密码保护的 ZIP 文件? 是的,您可以通过在使用如 ExtractArchiveToDirectory 这样的方法时提供密码来使用 IronZIP 处理有密码保护的 ZIP 文件,以安全地访问内容。 使用 IronZIP 管理 ZIP 文件有哪些优势? IronZIP 通过提供加密、密码保护和支持多种存档格式等功能,简化了文件备份、电子邮件附件管理和网页下载等任务。 IronZIP 支持 .NET 8 和其他版本吗? IronZIP 与 .NET 8、7、6、Core、Standard 和 Framework 兼容,使其可灵活集成到各种 C# 项目中。 开发人员如何访问 IronZIP 的试用版? 开发人员可以访问 IronZIP 网站的许可或下载部分来获取免费试用版本,以评估其功能。 使用 ZIP 文件进行数据传输有什么好处? 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 归档文件提取到目录如何在 VB .NET 中提取 ZIP 文件