跳至頁尾內容
開發者更新

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# (How It Works For Developers) Figure 1 - 7-Zip

主要功能

  • 高壓縮比: 7-Zip的一個關鍵功能是其能實現高壓縮率,通常超越其他存檔工具。 這可以帶來顯著的儲存空間節省和更快的檔案傳輸到資料夾。
  • 廣泛的格式支持: 7-Zip可以處理各種存檔格式,包括7z、ZIP、TAR、GZIP等。 這種多樣性確保了與不同操作系統和軟體的相容性。
  • 加密和安全: 7-Zip提供強大的加密功能,允許使用者用AES-256加密來保護他們的存檔。 這確保了敏感資料保持受保護。
  • 命令列支持: 除了其使用者友好的圖形介面外,7-Zip還提供命令列版本,適合喜歡自動化和腳本化對檔案管理任務的使用者。

它如何運作

7-Zip利用先進的壓縮算法來減少檔案和資料夾的大小。 它使用LZMA (Lempel-Ziv-Markov chain-Algorithm) 壓縮算法來支持其本地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#源程式碼中通過@x在@Bs@C類中使用7-Zip命令列可執行文件(7z.exe)。

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# (How It Works For Developers) Figure 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#開發人員的實用性,促進了無縫整合到自訂應用程式中。 IronZIP在.NET領域中作為一個值得注意的競爭者出現,提供類似的功能,以滿足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# 應用程式中使用文件壓縮?

您可以通過使用像7-Zip SDK與7z.dll或IronZIP這樣的庫在C#應用程式中使用檔案壓縮功能,這兩者均提供處理文件壓縮及提取的完整工具。

Jacob Mellor,首席技術官 @ Team Iron
首席技術官

Jacob Mellor是Iron Software的首席技術官,一位在C# PDF技術上開創先河的遠見工程師。作為Iron Software核心程式碼庫的原開發者,他從創立以來就一直在塑造公司的產品架構,與首席執行官Cameron Rimington一起將公司轉變為服務於NASA、特斯拉和全球政府公司的50多名人員的公司。

Jacob擁有曼徹斯特大學的土木工程一等榮譽學士學位(BEng),於1998-2001年之間獲得。在1999年於倫敦創辦他的第一家軟體公司並於2005年建立了他的第一批.NET元組件後,他專注於解決Microsoft生態系統中的複雜問題。

他的旗艦IronPDF和Iron Suite .NET程式庫在全球獲得了超過3000萬次NuGet安裝依據,他的基礎程式碼基繼續支援著世界各地開發者使用的工具。擁有25年的商業經驗和41年的程式設計專業知識,他仍專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話