如何在 C# 中使用密碼壓縮文件
ZIP 檔案廣泛用於壓縮和歸檔數據,使傳輸和儲存大量檔案變得更加容易。 然而,在某些情況下,額外的安全措施至關重要,這就凸顯了密碼保護的 zip 檔案的重要性。 密碼保護確保只有授權人員才能存取和提取 ZIP 存檔的內容,為敏感資料增加額外的安全性。
在本文中,我們將探討如何使用 C# 和 IronZIP 程式庫建立受密碼保護的 ZIP 檔案。 IronZIP 是一個功能強大的 C# ZIP 歸檔庫,它簡化了在 .NET 應用程式中處理 ZIP 檔案的過程。
如何建立帶密碼保護的 C# ZIP 文件
- 在 Visual Studio 中建立一個 C# 項目
- 從 NuGet 套件管理器安裝 IronZIP 庫
- 使用
IronZipArchive類別建立一個空的 ZIP 歸檔對象 - 使用
Encrypt方法新增密碼保護 - 使用
Add方法將檔案加入歸檔物件中 - 使用
SaveAs方法匯出 ZIP 檔案
IronZIP庫簡介
如何在 C# 中使用密碼壓縮檔案:圖 1 - IronZIP 網頁
IronZIP是一個領先的 C# ZIP 歸檔庫,專為在 .NET 中建立、讀取和提取歸檔檔案而設計。 它提供了一個用戶友好的 API,使開發人員能夠輕鬆地將歸檔管理功能整合到他們的 .NET 專案中。 IronZIP 支援多種歸檔格式,包括 ZIP、TAR、GZIP 和 BZIP2,為輕鬆處理 zip 檔案提供了全面的解決方案。
IronZIP的詳細功能
相容性
- 支援 .NET 8、7、6、5、Core、Standard 和 Framework。
- 相容於 C#、VB.NET 和 F# 語言。
- 跨平台支援 Windows、Linux、Mac、iOS、Android、Docker、Azure 和 AWS。
- 與 Microsoft Visual Studio 和 JetBrains ReSharper & Rider 等流行的 IDE 整合。
檔案產生與編輯
- 支援 ZIP、TAR、GZIP 和 BZIP2 歸檔格式。
- 建立、匯入和匯出 ZIP 檔案。
- 使用傳統加密、AES128 或 AES256 加密設定對 ZIP 檔案進行密碼保護。
- 自訂壓縮,9 個等級。 提供最佳的尺寸縮小效果。
- 管理歸檔中的文件項目,包括新增、提取和刪除。
安裝
- 透過 NuGet 套件管理器或套件管理器控制台快速輕鬆地安裝。
- 與 DigiCert 簽署二進位檔案集成,實現安全的二進位檔案認證。
在 Visual Studio 中建立 C# 控制台專案的步驟
讓我們逐步了解如何在 Visual Studio 中建立一個 C# 控制台項目,並使用 IronZIP 對 zip 檔案進行密碼保護。
1.開啟 Visual Studio。
- 建立一個新的 C# 控制台應用程式專案。
為你的項目命名並選擇一個地點。
- 從"附加資訊"中選擇最新版本的 .NET Framework。 IronZIP 支援最新的 8.0 .NET Framework。
- 點選"建立"生成項目。
安裝 IronZIP
要在您的專案中使用 IronZIP,您需要安裝該程式庫。 您可以使用 NuGet 套件管理器或套件管理器控制台來完成此操作。
使用 NuGet 套件管理員
- 在解決方案資源管理器中以滑鼠右鍵按一下您的專案。
- 選擇"管理 NuGet 套件..."
- 搜尋"IronZip",然後點選"安裝"。
使用軟體套件管理器控制台
- 開啟軟體包管理器控制台。
執行以下命令:
Install-Package IronZip
給 ZIP 檔案設定密碼保護的步驟
IronZIP 安裝完成後,您就可以使用該程式庫對 zip 檔案進行密碼保護了。
導入所需庫
using IronZip;
using IronZip.Enum;using IronZip;
using IronZip.Enum;Imports IronZip
Imports IronZip.Enum這些程式碼從 IronZIP 庫導入必要的命名空間: IronZip包含主要類別和功能,而IronZip.Enum包含庫中使用的枚舉。
主修課程
class Program
{
static void Main()
{
// Code execution starts here
}
}class Program
{
static void Main()
{
// Code execution starts here
}
}Friend Class Program
Shared Sub Main()
' Code execution starts here
End Sub
End Class這是程式的主類,其中包含Main方法,程式碼從這裡開始執行。
建立空的 ZIP 壓縮包
using (var archive = new IronZipArchive(9))
{
// Code within the 'using' block
}using (var archive = new IronZipArchive(9))
{
// Code within the 'using' block
}Using archive = New IronZipArchive(9)
' Code within the 'using' block
End Usingusing語句確保IronZipArchive物件在使用後被正確釋放。 它創建了一個具有最高壓縮等級(9)的IronZipArchive新實例。
對 ZIP 壓縮檔進行密碼保護
以下一行程式碼可為 ZIP 壓縮檔案新增密碼保護:
archive.Encrypt("P@ssw0rd", EncryptionMethods.Traditional);archive.Encrypt("P@ssw0rd", EncryptionMethods.Traditional);IRON VB CONVERTER ERROR developers@ironsoftware.com對archive物件呼叫Encrypt方法來為 ZIP 檔案設定密碼保護。它接受兩個參數:密碼字串("P@ssw0rd")和加密方法( EncryptionMethods.Traditional )。
IronZIP 也提供 AES128 和 AES256 進階密碼保護,更安全,可防止 ZIP 檔案被竄改。
將檔案加入 ZIP 壓縮包
archive.Add("./assets/file1.txt");
archive.Add("./assets/image1.png");archive.Add("./assets/file1.txt");
archive.Add("./assets/image1.png");archive.Add("./assets/file1.txt")
archive.Add("./assets/image1.png")Add方法用於向 ZIP 壓縮包中新增檔案。在本例中,位於"./assets/"目錄下的一個文字檔案和一個圖片檔案( file1.txt和image1.png )被加入到壓縮包中。
需要新增的文件如下:
導出 ZIP 壓縮包
archive.SaveAs("output.zip");archive.SaveAs("output.zip");archive.SaveAs("output.zip")呼叫SaveAs方法匯出 ZIP 壓縮文件,並將輸出檔案名稱指定為"output.zip"。 這將建立一個包含指定內容和密碼的受密碼保護的 ZIP 檔案。
造訪程式碼範例頁面,以了解更多關於如何使用 IronZIP 在 C# 中建立、讀取、提取和執行其他與 ZIP 檔案相關的操作的資訊。
以下是完整的原始程式碼,其中包含分隔的字串路徑和一個密碼屬性,以便更好地進行控制:
using IronZip;
using IronZip.Enum;
class Program
{
static void Main()
{
// Define password and file paths for the ZIP archive
string password = "P@ssw0rd";
string filename = "./assets/file1.txt";
string imagename = "./assets/image1.png";
// Create a new ZIPArchive with the highest compression level
using (var archive = new IronZipArchive(9))
{
// Add Password to protect the ZIP (Support AES128 & AES256)
archive.Encrypt(password, EncryptionMethods.Traditional);
// Add files to the archive
archive.Add(filename);
archive.Add(imagename);
// Export the Encrypted ZIP file archive
archive.SaveAs("output.zip");
}
}
}using IronZip;
using IronZip.Enum;
class Program
{
static void Main()
{
// Define password and file paths for the ZIP archive
string password = "P@ssw0rd";
string filename = "./assets/file1.txt";
string imagename = "./assets/image1.png";
// Create a new ZIPArchive with the highest compression level
using (var archive = new IronZipArchive(9))
{
// Add Password to protect the ZIP (Support AES128 & AES256)
archive.Encrypt(password, EncryptionMethods.Traditional);
// Add files to the archive
archive.Add(filename);
archive.Add(imagename);
// Export the Encrypted ZIP file archive
archive.SaveAs("output.zip");
}
}
}Imports IronZip
Imports IronZip.Enum
Friend Class Program
Shared Sub Main()
' Define password and file paths for the ZIP archive
Dim password As String = "P@ssw0rd"
Dim filename As String = "./assets/file1.txt"
Dim imagename As String = "./assets/image1.png"
' Create a new ZIPArchive with the highest compression level
Using archive = New IronZipArchive(9)
' Add Password to protect the ZIP (Support AES128 & AES256)
archive.Encrypt(password, EncryptionMethods.Traditional)
' Add files to the archive
archive.Add(filename)
archive.Add(imagename)
' Export the Encrypted ZIP file archive
archive.SaveAs("output.zip")
End Using
End Sub
End Class輸出
執行程式後,您的專案目錄中將出現一個名為"output.zip"的受密碼保護的單一文件,其中包含指定的文件。
結論
在本文中,我們探討了密碼保護的 ZIP 檔案的重要性,並介紹了 IronZIP 庫,它是在 C# 專案中處理 ZIP 歸檔檔案的強大解決方案。 我們詳細介紹了 IronZIP 的各項功能,包括相容性、歸檔產生、編輯功能以及簡易的安裝步驟。 該程式庫支援傳統和進階加密方法,以保護文件免遭篡改。 最後,我們逐步解解如何在 Visual Studio 中建立一個 C# 控制台專案、安裝 IronZIP 以及對 ZIP 檔案進行密碼保護。
IronZIP 簡化了在 C# 應用程式中處理 ZIP 檔案的過程,為開發人員提供了一套強大的歸檔管理和安全工具集。 將 IronZIP 整合到您的專案中,可增強在處理 ZIP 檔案中的敏感資訊時的資料保護。 有關 IronZIP 及其功能的更多詳細信息,請訪問官方文件頁面。
IronZIP 提供免費試用,方便用戶長期使用。 其精簡版套餐起價為$799 。
常見問題解答
如何在 C# 中建立受密碼保護的 ZIP 檔案?
您可以使用 IronZIP 函式庫在 C# 中建立受密碼保護的 ZIP 檔案。首先,透過 NuGet 安裝該函式庫,然後創建 IronZipArchive 物件,使用 Encrypt 方法新增密碼,將檔案新增至歸檔,並使用 SaveAs 儲存歸檔。
有哪些可用於保護 ZIP 檔案安全的加密選項?
IronZIP 提供傳統、AES128 和 AES256 加密方法來保護 ZIP 檔案。這些選項提供不同層級的安全性,以保護 ZIP 存檔中的敏感資料。
IronZIP 是否與多個 .NET 版本相容?
是的,IronZIP 與 .NET 8、7、6、5、Core、Standard 和 Framework 相容,使其成為在不同 .NET 環境下工作的開發人員的多用途選擇。
如何在我的專案中安裝 IronZIP?
您可以使用 Visual Studio 中的 NuGet Package Manager 安裝 IronZIP。在套件管理員中搜尋「IronZIP」,並將其新增至您的專案,即可開始管理 ZIP 檔案。
IronZIP 可以用於 C# 以外的程式語言嗎?
是的,除了 C# 之外,IronZIP 還與 VB.NET 和 F# 相容,讓開發人員可以在各種 .NET 語言的應用程式中使用。
設定 ZIP 檔案管理的 C# 主控台應用程式需要哪些步驟?
若要使用 IronZIP 設定 ZIP 檔案管理的 C# 主控台應用程式,請在 Visual Studio 中建立新的主控台專案,透過 NuGet 安裝 IronZIP,並依照函式庫的說明文件新增 ZIP 檔案功能。
使用 IronZIP 處理 ZIP 檔案的主要優點是什麼?
IronZIP 透過提供易於使用的 API、跨平台支援,以及密碼保護和支援多種歸檔格式等功能,簡化了 ZIP 檔案的管理,同時增強了功能性和資料安全性。
密碼保護如何提高 ZIP 檔案的安全性?
密碼保護功能可確保只有經過授權的個人才能存取 ZIP 檔案的內容,為存檔中儲存的敏感資料增加重要的安全層級。





