IronZIP入门指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronZIP:您的 .NET 一体化归档库

IronZIP是由 Iron Software 开发的归档压缩和解压缩库。 除了广泛使用的 ZIP 格式外,它还可以处理 TAR、GZIP 和 BZIP2 格式。

C# 归档压缩和解压缩库

1.下载用于文件压缩和解压缩的 C# 库

  1. 支持 ZIP、TAR、GZIP 和 BZIP2 格式
  2. 可自定义压缩级别,范围从 0 到 9
  3. 从压缩文件中提取内容
  4. 将文件追加到现有 ZIP 压缩包并生成新的 ZIP 文件

兼容性

IronZIP具有跨平台兼容性,支持以下平台:

.NET 版本支持:

  • C#VB.NETF#
  • .NET 7、6、5和 Core 3.1+
  • .NET Standard (2.0+)
  • .NET Framework (4.6.2+)

操作系统和环境支持:

  • Windows (10+,Server 2016+)
  • Linux (Ubuntu、Debian、CentOS 等)
  • macOS (10+)
  • iOS (12+)
  • Android API 21+(v5"棒棒糖") Docker (Windows、Linux、Azure)
  • Azure (VPS、WebApp、函数)
  • AWS (EC2、Lambda)

.NET 项目类型支持:

  • Web (Blazor 和 WebForms) -移动端(Xamarin & MAUI) -桌面(WPF 和 MAUI) -控制台(应用和库)

安装

IronZIP库

要安装 IronZIP 软件包,请在终端或软件包管理器控制台中使用以下命令:

Install-Package IronZip

或者,直接从IronZIP NuGet 官方网站下载。

安装完成后,您可以通过在 C# 代码顶部添加using IronZip;来开始使用。

应用许可证密钥

接下来,通过将许可证密钥分配给 License 类的 LicenseKey 属性,将有效的许可证密钥或试用密钥应用于 IronZIP。 在导入语句之后、使用任何 IronZIP 方法之前,添加以下代码:

using IronZip;

// Apply your license key here
IronZip.License.LicenseKey = "YOUR_LICENSE_KEY";
using IronZip;

// Apply your license key here
IronZip.License.LicenseKey = "YOUR_LICENSE_KEY";
Imports IronZip

' Apply your license key here
IronZip.License.LicenseKey = "YOUR_LICENSE_KEY"
$vbLabelText   $csharpLabel

代码示例

创建存档示例

使用using语句创建 ZIP 文件。 在 using 代码块中,使用AddArchiveEntry方法将文件导入 ZIP 文件。最后,使用SaveAs方法导出 ZIP 文件。

using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Create a new ZIP file
        using (var archive = new ZipArchive())
        {
            // Add a file to the archive
            archive.AddArchiveEntry("example.txt", File.ReadAllBytes("path/to/example.txt"));

            // Save the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Create a new ZIP file
        using (var archive = new ZipArchive())
        {
            // Add a file to the archive
            archive.AddArchiveEntry("example.txt", File.ReadAllBytes("path/to/example.txt"));

            // Save the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
Imports IronZip
Imports System.IO

Friend Class Program
	Shared Sub Main()
		' Create a new ZIP file
		Using archive = New ZipArchive()
			' Add a file to the archive
			archive.AddArchiveEntry("example.txt", File.ReadAllBytes("path/to/example.txt"))

			' Save the ZIP archive
			archive.SaveAs("archive.zip")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

将压缩文件解压到文件夹

使用ExtractArchiveToDirectory方法从 ZIP 文件中提取内容。 指定目标 ZIP 文件的路径和解压目录。

using IronZip;

class Program
{
    static void Main()
    {
        // path to the ZIP file and extraction directory
        string zipPath = "archive.zip";
        string extractPath = "extracted/";

        // Extract all files in the ZIP archive to the specified directory
        using (var archive = new ZipArchive(zipPath))
        {
            archive.ExtractArchiveToDirectory(extractPath);
        }
    }
}
using IronZip;

class Program
{
    static void Main()
    {
        // path to the ZIP file and extraction directory
        string zipPath = "archive.zip";
        string extractPath = "extracted/";

        // Extract all files in the ZIP archive to the specified directory
        using (var archive = new ZipArchive(zipPath))
        {
            archive.ExtractArchiveToDirectory(extractPath);
        }
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main()
		' path to the ZIP file and extraction directory
		Dim zipPath As String = "archive.zip"
		Dim extractPath As String = "extracted/"

		' Extract all files in the ZIP archive to the specified directory
		Using archive = New ZipArchive(zipPath)
			archive.ExtractArchiveToDirectory(extractPath)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

将文件添加到现有存档

将 ZIP 文件路径传递给构造函数即可打开该 ZIP 文件。使用相同的AddArchiveEntry方法向打开的 ZIP 文件添加文件,并使用SaveAs方法导出该文件。

using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Open an existing ZIP file
        using (var archive = new ZipArchive("archive.zip"))
        {
            // Add more files to the archive
            archive.AddArchiveEntry("anotherfile.txt", File.ReadAllBytes("path/to/anotherfile.txt"));

            // Save updates to the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
using IronZip;
using System.IO;

class Program
{
    static void Main()
    {
        // Open an existing ZIP file
        using (var archive = new ZipArchive("archive.zip"))
        {
            // Add more files to the archive
            archive.AddArchiveEntry("anotherfile.txt", File.ReadAllBytes("path/to/anotherfile.txt"));

            // Save updates to the ZIP archive
            archive.SaveAs("archive.zip");
        }
    }
}
Imports IronZip
Imports System.IO

Friend Class Program
	Shared Sub Main()
		' Open an existing ZIP file
		Using archive = New ZipArchive("archive.zip")
			' Add more files to the archive
			archive.AddArchiveEntry("anotherfile.txt", File.ReadAllBytes("path/to/anotherfile.txt"))

			' Save updates to the ZIP archive
			archive.SaveAs("archive.zip")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

可用的许可和支持

IronZIP是一个付费库,但这里也提供免费试用许可证。

有关 Iron Software 的更多信息,请访问我们的网站:https://ironsoftware.com/ 如需更多支持或有任何疑问,请联系我们的团队

Iron Software 提供的支持

如需一般支持和技术咨询,请发送电子邮件至:support@ironsoftware.com

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,647 | Version: 2025.11 刚刚发布