跳過到頁腳內容
.NET幫助

SevenZip C#(開發者的工作原理)

在文件输入压缩和归档工具领域,7-Zip作为一个多功能的开源解决方案脱颖而出。 由于其高压缩比和对各种归档格式的支持,7-Zip已成为用户寻求高效文件管理的热门选择。 在本文中,我们将探讨7-Zip是什么、如何工作、其主要功能、独特的7z格式、压缩比以及其用于C#的.NET SDK,帮助创建7z归档文件。 此外,我们将介绍IronZIP作为.NET生态系统中的替代解决方案。

什么是7-Zip?

7-Zip是一个免费和开源的文件归档工具,允许用户压缩和解压文件。 由Igor Pavlov开发的7-Zip支持广泛的压缩格式,使其成为管理和组织数据的多功能工具。

Sevenzip C# (开发者如何使用) 图1 - 7-Zip

主要功能

  • 高压缩比:7-Zip的关键特点之一是能够实现高压缩比,通常超过其他归档工具。 这可以显著节省存储空间和加快文件传输到文件夹的速度。
  • 广泛格式支持:7-Zip可以处理多种归档格式,包括7z、ZIP、TAR、GZIP等。 这种多功能性确保了与不同操作系统和软件的兼容性。
  • 加密和安全性:7-Zip提供强大的加密能力,允许用户使用AES-256加密保护他们的归档文件。 这确保了敏感数据的安全。
  • 命令行支持:除了其用户友好的图形界面,7-Zip还提供命令行版本,适用于倾向于在文件管理任务中进行自动化和脚本编写的用户。

工作原理

7-Zip利用先进的压缩算法来减小文件和文件夹的大小。 它采用LZMA(Lempel-Ziv-Markov链算法)压缩算法用于其本地7z格式,这有助于其出色的压缩比。 该工具还支持其他常见格式,如ZIP、TAR和GZIP。

7z格式

7z格式是7-Zip用于其归档文件的专有格式。 它采用LZMA压缩算法,以其极佳的压缩比而闻名。 7z格式支持固态压缩、文件分割和自解压归档等功能。

压缩比

7-Zip以其出色的压缩比而闻名,尤其是在使用7z格式和LZMA算法时。 这种效率使得归档文件的大小更小,但不损害压缩文件的完整性。 由7-Zip创建的文件比普通ZIP格式压缩效果好30-70%。

7-Zip LZMA SDK for C#

对于使用C#的开发人员,7-Zip提供了一个.NET SDK,使得7-Zip功能可以无缝集成到自定义应用程序中。 SDK允许开发人员以编程方式执行压缩和解压操作,从而在C#项目中灵活管理归档文件。

如果你想在C#应用程序中使用7-Zip,可以使用7-Zip SDK或在C#代码中利用命令行可执行文件。 下面是两种方法的简要概述。

1. 7-Zip SDK (7z.dll)

7-Zip SDK包含7z.dll库,你可以在C#项目中使用。 这种方法允许你以编程方式执行压缩和解压操作。

以下是使用7-Zip SDK的示例源代码:

using SevenZip;

class Program
{
    static void Main()
    {
        // Specify the path to the 7z.dll library
        SevenZipBase.SetLibraryPath("path_to_7z.dll");

        // Example: Extract files from an archive
        using (var extractor = new SevenZipExtractor("archive.7z"))
        {
            extractor.ExtractArchive("output_directory");
        }

        // Example: Compress files into an archive
        using (var compressor = new SevenZipCompressor())
        {
            // Add files to the archive
            compressor.CompressFiles("archive.7z", "file1.txt", "file2.txt");
        }
    }
}
using SevenZip;

class Program
{
    static void Main()
    {
        // Specify the path to the 7z.dll library
        SevenZipBase.SetLibraryPath("path_to_7z.dll");

        // Example: Extract files from an archive
        using (var extractor = new SevenZipExtractor("archive.7z"))
        {
            extractor.ExtractArchive("output_directory");
        }

        // Example: Compress files into an archive
        using (var compressor = new SevenZipCompressor())
        {
            // Add files to the archive
            compressor.CompressFiles("archive.7z", "file1.txt", "file2.txt");
        }
    }
}
Imports SevenZip

