使用 IRONZIP 如何在 C# 中開啟 Zip 檔案 Curtis Chau 更新:6月 22, 2025 下載 IronZIP NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 ZIP 是一種支援無損資料壓縮的歸檔條目檔案系統格式。 ZIP 檔案可能包含一個或多個已壓縮的檔案或目錄。 ZIP 檔案格式支援多種壓縮演算法,其中 DEFLATE 是最常用的。 隨後,許多軟體工具很快便支援了ZIP格式。 主流作業系統廠商早已在系統中加入了對 ZIP 壓縮檔案的支援。微軟從 Windows 98 開始就加入了對 ZIP 檔案的支持,其他廠商也紛紛效法。 在本部落格中,我們將探索一種使用IronZIP開啟 ZIP 壓縮檔案或提取檔案的現代、簡單且高效的方法。 我們將學習有關 ZIP 檔案的基本知識及其優點。 接下來,我們將看到系統命名空間中可用於處理 ZIP 檔案格式的選項。 然後,我們將探索打開 ZIP 文件的分步指導,將 ZIP 文件提取到臨時文件夾,創建新 ZIP 文件並向現有 ZIP 文件添加文件。 在軟體應用程式中使用 ZIP 檔案的優勢 1.壓縮:此技術使用各種壓縮演算法(如 Implode、Deflate、Deflate64、bzip2、LZMA、WavPack、PPMd 等)來減少歸檔檔案/資料夾的大小。 2.傳輸時間縮短:檔案越小,傳輸速度越快,尤其是透過網路傳送檔案時。 這對於電子郵件附件以及從網站上傳或下載檔案尤其有利。 3.檔案合併: ZIP 檔案可以將多個檔案合併到一個檔案中,從而減少需要管理的檔案數量。 這對於組織專案或分發由多個文件組成的軟體非常有用。 4.密碼保護:許多 ZIP 工具提供對壓縮檔案進行密碼保護的選項,為您的檔案增加一層安全性。 當您想要限制對 ZIP 檔案內容的存取時,這非常有用。 使用 IronZip 建立 ZIP 壓縮檔案並解壓縮 ZIP 文件 IronZIP 庫的介紹和文件可以在這裡找到。 在 C# 應用程式中,可以透過多種方式建立和提取 ZIP 檔案。 IronZIP NuGet 套件具備將檔案歸檔為不同格式(ZIP、TAR、GZIP 和 BZIP2)的所有功能。以下範例步驟展示如何在現代應用程式編程中使用 IronZIP 來建立新一代應用程序,例如開啟 ZIP 檔案、解壓縮 ZIP 檔案、建立新的 ZIP 檔案等。 步驟 1. 建立 .NET Core 控制台應用程式 創建專案 可以使用 Visual Studio 建立 .NET 控制台應用程式。 開啟 Visual Studio 並選擇"建立專案"。 這裡可以看到各種可用於建立專案的範本。 演示或測試程式碼最簡單的方法是建立一個控制台應用程式。 我們將選擇控制台應用程式專案範本。 如何在 C# 中開啟 Zip 檔案:圖 1 - 新建項目 請輸入項目名稱 在下面的視窗中,您可以輸入項目名稱、項目在檔案系統中的儲存位置,最後一個是解決方案資料夾的路徑。 您可以將解決方案資料夾和專案資料夾放在同一個資料夾中,也可以將它們放在不同的資料夾中。 如何在 C# 中開啟 Zip 檔案:圖 2 - 設定項目 選擇 .NET Framework 版本 下一步是為專案選擇 .NET Framework 版本。 如果您想在特定版本中進行開發,請指定您想要的版本。否則,請務必選擇最新的穩定版本來建立專案。 您可以從微軟網站下載最新版本。然後點擊"建立"以產生控制台應用程式。 如何在 C# 中開啟 Zip 檔案:圖 3 - Target Framework 這將根據範本建立預設項目,並將項目和解決方案檔案儲存在指定的目錄中。 專案創建完成後,它將類似於下圖。 有時,在最新版本中, program.cs檔案中不會使用該類別。 // Import necessary namespaces using System; // Define the namespace namespace MyApp // Note: actual namespace depends on the project name. { // Define the Program class internal class Program { // Main method: Entry point of the application static void Main(string[] args) { // Print a welcome message Console.WriteLine("Hello, World!"); } } } // Import necessary namespaces using System; // Define the namespace namespace MyApp // Note: actual namespace depends on the project name. { // Define the Program class internal class Program { // Main method: Entry point of the application static void Main(string[] args) { // Print a welcome message Console.WriteLine("Hello, World!"); } } } ' Import necessary namespaces Imports System ' Define the namespace Namespace MyApp ' Note: actual namespace depends on the project name. ' Define the Program class Friend Class Program ' Main method: Entry point of the application Shared Sub Main(ByVal args() As String) ' Print a welcome message Console.WriteLine("Hello, World!") End Sub End Class End Namespace $vbLabelText $csharpLabel 要建立一個新的 ZIP 檔案並提取所有 ZIP 歸檔文件,我們可以使用預設庫中的 System.IO.Compression。 以下程式碼示範如何使用ZipFile.OpenRead和ZipFile.Open靜態方法來開啟 ZIP 檔案或提取 ZIP 檔案。 // Import necessary namespaces using System; using System.IO; using System.IO.Compression; public class ZipExample { public static void Main() { Console.WriteLine("-----------Zip - Unzip-----------"); // Method to add a file entry to the ZIP archive static void AddEntry(string filePath, ZipArchive zipArchive) { // Get file name from the file path var file = Path.GetFileName(filePath); // Create a new entry in the ZIP archive for the file zipArchive.CreateEntryFromFile(filePath, file); } // Name of the ZIP file to be created var zip = "myFile.zip"; // Open or create the ZIP file for updating using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Update)) { // Add files to the archive AddEntry("file1.txt", archive); AddEntry("file2.txt", archive); } // Directory where we want to extract the ZIP files var dirToExtract = "extract"; // Create the directory if it does not exist if (!Directory.Exists(dirToExtract)) { Directory.CreateDirectory(dirToExtract); } // Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract); // Indicate that extraction is complete Console.WriteLine("Files extracted to: " + dirToExtract); } } // Import necessary namespaces using System; using System.IO; using System.IO.Compression; public class ZipExample { public static void Main() { Console.WriteLine("-----------Zip - Unzip-----------"); // Method to add a file entry to the ZIP archive static void AddEntry(string filePath, ZipArchive zipArchive) { // Get file name from the file path var file = Path.GetFileName(filePath); // Create a new entry in the ZIP archive for the file zipArchive.CreateEntryFromFile(filePath, file); } // Name of the ZIP file to be created var zip = "myFile.zip"; // Open or create the ZIP file for updating using (ZipArchive archive = ZipFile.Open(zip, ZipArchiveMode.Update)) { // Add files to the archive AddEntry("file1.txt", archive); AddEntry("file2.txt", archive); } // Directory where we want to extract the ZIP files var dirToExtract = "extract"; // Create the directory if it does not exist if (!Directory.Exists(dirToExtract)) { Directory.CreateDirectory(dirToExtract); } // Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract); // Indicate that extraction is complete Console.WriteLine("Files extracted to: " + dirToExtract); } } ' Import necessary namespaces Imports System Imports System.IO Imports System.IO.Compression Public Class ZipExample Public Shared Sub Main() Console.WriteLine("-----------Zip - Unzip-----------") ' Method to add a file entry to the ZIP archive 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' static void AddEntry(string filePath, ZipArchive zipArchive) ' { ' ' Get file name from the file path ' var file = Path.GetFileName(filePath); ' ' ' Create a new entry in the ZIP archive for the file ' zipArchive.CreateEntryFromFile(filePath, file); ' } ' Name of the ZIP file to be created Dim zip = "myFile.zip" ' Open or create the ZIP file for updating Using archive As ZipArchive = ZipFile.Open(zip, ZipArchiveMode.Update) ' Add files to the archive AddEntry("file1.txt", archive) AddEntry("file2.txt", archive) End Using ' Directory where we want to extract the ZIP files Dim dirToExtract = "extract" ' Create the directory if it does not exist If Not Directory.Exists(dirToExtract) Then Directory.CreateDirectory(dirToExtract) End If ' Extract the contents of the ZIP file to the specified directory ZipFile.ExtractToDirectory(zip, dirToExtract) ' Indicate that extraction is complete Console.WriteLine("Files extracted to: " & dirToExtract) End Sub End Class $vbLabelText $csharpLabel 上面的程式碼中使用了名為myFile.zip的 ZIP 文件,並指定了系統命名空間。 Open方法用於以指定模式開啟 ZIP 檔案。 這也可以用來建立新的 ZIP 壓縮檔。 開啟後,我們可以使用AddEntry方法新增新的存檔條目,然後ExtractToDirectory將 ZIP 存檔檔案提取到指定的目錄。 如果目錄不存在,則使用Directory.CreateDirectory建立該目錄。 步驟 2. 使用 NuGet 套件管理器安裝 IronZIP 從 Visual Studio 開啟專案管理器,搜尋 IronZIP 套件。 然後選擇最新版本並點擊安裝。 您可以從下拉式選單中變更要安裝的版本。 然後點選安裝。 如何在 C# 中開啟 Zip 檔案:圖 4 - NuGet 套件管理器 使用 IronZIP 建立 ZIP 壓縮檔案並新增文件 如何在 C# 中開啟 Zip 檔案:圖 5 - IronZIP IronZIP是由Iron Software開發的歸檔壓縮和解壓縮函式庫。 除了廣泛使用的 ZIP 格式外,它還可以處理 TAR、GZIP 和 BZIP2 格式。 IronZIP是一個 C# ZIP 歸檔庫,它優先考慮準確性、易用性和速度。 它易於使用的 API 使開發人員能夠在幾分鐘內輕鬆地將存檔功能添加到現代 .NET 專案中。 與System.IO.Compression庫相比, IronZIP具有許多優勢。 您可以在壓縮過程中指定所需的壓縮比,也可以使用不同的壓縮演算法,例如 ZIP、TAR、GZIP、BZIP2。它還支援行動、Web 和桌面平台以及各種 .NET 版本。 // Setup: Specify the path for the new ZIP archive var archivePath = "ironZip.zip"; // Check if the archive already exists, and delete it if so if (File.Exists(archivePath)) { File.Delete(archivePath); } // Use IronZIP library to create a new ZIP archive using (var archive = new IronZipArchive(9)) // Compression level: 9 for maximum compression { // Add files to the ZIP archive archive.Add("file1.txt"); archive.Add("file2.txt"); // Save the archive to the specified path archive.SaveAs(archivePath); } // Setup: Specify the path for the new ZIP archive var archivePath = "ironZip.zip"; // Check if the archive already exists, and delete it if so if (File.Exists(archivePath)) { File.Delete(archivePath); } // Use IronZIP library to create a new ZIP archive using (var archive = new IronZipArchive(9)) // Compression level: 9 for maximum compression { // Add files to the ZIP archive archive.Add("file1.txt"); archive.Add("file2.txt"); // Save the archive to the specified path archive.SaveAs(archivePath); } ' Setup: Specify the path for the new ZIP archive Dim archivePath = "ironZip.zip" ' Check if the archive already exists, and delete it if so If File.Exists(archivePath) Then File.Delete(archivePath) End If ' Use IronZIP library to create a new ZIP archive Using archive = New IronZipArchive(9) ' Compression level: 9 for maximum compression ' Add files to the ZIP archive archive.Add("file1.txt") archive.Add("file2.txt") ' Save the archive to the specified path archive.SaveAs(archivePath) End Using $vbLabelText $csharpLabel 初始原始碼透過指定 ZIP 歸檔檔案名稱並檢查指定的目錄是否存在來進行設定。 然後我們使用Add方法將檔案歸檔以建立 ZIP 檔案。 壓縮參數中的第二個參數,1 表示較低,9 表示較高。 文字檔案可以壓縮到最高 9 倍而不會遺失數據,圖像檔案可以使用較低的壓縮率以避免數據遺失。 使用 IronZip 開啟 ZIP 壓縮文件 IronArchive類別也可以用於從 ZIP 壓縮檔案中提取檔案。所有檔案都使用IronArchive.ExtractArchiveToDirectory方法提取,該方法可以將所有檔案提取到如下所示的指定目錄。 // Directory to extract all files from the ZIP archive var extractionPath = "IronZipFiles"; // Check if the directory exists; if not, create it if (!Directory.Exists(extractionPath)) { Directory.CreateDirectory(extractionPath); } // Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath); // Directory to extract all files from the ZIP archive var extractionPath = "IronZipFiles"; // Check if the directory exists; if not, create it if (!Directory.Exists(extractionPath)) { Directory.CreateDirectory(extractionPath); } // Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath); ' Directory to extract all files from the ZIP archive Dim extractionPath = "IronZipFiles" ' Check if the directory exists; if not, create it If Not Directory.Exists(extractionPath) Then Directory.CreateDirectory(extractionPath) End If ' Extract all files from the specified ZIP archive to the directory IronArchive.ExtractArchiveToDirectory("ironZip.zip", extractionPath) $vbLabelText $csharpLabel 以上程式碼將 ZIP 檔案解壓縮到指定目錄。 程式碼檢查目錄是否存在,如果不存在,則將 ZIP 歸檔檔案解壓縮到指定目錄。 授權(可免費試用) 為了讓上述程式碼能正常運作,需要授權金鑰。 此 key 需要放在 appsettings.json 中。 { "IronZip.LicenseKey": "your license key" } 開發者可在此註冊以取得試用許可證,試用許可證無需信用卡。 使用者可以提供電子郵件地址並註冊免費試用。 在現有 ZIP 壓縮檔中新增文件 使用IronZIP的靜態方法FromFile開啟現有壓縮檔。此方法還需要指定將要建立的輸出新壓縮檔案的檔案名稱。 // Path to a new file to be added to the existing ZIP archive const string file3 = ".\\image3.png"; var archivePlusPath = "ironZipPlus.zip"; // Open the existing ZIP archive and add a new file using (var file = IronArchive.FromFile("ironZip.zip", archivePlusPath)) { // Add additional files to the existing archive file.Add(file3); } // Path to a new file to be added to the existing ZIP archive const string file3 = ".\\image3.png"; var archivePlusPath = "ironZipPlus.zip"; // Open the existing ZIP archive and add a new file using (var file = IronArchive.FromFile("ironZip.zip", archivePlusPath)) { // Add additional files to the existing archive file.Add(file3); } ' Path to a new file to be added to the existing ZIP archive Const file3 As String = ".\image3.png" Dim archivePlusPath = "ironZipPlus.zip" ' Open the existing ZIP archive and add a new file Using file = IronArchive.FromFile("ironZip.zip", archivePlusPath) ' Add additional files to the existing archive file.Add(file3) End Using $vbLabelText $csharpLabel 程式碼使用IronArchive.FromFile靜態方法開啟現有的 ZIP 文件,然後將新文件新增為存檔條目。 文件物件被釋放後,更新後的歸檔檔案即成功儲存。 更新 IronZIP 庫會根據客戶/用戶的回饋不斷更新,所有更新都可以在這裡找到。 結論 總之,在現代應用程式開發中,儲存和資料傳輸費用由雲端主機供應商收取,因此掌握 ZIP 檔案程式設計是一項至關重要的技能。 掌握這項技能可以幫助程式設計師降低應用程式的成本並提高應用程式的效能。 透過依照安裝步驟和探索提供的程式碼範例,開發人員可以快速利用IronZIP的強大功能輕鬆處理 ZIP 任務。 隨著越來越多的應用程式現代化並遷移到雲端,擁有像IronZIP這樣可靠的 ZIP 程式庫可以為 C# 開發人員提供滿足現代應用程式開發需求所需的工具。 因此,盡情享受IronZIP的強大功能,解鎖在 C# 應用程式中處理 ZIP 檔案的新可能性。 IronZIP為其開發者提供全面的支援。 要了解更多關於IronZIP for C# 的工作原理,請造訪這裡。 IronZIP提供免費試用許可證,這是了解IronZIP及其功能的絕佳機會。 Iron Software還擁有各種其他函式庫,探索它們可以獲得更多知識並更新您的技能,從而進行現代應用程式的程式設計/開發。 常見問題解答 如何用 C# 開啟 ZIP 檔案? 您可以使用 System.IO.Compression 函式庫在 C# 中開啟 ZIP 檔案。另外,IronZIP 提供處理 ZIP 檔案的進階功能,提供更簡單、更有效率的方式來管理您的歸檔。 如何使用 C# 從 ZIP 存檔中萃取檔案? 使用 IronZIP,您可以使用 IronArchive.ExtractArchiveToDirectory 方法從 ZIP 存檔中萃取檔案。此方法需要您指定 ZIP 檔案和萃取的目標目錄。 在應用程式開發中使用 ZIP 檔案有什麼好處? ZIP 檔案可縮小檔案大小,從而加快傳輸時間,並可將多個檔案整合為單一存檔。IronZIP 透過指定壓縮比率和支援多種格式等附加功能增強了這些優點。 如何在 C# 中將檔案新增至現有的 ZIP 存檔? 若要使用 IronZIP 將檔案新增至現有的 ZIP 存檔,請使用 IronArchive.FromFile 方法開啟存檔,然後再使用 Add 方法加入新檔案。儲存更新的存檔以完成程序。 我可以使用 IronZIP 建立新的 ZIP 檔案並將檔案加入其中嗎? 是的,使用 IronZIP,您可以透過指定歸檔路徑,並使用 Add 方法新增檔案,以建立新的 ZIP 檔案。然後使用 SaveAs 方法儲存存檔。 如何使用 NuGet Package Manager 安裝 IronZIP? 若要透過 NuGet Package Manager 安裝 IronZIP,請開啟 Visual Studio 中的 Project Manager,搜尋 IronZIP,選擇最新版本,然後按一下安裝。這將把 IronZIP 加入您的專案,啟用 ZIP 檔案管理。 IronZIP 支援多種壓縮格式嗎? 是的,IronZIP 支援多種壓縮格式,包括 ZIP、TAR、GZIP 和 BZIP2,為各種應用程式需求提供彈性。 IronZIP 是否提供免費試用? 是的,IronZIP 為開發人員提供免費試用。您可以在 Iron Software 網站註冊,無需信用卡即可使用試用版。 是什麼讓 IronZIP 成為現代應用程式開發的合適選擇? IronZIP 以易用、快速和跨平台相容性著稱。這些特性加上先進的功能,使其成為現代應用程式開發的理想選擇。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新6月 22, 2025 如何在 C# 中將壓縮檔萃取至目錄 ZIP 檔案是一種方便的方式,可將多個檔案和目錄捆綁成單一歸檔。 閱讀更多 更新7月 28, 2025 如何在 C# 中使用密碼壓縮檔案 在本文中,我們將探討如何使用 C# 和 IronZIP 函式庫建立受密碼保護的 ZIP 檔案 閱讀更多 更新7月 28, 2025 如何在 C# 中解壓縮檔案至目錄 無論您是在開發 Windows 應用程式或 .NET 專案,瞭解解壓縮檔的過程都是非常寶貴的。 閱讀更多 如何在 .NET Core 中解壓縮檔案如何在 C# .NET Core 中建立 Zi...