使用 IRONZIP 如何在 C# 中打开 ZIP 文件 Curtis Chau 已更新:六月 22, 2025 下载 IronZIP NuGet 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 ZIP 是一种归档条目文件系统格式,支持无损数据压缩。 一个 ZIP 文件可以包含一个或多个已压缩的文件或目录。 ZIP 文件格式允许使用几种压缩算法,但 DEFLATE 是其中最常见的。 随即,许多软件工具快速地支持了 ZIP 格式。 主要操作系统供应商长期以来均已包含 ZIP 文件支持。微软从 Windows 98 开始加入 ZIP 文件支持,其他操作系统也随之效仿。 在这篇博客中,我们将探讨使用IronZIP打开 ZIP 死案或提取文件的一种现代、简单且高效的方法。 我们将了解 ZIP 文件的一般情况及其优势。 然后,我们将看到系统命名空间中可用于 ZIP 文件格式的选项。 然后我们将探索逐步说明如何打开 ZIP 文件,将 ZIP 文件提取到临时文件夹,创建新的 ZIP 文件,并向现有 ZIP 文件添加文件。 在软件应用程序中使用 ZIP 文件的优势 压缩:此技术使用多种压缩算法,例如 Implode、Deflate、Deflate64、bzip2、LZMA、WavPack、PPMd 等,来减少归档文件/文件夹的大小。 减少传输时间:文件尺寸越小,传输时间越短,尤其是在通过互联网发送文件时。 这特别适用于邮件附件和从网站上传或下载文件。 文件整合:ZIP 文件使您可以将多个文件整合为一个单一的归档,从而减少需要管理的文件数量。 这对于组织项目或分发由多个文件组成的软件非常有用。 密码保护:许多 ZIP 工具提供密码保护归档文件的选项,为文件增加一层安全性。 当您想限制对 ZIP 文件内容的访问时,这非常有用。 使用 IronZIP 创建 ZIP 归档并提取 ZIP 文件 关于 IronZIP 库和文档的介绍可以在这里找到。 在 C# 应用程序中,可以通过多种方式创建和提取 ZIP 文件。 IronZIP NuGet 包具有将文件归档为不同格式的所有功能:ZIP、TAR、GZIP 和 BZIP2。以下是如何在现代应用程序编程中使用 IronZIP 来构建下一代应用程序以打开 ZIP 文件、提取 ZIP 文件、创建新的 ZIP 文件等的示范步骤。 步骤 1. 创建 .NET Core 控制台应用程序 创建项目 .NET 控制台应用程序可以使用 Visual Studio 创建。 打开 Visual Studio 并选择创建项目。 在此处,您可以看到创建项目的各种模板。 演示或测试代码的最简单方法是创建一个控制台应用程序。 我们将选择控制台应用程序项目模板。 输入项目名称 在下面的窗口中,您可以输入项目名称、项目在文件系统中存储的位置,最后一个是解决方案文件夹的路径。 您可以将解决方案和项目文件夹保持相同,或者将它们放在不同的文件夹中。 选择 .NET 框架版本 下一步是为项目选择 .NET 框架版本。 如果要在特定版本中开发,请指定您喜欢的版本。否则,始终选择最新稳定版本以创建项目。 最新版本可从微软网站下载。然后单击创建以生成控制台应用程序。 这将从模板创建默认项目,并将项目和解决方案文件存储在指定的目录中。 项目创建后,其外观将类似于下面这样。 在最新版本中,类有时不会在program.cs中使用。 // Import necessary namespaces using System; // Define the namespace namespace MyApp // Note: actual namespace depends on the project name. { // Define the Program class internal class Program { // Main method: Entry point of the application static void Main(string[] args) { // Print a welcome message Console.WriteLine("Hello, World!"); } } } // Import necessary namespaces using System; // Define the namespace namespace MyApp // Note: actual namespace depends on the project name. { // Define the Program class internal class Program { // Main method: Entry point of the application static void Main(string[] args) { // Print a welcome message Console.WriteLine("Hello, World!"); } } } ' Import necessary namespaces Imports System ' Define the namespace Namespace MyApp ' Note: actual namespace depends on the project name. ' Define the Program class Friend Class Program ' Main method: Entry point of the application Shared Sub Main(ByVal args() As String) ' Print a welcome message Console.WriteLine("Hello, World!") End Sub End Class End Namespace $vbLabelText $csharpLabel 要创建新的 ZIP 文件并提取所有 ZIP 归档文件,我们可以使用来自默认库的 System.IO.Compression。 以下代码演示如何使用 ZipFile.OpenRead 和 ZipFile.Open 静态方法打开 ZIP 文件或提取 ZIP 文件。 // Import necessary namespaces using System; using System.IO; using System.IO.Compression; public class ZipExample { public static void Main() { Console.WriteLine("-----------Zip - Unzip-----------"); // Method to add a file entry to the ZIP archive static void AddEntry(string filePath, ZipArchive zipArchive) { // Get file name from the file path var file = Path.GetFileName(filePath); // Create a new entry in the ZIP archive for the file zipArchive.CreateEntryFromFile(filePath, file); } // Name of the ZIP file to be created var zip = "myFile.zip"; // Open or create the ZIP file for updating using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Update)) { // Add files to the archive AddEntry("file1.txt", archive); AddEntry("file2.txt", archive); } // Directory where we want to extract the ZIP files var dirToExtract = "extract"; // Create the directory if it does not exist if (!Directory.Exists(dirToExtract)) { Directory.CreateDirectory(dirToExtract); } // Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract); // Indicate that extraction is complete Console.WriteLine("Files extracted to: " + dirToExtract); } } // Import necessary namespaces using System; using System.IO; using System.IO.Compression; public class ZipExample { public static void Main() { Console.WriteLine("-----------Zip - Unzip-----------"); // Method to add a file entry to the ZIP archive static void AddEntry(string filePath, ZipArchive zipArchive) { // Get file name from the file path var file = Path.GetFileName(filePath); // Create a new entry in the ZIP archive for the file zipArchive.CreateEntryFromFile(filePath, file); } // Name of the ZIP file to be created var zip = "myFile.zip"; // Open or create the ZIP file for updating using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Update)) { // Add files to the archive AddEntry("file1.txt", archive); AddEntry("file2.txt", archive); } // Directory where we want to extract the ZIP files var dirToExtract = "extract"; // Create the directory if it does not exist if (!Directory.Exists(dirToExtract)) { Directory.CreateDirectory(dirToExtract); } // Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract); // Indicate that extraction is complete Console.WriteLine("Files extracted to: " + dirToExtract); } } ' Import necessary namespaces Imports System Imports System.IO Imports System.IO.Compression Public Class ZipExample Public Shared Sub Main() Console.WriteLine("-----------Zip - Unzip-----------") ' Method to add a file entry to the ZIP archive 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' static void AddEntry(string filePath, ZipArchive zipArchive) ' { ' ' Get file name from the file path ' var file = Path.GetFileName(filePath); ' ' ' Create a new entry in the ZIP archive for the file ' zipArchive.CreateEntryFromFile(filePath, file); ' } ' Name of the ZIP file to be created Dim zip = "myFile.zip" ' Open or create the ZIP file for updating Using archive As ZipArchive = ZipFile.Open(zip, ZipArchiveMode.Update) ' Add files to the archive AddEntry("file1.txt", archive) AddEntry("file2.txt", archive) End Using ' Directory where we want to extract the ZIP files Dim dirToExtract = "extract" ' Create the directory if it does not exist If Not Directory.Exists(dirToExtract) Then Directory.CreateDirectory(dirToExtract) End If ' Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract) ' Indicate that extraction is complete Console.WriteLine("Files extracted to: " & dirToExtract) End Sub End Class $vbLabelText $csharpLabel 在上述代码中,使用了命名空间的 ZIP 文件名为myFile.zip。 使用 Open 方法以指定的模式打开 ZIP 文件。 这也可用于创建新的 ZIP 归档文件。 打开后,我们可以使用方法 AddEntry 添加新的归档条目,然后 ExtractToDirectory 提取 ZIP 归档文件到指定的目录。 如果目录不存在,则使用 Directory.CreateDirectory 创建。 步骤 2. 使用 NuGet 包管理器安装 IronZIP 从 Visual Studio 打开项目管理器并搜索 IronZIP 包。 然后选择最新版本并单击安装。 您可以从下拉列表更改要安装的版本。 然后点击安装。 使用 IronZIP 创建 ZIP 归档文件并添加文件 IronZIP 是由 Iron Software 开发的归档压缩和解压库。 除了广泛使用的 ZIP 格式外,它还可以处理 TAR、GZIP 和 BZIP2 格式。 IronZIP 是一个 C# ZIP 归档库,它优先考虑准确性、易用性和速度。 其用户友好的 API 使开发人员可以在几分钟内轻松地将归档功能添加到现代 .NET 项目中。 IronZIP 提供了许多相较于 System.IO.Compression 库的优势。 在压缩期间,您可以指定所需的压缩比,并且还可以使用不同的压缩算法,例如 ZIP、TAR、GZIP、BZIP2。它还支持移动、网络和桌面平台以及各种 .NET 版本。 // Setup: Specify the path for the new ZIP archive var archivePath = "ironZip.zip"; // Check if the archive already exists, and delete it if so if (File.Exists(archivePath)) { File.Delete(archivePath); } // Use IronZIP library to create a new ZIP archive using (var archive = new IronZipArchive(9)) // Compression level: 9 for maximum compression { // Add files to the ZIP archive archive.Add("file1.txt"); archive.Add("file2.txt"); // Save the archive to the specified path archive.SaveAs(archivePath); } // Setup: Specify the path for the new ZIP archive var archivePath = "ironZip.zip"; // Check if the archive already exists, and delete it if so if (File.Exists(archivePath)) { File.Delete(archivePath); } // Use IronZIP library to create a new ZIP archive using (var archive = new IronZipArchive(9)) // Compression level: 9 for maximum compression { // Add files to the ZIP archive archive.Add("file1.txt"); archive.Add("file2.txt"); // Save the archive to the specified path archive.SaveAs(archivePath); } ' Setup: Specify the path for the new ZIP archive Dim archivePath = "ironZip.zip" ' Check if the archive already exists, and delete it if so If File.Exists(archivePath) Then File.Delete(archivePath) End If ' Use IronZIP library to create a new ZIP archive Using archive = New IronZipArchive(9) ' Compression level: 9 for maximum compression ' Add files to the ZIP archive archive.Add("file1.txt") archive.Add("file2.txt") ' Save the archive to the specified path archive.SaveAs(archivePath) End Using $vbLabelText $csharpLabel 最初的源代码通过指定 ZIP 归档文件名并检查指定目录是否存在来设置。 然后我们使用 Add 方法进行文件归档以创建 ZIP 文件。 压缩参数中的第二个参数为 1 是最低,9 是最高。 文本文件可以使用 9 最大程度地无损压缩,图像文件可以使用较低压缩以避免数据丢失。 使用 IronZIP 打开 ZIP 归档文件 IronArchive 类也可以用于从 ZIP 归档文件中提取文件。所有文件都是使用 IronArchive.ExtractArchiveToDirectory 提取的,这有助于将所有文件提取到特定目录,如下所示。 // Directory to extract all files from the ZIP archive var extractionPath = "IronZipFiles"; // Check if the directory exists; if not, create it if (!Directory.Exists(extractionPath)) { Directory.CreateDirectory(extractionPath); } // Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath); // Directory to extract all files from the ZIP archive var extractionPath = "IronZipFiles"; // Check if the directory exists; if not, create it if (!Directory.Exists(extractionPath)) { Directory.CreateDirectory(extractionPath); } // Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath); ' Directory to extract all files from the ZIP archive Dim extractionPath = "IronZipFiles" ' Check if the directory exists; if not, create it If Not Directory.Exists(extractionPath) Then Directory.CreateDirectory(extractionPath) End If ' Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath) $vbLabelText $csharpLabel 上述代码将 ZIP 文件提取到目录。 代码检查目录是否存在,如果不存在,ZIP 归档文件会提取到指定目录。 许可(提供免费试用) 为了让上述代码工作,需要许可证密钥。 此密钥需要放置在appsettings.json中。 { "IronZip.LicenseKey": "your license key" } 开发人员可在此处注册后获得试用许可证,而且,试用许可证不需要信用卡。 可以提供电子邮件 ID 并注册免费试用。 将文件添加到现有 ZIP 归档文件 使用 IronZIP 的静态 FromFile 方法打开现有归档。该方法还要求指定将作为输出创建的新归档的文件名。 // Path to a new file to be added to the existing ZIP archive const string file3 = ".\\image3.png"; var archivePlusPath = "ironZipPlus.zip"; // Open the existing ZIP archive and add a new file using (var file = IronArchive.FromFile("ironZip.zip", archivePlusPath)) { // Add additional files to the existing archive file.Add(file3); } // Path to a new file to be added to the existing ZIP archive const string file3 = ".\\image3.png"; var archivePlusPath = "ironZipPlus.zip"; // Open the existing ZIP archive and add a new file using (var file = IronArchive.FromFile("ironZip.zip", archivePlusPath)) { // Add additional files to the existing archive file.Add(file3); } ' Path to a new file to be added to the existing ZIP archive Const file3 As String = ".\image3.png" Dim archivePlusPath = "ironZipPlus.zip" ' Open the existing ZIP archive and add a new file Using file = IronArchive.FromFile("ironZip.zip", archivePlusPath) ' Add additional files to the existing archive file.Add(file3) End Using $vbLabelText $csharpLabel 代码使用静态方法 IronArchive.FromFile 打开现有 ZIP 文件,然后将新文件作为归档条目添加。 文件对象处理完毕后,更新的归档将成功保存。 更新 IronZIP 库根据客户/用户的反馈不断更新,所有更新可在此处找到。 结论 总之,ZIP 文件编程是现代应用开发中构建的一个重要技能,其中存储和数据传输成本由云托管服务商收取。 掌握这一技能可以帮助程序员降低应用成本并提高应用性能。 通过遵循安装步骤并探索提供的代码示例,开发人员可以快速利用 IronZIP 的强大功能轻松处理 ZIP 任务。 随着越来越多的应用程序进行现代化改造并迁移到云端,拥有像 IronZIP 这样的可靠 ZIP 库为 C# 开发人员提供了满足现代应用开发需求的必要工具。 因此,拥抱 IronZIP 的力量,为 C# 应用程序中处理 ZIP 文件开启新的可能性。 IronZIP 为开发人员提供广泛的支持。 若要了解更多关于 IronZIP 的工作原理,访问此处。 IronZIP 提供免费的试用许可证,这是了解 IronZIP 及其功能的绝佳机会。 Iron Software 有各种其他库,探索它们以获得更多知识并更新您的技能,以编写/开发现代应用程序。 常见问题解答 如何在 C# 中打开一个 ZIP 文件? 您可以使用 System.IO.Compression 库在 C# 中打开 ZIP 文件。或者,IronZIP 提供了处理 ZIP 文件的高级功能,提供了一种更简单、更高效的管理您的存档方式。 如何使用 C# 从 ZIP 存档中提取文件? 使用 IronZIP,您可以使用 IronArchive.ExtractArchiveToDirectory 方法从 ZIP 存档中提取文件。此方法要求您指定 ZIP 文件和提取的目标目录。 ZIP 文件在应用程序开发中的好处是什么? ZIP 文件可减少文件大小,从而实现更快的传输时间,并能够将多个文件合并到一个存档中。IronZIP 增强了这些好处,具有诸如指定压缩比和支持多种格式的附加功能。 如何在 C# 中向现有 ZIP 存档添加文件? 要使用 IronZIP 向现有 ZIP 存档添加文件,请利用 IronArchive.FromFile 方法打开存档,然后使用 Add 方法包含新文件。保存更新后的存档以完成过程。 我可以使用 IronZIP 创建一个新的 ZIP 文件并向其添加文件吗? 是的,使用 IronZIP,您可以通过指定存档路径和使用 Add 方法添加文件来创建一个新的 ZIP 文件。然后使用 SaveAs 方法保存存档。 如何使用 NuGet 程序包管理器安装 IronZIP? 要通过 NuGet 程序包管理器安装 IronZIP,请打开 Visual Studio 中的项目管理器,搜索 IronZIP,选择最新版本,然后点击安装。这将把 IronZIP 添加到您的项目中,从而启用 ZIP 文件管理。 IronZIP 是否支持多种压缩格式? 是的,IronZIP 支持多种压缩格式,包括 ZIP、TAR、GZIP 和 BZIP2,为各种应用需求提供灵活性。 IronZIP 是否提供免费试用版? 是的,IronZIP 为开发人员提供免费试用。您可以在 Iron Software 网站上注册以访问试用版,无需信用卡。 是什么让 IronZIP 成为现代应用程序开发的合适选择? IronZIP 以其易用性、速度和跨平台兼容性而闻名。这些功能,以及其先进的功能,使其成为现代应用程序开发的理想选择。 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 项目,理解文件解压过程都非常有价值。 阅读更多 如何在 .NET Core 中解压缩文件如何在 C# .NET Core 中创建 ZI...