Friend Class Program
	Shared Sub Main()
		' Specify the path to the 7z.dll library
		SevenZipBase.SetLibraryPath("path_to_7z.dll")

		' Example: Extract files from an archive
		Using extractor = New SevenZipExtractor("archive.7z")
			extractor.ExtractArchive("output_directory")
		End Using

		' Example: Compress files into an archive
		Using compressor = New SevenZipCompressor()
			' Add files to the archive
			compressor.CompressFiles("archive.7z", "file1.txt", "file2.txt")
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

确保将"path_to_7z.dll"替换为7z.dll库的实际路径。 你可以在7-Zip安装目录中找到7z.dll文件。

2. 命令行可执行文件

或者,你可以在C#源代码中使用7-Zip命令行可执行文件(7z.exe),通过System.Diagnostics.Process类调用它。

using System.Diagnostics;

class Program
{
    static void Main()
    {
        // Example: Extract files from an archive using the command-line executable
        string archivePath = "archive.7z";
        string outputPath = "output_directory";
        ProcessStartInfo processStartInfo = new ProcessStartInfo
        {
            FileName = "7z.exe",
            Arguments = $"x \"{archivePath}\" -o\"{outputPath}\"",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        using (Process process = new Process { StartInfo = processStartInfo })
        {
            process.Start();
            process.WaitForExit();
        }
    }
}
using System.Diagnostics;

class Program
{
    static void Main()
    {
        // Example: Extract files from an archive using the command-line executable
        string archivePath = "archive.7z";
        string outputPath = "output_directory";
        ProcessStartInfo processStartInfo = new ProcessStartInfo
        {
            FileName = "7z.exe",
            Arguments = $"x \"{archivePath}\" -o\"{outputPath}\"",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        using (Process process = new Process { StartInfo = processStartInfo })
        {
            process.Start();
            process.WaitForExit();
        }
    }
}
Imports System.Diagnostics

Friend Class Program
	Shared Sub Main()
		' Example: Extract files from an archive using the command-line executable
		Dim archivePath As String = "archive.7z"
		Dim outputPath As String = "output_directory"
		Dim processStartInfo As New ProcessStartInfo With {
			.FileName = "7z.exe",
			.Arguments = $"x ""{archivePath}"" -o""{outputPath}""",
			.RedirectStandardOutput = True,
			.UseShellExecute = False,
			.CreateNoWindow = True
		}
		Using process As New Process With {.StartInfo = processStartInfo}
			process.Start()
			process.WaitForExit()
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

确保"7z.exe"在你的系统PATH中,或在FileName属性中提供可执行文件的完整路径。

介绍IronZIP

虽然7-Zip是一个强大的解决方案,但在.NET生态系统中寻找替代方案的开发人员可能会发现IronZIP是一个具有吸引力的选择。IronZIP是一个.NET压缩库,提供与7-Zip类似的功能,为开发人员提供压缩、解压和在他们的C#应用程序中处理归档文件的工具。

Sevenzip C# (开发者如何使用) 图2 - IronZIP

IronZIP是一个功能强大的C# ZIP归档库,它简化了在.NET应用程序中处理ZIP文件的工作。 凭借其用户友好的API,开发人员可以高效地使用IronZIP创建、读取和提取ZIP归档文件。 这里是一个简单的代码示例,展示了使用IronZIP创建ZIP归档文件的简便性:

using IronZip;

class Program
{
    static void Main()
    {
        // Specify the path for the new ZIP archive
        string zipFilePath = "output.zip";

        // Create an empty ZIP archive
        using (var archive = new IronArchive(zipFilePath))
        {
            // Add files to the ZIP
            archive.AddArchiveEntry("./assets/file1.txt");
            archive.AddArchiveEntry("./assets/file2.jpg");
            archive.AddArchiveEntry("./assets/file3.pdf");
        }

        Console.WriteLine("ZIP archive created successfully!");
    }
}
using IronZip;

class Program
{
    static void Main()
    {
        // Specify the path for the new ZIP archive
        string zipFilePath = "output.zip";

        // Create an empty ZIP archive
        using (var archive = new IronArchive(zipFilePath))
        {
            // Add files to the ZIP
            archive.AddArchiveEntry("./assets/file1.txt");
            archive.AddArchiveEntry("./assets/file2.jpg");
            archive.AddArchiveEntry("./assets/file3.pdf");
        }

        Console.WriteLine("ZIP archive created successfully!");
    }
}
Imports IronZip

Friend Class Program
	Shared Sub Main()
		' Specify the path for the new ZIP archive
		Dim zipFilePath As String = "output.zip"

