.NET 幫助

SevenZip C#(開發者如何使用)

介紹

在檔案進行壓縮和存檔工具領域中,7-Zip 脫穎而出,成為一種多元化的開源解決方案。 SevenZip因其高壓縮比和對多種壓縮格式的支援而聞名,成為尋求高效文件管理的用戶的熱門選擇。 在本文中,我們將探討什麼是7-Zip、它如何運作、其主要功能、獨特的7z格式、壓縮比率以及其用於C#的.NET SDK,這有助於建立7z檔案。 此外,我們將介紹 IronZIP 作為 .NET 生態系統中的替代方案。

什麼是SevenZip?

SevenZip 或 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# 代碼中使用命令行可執行檔。 以下是兩種方法的簡要概述。

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");
        // Create a SevenZipExtractor or SevenZipCompressor instance
        // Perform compression or decompression operations as needed
        // 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 File entry in 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");
        // Create a SevenZipExtractor or SevenZipCompressor instance
        // Perform compression or decompression operations as needed
        // 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 File entry in 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")
		' Create a SevenZipExtractor or SevenZipCompressor instance
		' Perform compression or decompression operations as needed
		' 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 File entry in 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 庫。

Chipego
奇佩戈·卡林达
軟體工程師
Chipego 擁有天生的傾聽技能,這幫助他理解客戶問題,並提供智能解決方案。他在獲得信息技術理學學士學位後,于 2023 年加入 Iron Software 團隊。IronPDF 和 IronOCR 是 Chipego 專注的兩個產品,但隨著他每天找到新的方法來支持客戶,他對所有產品的了解也在不斷增長。他喜歡在 Iron Software 的協作生活,公司內的團隊成員從各自不同的經歷中共同努力,創造出有效的創新解決方案。當 Chipego 離開辦公桌時,他常常享受讀好書或踢足球的樂趣。
< 上一頁
Mailkit C#(適用於開發人員的工作原理)
下一個 >
ByteSize C#(對開發人員的運作方式)