使用「ZipArchive 類別」撰寫檔案壓縮、解壓縮範例

一、開發環境 整合開發環境:Visual Studio 2019 語言:C# .net framework:4.6.2 二、組件 圖、組件 System.IO.Compression 三、專案結構介紹 圖、建置 Test/Compression 、 Test/Decompression 資料夾、在 Test/Compression 建置幾個測試檔案,在 Test/Decompression 建置 Test.zip 壓縮檔 四、解壓縮 原始碼 C# /// <summary> /// 解壓縮 /// </summary> private static void Decompression() { var path = $"Test/Decompression/"; using (var fs = new FileStream($"{path}\\Test.zip", FileMode.Open)) { using (var archive = new ZipArchive(fs, ZipArchiveMode.Read, true)) { foreach (var entry in archive.Entries) { if (File.Exists($"{path}\\{entry.Name}")) { File.Delete($"{path}\\{entry.Name}"); } entry.ExtractToFile($"{path}\\{entry.Name}"); } } } } 圖、執行結果 五、壓縮 原始碼 C# /// <summary> /// 壓縮 /// </summary> private static void Compressio...