如何使用 C# 壓縮文件夾中的文件
ZIP 檔案是包含一個或多個使用 ZIP 格式的壓縮檔案或資料夾的檔案。 這是一種常見的將多個檔案或資料夾壓縮並歸檔到單一檔案中的方法。它可以減小資料大小,節省磁碟空間,並使透過網路傳輸檔案更加容易。 本文將介紹如何使用IronZIP函式庫在C#中處理ZIP檔。 您將了解如何以程式設計方式建立、讀取、提取和更新 ZIP 文件,以及如何使用 IronZIP 的各種功能,例如加密、密碼保護和壓縮等級。 閱讀本文,您將能夠輕鬆地在 C# 應用程式中使用 IronZIP 處理 ZIP 檔案。
本文將涵蓋以下內容
- 在我們的專案中安裝 IronZIP
- 建立 ZIP 文件
- 建立受密碼保護的 ZIP 文件
- 解壓縮 ZIP 文件
- 解壓縮受密碼保護的 ZIP 文件
- 存取現有 ZIP 壓縮包
IronZip是什麼?
IronZIP是一個功能強大且用途廣泛的 C# ZIP 歸檔庫,可讓您以程式設計方式建立、讀取和提取 ZIP 檔案。 它支援多種歸檔格式,例如ZIP 、 TAR 、 GZIP和BZIP2 。 它還支援密碼保護、加密和壓縮等級。 IronZIP 與 .NET 8、7、6、Core、Standard 和 Framework 相容。
IronZIP 可以幫助您處理使用 ZIP 檔案的各種場景和優勢,例如:
- 建立備份系統:您可以使用 IronZIP 將重要的檔案和資料夾壓縮並加密成 ZIP 檔案,然後儲存於安全的位置。 這樣可以節省磁碟空間,並保護您的資料免受未經授權的存取。
- 傳送電子郵件附件:您可以使用 IronZIP 將電子郵件附件壓縮成 ZIP 檔案,以縮小其大小。此舉有助於避免超過檔案大小限制,並加速傳輸過程。
- 從網路下載檔案:您可以使用 IronZIP 從網路下載並解壓縮 ZIP 檔案,例如軟體套件、文件、圖片及其他類型的檔案。 這可以幫助您節省頻寬和時間,並輕鬆存取所需文件。
IronZIP入門指南
在編寫代碼之前,您需要在 C# 專案中安裝 IronZIP NuGet 套件。 IronZIP 是一個受歡迎的壓縮函式庫,可透過 NuGet 取得。
安裝 IronZIP 庫
若要安裝 IronZIP,您可以使用 Visual Studio 中的 NuGet 套件管理器控制台。 只需執行以下指令即可:
Install-Package IronZip
或者,您可以直接從IronZIP官方網站下載軟體包。安裝完成後,您可以透過在 C# 程式碼頂部新增以下命名空間來開始使用。
using IronZip;
using IronZip;
Imports IronZip
在資料夾中建立 C# ZIP 文件
您可以使用 IronZIP 輕鬆地在資料夾中建立 ZIP 檔案。以下程式碼會將指定目錄中的所有檔案壓縮成 ZIP 檔案。
using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}
using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}
Imports System
Imports System.IO
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Get all files from the specified directory
Dim fileArray() As String = Directory.GetFiles("D:\Docs\")
' Create a new ZIP archive
Using archive = New IronZipArchive()
' Iterate through each file and add it to the archive
For Each file In fileArray
archive.Add(file) ' Add files to the ZIP
Next file
' Save the archive to a file
archive.SaveAs("myZipFile.zip")
End Using
End Sub
End Class
上述 C# 程式碼使用 IronZIP 函式庫將所有檔案壓縮到一個 ZIP 檔案中。該程式碼執行以下操作:
- 宣告一個名為
fileArray的字串數組,並將Directory.GetFiles方法的結果賦值給它,並將目錄路徑("D:\Docs")作為參數傳遞。 此方法傳回一個字串數組,其中包含指定目錄中所有檔案的完整名稱。 - 建立
IronZipArchive類別的新實例,該實例表示記憶體中的 ZIP 檔案。 此實例被賦值給一個名為archive的變量,並包裝在using語句中,以確保在程式碼區塊結束時釋放 ZIP 檔案。 - 使用循環遍歷數組,對於每個文件,呼叫物件的方法,並將文件名作為參數傳遞。 此方法會在 ZIP 檔案中新增一個條目,其名稱和內容與檔案相同。
- 呼叫
SaveAs物件的archive方法,並將 ZIP 檔案的名稱("myZipFile.zip")作為參數傳遞。 此方法將 ZIP 壓縮檔案儲存到目前檔案系統中的一個檔案中。
這樣,您只需幾行程式碼即可輕鬆建立新的 ZIP 壓縮檔案。
輸出
輸出結果如下:
建立受密碼保護的 ZIP 文件
IronZIP 提供了一種建立受密碼保護的 ZIP 檔案的簡單方法。 以下程式碼範例將壓縮檔案並建立一個帶有密碼的新 ZIP 檔案。
using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256);
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}
using System;
using System.IO;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Get all files from the specified directory
string[] fileArray = Directory.GetFiles(@"D:\Docs\");
// Create a new ZIP archive
using (var archive = new IronZipArchive())
{
// Iterate through each file and add it to the archive
foreach (var file in fileArray)
{
archive.Add(file); // Add files to the ZIP
}
// Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256);
// Save the archive to a file
archive.SaveAs("myZipFile.zip");
}
}
}
Imports System
Imports System.IO
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Get all files from the specified directory
Dim fileArray() As String = Directory.GetFiles("D:\Docs\")
' Create a new ZIP archive
Using archive = New IronZipArchive()
' Iterate through each file and add it to the archive
For Each file In fileArray
archive.Add(file) ' Add files to the ZIP
Next file
' Set Password and Encryption Method
archive.Encrypt("myPa55word", EncryptionMethods.AES256)
' Save the archive to a file
archive.SaveAs("myZipFile.zip")
End Using
End Sub
End Class
這行程式碼 archive.Encrypt("myPa55word", EncryptionMethods.AES256); 使用 IronZIP 為 ZIP 壓縮檔案設定密碼("myPa55word")。它透過對壓縮檔案套用 AES-256 加密來增強安全性,確保只有擁有正確密碼的使用者才能存取其內容。 此功能對於在 C# 應用程式中儲存或傳輸敏感資料時提供保護非常有用。 您需要在第二個參數中傳遞指定的加密演算法模式。
文件已加密,如下所示。
輸出
如何使用 C# 將資料夾中的檔案壓縮成 ZIP 檔案:圖 2 - 上一個程式碼範例中輸出的加密文件
我們已經看到了透過循環遍歷指定路徑中的目錄來建立 ZIP 檔案的示範。 現在,讓我們來看一個解壓縮檔案的例子。
從 ZIP 壓縮包中提取文件
IronZIP 提供了一種在 C# 中從 ZIP 檔案中提取檔案的方法。 以下程式碼範例將提取 ZIP 壓縮包中的壓縮檔案。
using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles");
}
}
using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles");
}
}
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Extract all files from the ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles")
End Sub
End Class
代碼 IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles"); 使用 IronZIP 從"myZipFile.zip"中提取所有文件,並將它們放置在"myExtractedFiles"目錄中。 這種簡潔的方法簡化了在 C# 中提取 ZIP 存檔的過程,為文件提取任務提供了一個便捷的解決方案。
輸出
輸出結果如下:
如何從受密碼保護的 ZIP 檔案中提取文件
IronZIP 還提供了一種提取受密碼保護的 ZIP 檔案的方法。 以下程式碼將使用 IronZIP 方法從指定的 ZIP 檔案中提取所有現有檔案和目錄。
using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word");
}
}
using IronZip;
class Program
{
static void Main(string[] args)
{
// Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word");
}
}
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Extract all files from the password-protected ZIP archive to the specified directory
IronZipArchive.ExtractArchiveToDirectory("myZipFile.zip", "myExtractedFiles", "myPa55word")
End Sub
End Class
ExtractArchiveToDirectory 類別的方法將 ZIP 檔案中的所有條目提取到指定的目錄中。 它會向該方法傳遞三個參數:ZIP 檔案的路徑("myZipFile.zip")、目標目錄的路徑("myExtractedFiles")和 ZIP 檔案的密碼("myPa55word")。
這樣,您就可以輕鬆提取受密碼保護的 ZIP 檔案。
如何存取現有存檔
IronZIP 提供了存取現有存檔並查看文件中所有條目的方法。
using System;
using System.Collections.Generic;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Open an existing ZIP archive with a password
using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word"))
{
// Get entries list
List<string> names = archive.GetArchiveEntryNames();
// Iterate through each entry name and print it
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
}
using System;
using System.Collections.Generic;
using IronZip;
class Program
{
static void Main(string[] args)
{
// Open an existing ZIP archive with a password
using (var archive = new IronZipArchive("myZipFile.zip", "myPa55word"))
{
// Get entries list
List<string> names = archive.GetArchiveEntryNames();
// Iterate through each entry name and print it
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
}
Imports System
Imports System.Collections.Generic
Imports IronZip
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Open an existing ZIP archive with a password
Using archive = New IronZipArchive("myZipFile.zip", "myPa55word")
' Get entries list
Dim names As List(Of String) = archive.GetArchiveEntryNames()
' Iterate through each entry name and print it
For Each name As String In names
Console.WriteLine(name)
Next name
End Using
End Sub
End Class
提供的 C# 程式碼使用 IronZIP 建立一個安全的 IronZipArchive 實例,方法是載入一個名為"myZipFile.zip"的 ZIP 文件,密碼為"myPa55word"。 如果文件未加密,則不要傳遞密碼參數。 然後,它會檢索並列印加密 ZIP 檔案中的條目名稱(檔案和資料夾名稱)清單。
GetArchiveEntryNames 方法收集條目名稱,foreach 循環將每個名稱輸出到控制台。 這展示了 IronZIP 如何以簡潔的方式安全地存取和檢索受密碼保護的 ZIP 存檔中的條目資訊。
輸出
結論
總之,IronZIP 被證明是一個強大且多功能的 C# 函式庫,可用於處理 ZIP 檔案。 它的功能不僅限於基本的壓縮和提取,還提供密碼保護、加密以及與各種存檔格式的兼容性等功能。 無論您是建立備份系統、管理電子郵件附件或從網路下載文件,IronZIP 都能以簡單且有效率的方式簡化這些任務。
透過將 IronZIP 整合到您的 C# 應用程式中,您可以獲得一個強大的工具來處理 ZIP 檔案、增強資料安全性並優化檔案傳輸過程。 您可以根據需要享受免費試用。
常見問題解答
如何在 C# 中從資料夾建立 ZIP 文件?
您可以使用 IronZIP 從資料夾建立 ZIP 文件,方法是遍歷目錄中的文件,並將它們添加到新的 ZIP 壓縮檔案中。使用 IronZipArchive 類別並調用 SaveAs 方法以保存 ZIP 文件。
如何在 C# 項目中安裝 IronZIP?
通過在 Visual Studio 的 NuGet 套件管理器中使用命令 Install-Package IronZip 在 C# 專案中安裝 IronZIP,或從官方 IronZIP 網站下載。
IronZIP 支持哪些格式的 ZIP 文件處理?
IronZIP 支持包括 ZIP、TAR、GZIP 和 BZIP2 在內的多種壓縮格式,提供不同壓縮和存檔需求的靈活性。
我可以加密在 C# 中創建的 ZIP 文件嗎?
是的,您可以使用 IronZIP 的 Encrypt 方法將 AES-256 加密應用於您的壓縮檔案中的數據進行加密。
如何在 C# 中從 ZIP 文件中提取文件?
要使用 IronZIP 從 ZIP 檔案中提取文件,請使用 ExtractArchiveToDirectory 方法,指定來源 ZIP 文件和目標目錄。
是否可以在 C# 中處理受密碼保護的 ZIP 文件?
是的,您可以通過在使用 ExtractArchiveToDirectory 等方法時提供密碼來處理受密碼保護的 ZIP 文件,以安全地訪問內容。
使用 IronZIP 進行 ZIP 文件管理有哪些優勢?
IronZIP 簡化了文件備份、電子郵件附件管理和網路下載等任務,提供加密、密碼保護和多種壓縮格式支持等功能。
IronZIP 支持 .NET 8 和其他版本嗎?
IronZIP 與 .NET 8、7、6、Core、Standard 和 Framework 兼容,便於整合到各種 C# 項目中。
開發人員如何獲取 IronZIP 的試用版本?
開發人員可以通過訪問 IronZIP 網站的授權或下載部分來訪問免費試用版本,以評估其功能。
使用 ZIP 文件進行數據傳輸有何好處?
ZIP 文件有助於減少文件大小、節省磁碟空間,並便於通過互聯網高效傳輸多個文件。