		' Create an empty ZIP archive
		Using archive = New IronArchive(zipFilePath)
			' Add files to the ZIP
			archive.AddArchiveEntry("./assets/file1.txt")
			archive.AddArchiveEntry("./assets/file2.jpg")
			archive.AddArchiveEntry("./assets/file3.pdf")
		End Using

		Console.WriteLine("ZIP archive created successfully!")
	End Sub
End Class
$vbLabelText   $csharpLabel

有关IronZIP及其功能或代码示例的更多信息,请访问IronZIP文档页面。

結論

7-Zip继续在文件压缩领域保持主导地位,为用户提供了一个开源、功能丰富的解决方案,具有卓越的压缩比。 其对多种归档格式的支持和强大的加密功能使其成为普通用户和开发人员的多功能选择。其.NET SDK进一步扩展了其对C#开发人员的实用性,便于无缝集成到自定义应用程序中。 对于在.NET空间中寻找替代方案的人,IronZIP是一个值得注意的竞争者,提供类似的功能,旨在满足C#开发人员的特定需求。

IronZIP提供IronZIP免费试用。 从IronZIP下载页面下载并试用IronZIP .NET Core和Framework库。

常見問題解答

什麼是 7-Zip?為什麼它很受歡迎?

7-Zip 是一種開源文件壓縮工具,以其高壓縮比和對多種壓縮格式如 7z、ZIP、TAR 和 GZIP 的支持而聞名。它因其在數據管理上的效率和強大的 AES-256 加密功能而受歡迎。

7z 格式與其他格式有何不同?

7z 格式採用了 LZMA 壓縮算法,這提供了更優越的壓縮效率,通常能比標準 ZIP 格式高出 30-70% 的壓縮率。這使它成為優先考慮減小文件尺寸用戶的理想選擇。

開發人員如何在 C# 項目中使用 7-Zip?

開發人員可以利用 7-Zip 的 .NET SDK,其中包括 7z.dll 庫,將壓縮和解壓功能以編程方式集成到 C# 應用中。此外,還可以使用 7z.exe 命令行工具來管理壓縮文件。

IronZIP 對於 .NET 應用有什麼好處?

IronZIP 為 .NET 應用提供了友好的 API,簡化了 ZIP 文件的創建、閱讀和提取。這對於尋求易用性和強大 ZIP 文件管理功能的 C# 項目開發人員來說是一個可行的替代方案。

我可以使用 7-Zip 加密文件嗎?

是的,7-Zip 提供了使用 AES-256 的強大加密功能,允許用戶在壓縮過程中安全地加密文件並保護敏感數據。

IronZIP 有試用版嗎?

有的,IronZIP 提供可從其網站下載的免費試用版。此試用版允許開發人員探索其功能並將 ZIP 文件管理集成到 .NET 應用中。

7-Zip 的主要功能有哪些?

7-Zip 的功能包括高壓縮比、支持多種格式、強大的 AES-256 加密,以及用戶友好的界面和命令行支持,滿足多樣化的用戶需求。

如何在 C# 應用中使用文件壓縮?

你可以在 C# 應用中通過使用像 7-Zip SDK 的 7z.dll 或 IronZIP 的庫來使用文件壓縮,這兩者都提供了處理文件壓縮和提取的綜合工具。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。