C# 使用 dotNetZip 壓縮與解壓縮檔案

使用dotnetzip 將檔案製作成zip壓縮檔

dotNetZip為一個開放式源碼軟體元件,可整合到.NET開發專案中,是一個容易操作使用的檔案壓縮/解壓縮元件。

本範例使用vs 2019

先安裝,如圖

安裝dotNetZip

壓縮:

ZipFile zip = new ZipFile("(壓所檔放置路徑+檔名+附檔名)");
zip.AddDirectory("(要壓縮的資料夾)");
zip.Password = "password";//壓縮的密碼
zip.Save();

解壓縮

string zipFilePath = "壓縮檔位置";
// 檢查檔案是否存在
if (File.Exists(zipFilePath))
{
    try
    {
        var options = new ReadOptions { StatusMessageWriter = System.Console.Out };
        using (ZipFile zip = ZipFile.Read(zipFilePath, options))
        {
            zip.Password = "password"; // 解壓密碼
            zip.ExtractAll("解壓縮後檔案放置位置");  // 解壓全部
        }
    }
    catch (Exception)
    {
        throw;
    }
}

參考:
https://archive.codeplex.com/?p=DotNetZip

https://www.nuget.org/packages/dotnetzip/

%%sitename%% %%title%% %%primary_category%% doNetZip 壓縮資料夾 %%sep%% 解壓縮 %%sep%% C# %%sep%% 檔案壓縮 %%sep%% doNetZip %%sep%% 壓縮