91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C#壓縮解壓文件處理方案

admin
2025年6月3日 10:17 本文熱度 360

1、通過 System.IO.Compression 命名空間中新增的ZipArchive、ZipFile等類實現。

不需要安裝第三方的組件包,微軟官方的實現,推薦使用

//壓縮
System.IO.Compression.ZipFile.CreateFromDirectory(@"C:\Users\Pride\Pictures\test\123", @"C:\Users\Pride\Pictures\test\123.zip"); 
//解壓
System.IO.Compression.ZipFile.ExtractToDirectory(@"C:\Users\Pride\Pictures\test\123.zip", @"C:\Users\Pride\Pictures\test\1234"); 

2、第三方類庫(DotNetZip的使用)

  • SharpZipLib[1]

  • DotNetZip[2]

SharpZipLib的簡單使用

DotNetZip的簡單使用

壓縮文件

using (ZipFile zip = new ZipFile())
{
  zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
  zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
  zip.AddFile("ReadMe.txt");

  zip.Save("Archive.zip");
}

更新文件,無需解壓

using (ZipFile zip = ZipFile.Read("ExistingArchive.zip"))
{
  // 1. remove an entry, given the name
  zip.RemoveEntry("README.txt");

  // 2. Update an existing entry, with content from the filesystem
  zip.UpdateItem("Portfolio.doc");

  // 3. modify the filename of an existing entry
  // (rename it and move it to a sub directory)
  ZipEntry e = zip["Table1.jpg"];
  e.FileName ="images/Figure1.jpg";

  // 4. insert or modify the comment on the zip archive
  zip.Comment ="This zip archive was updated" + System.DateTime.ToString("G");

  // 5. finally, save the modified archive
  zip.Save();
}

提取文件

using (ZipFile zip = ZipFile.Read("ExistingZipFile.zip"))
{
  foreach (ZipEntry e in zip)
  {
    e.Extract(TargetDirectory, true);  // true => overwrite existing files
  }
}

3、原生 System.IO.Compression 實現 zip 的壓縮與解壓

不需要安裝第三方的組件包,微軟官方的實現,需要添加命名空間using System.IO.Compression;

將指定目錄壓縮為Zip文件

/// <summary>
/// 將指定目錄壓縮為Zip文件
/// </summary>
/// <param name="folderPath">文件夾地址 D:/1/ </param>
/// <param name="zipPath">zip地址 D:/1.zip </param>
public static void CompressDirectoryZip(string folderPath, string zipPath)
{
    DirectoryInfo directoryInfo = new(zipPath);
 
    if (directoryInfo.Parent != null)
    {
        directoryInfo = directoryInfo.Parent;
    }
 
    if (!directoryInfo.Exists)
    {
        directoryInfo.Create();
    }
 
    ZipFile.CreateFromDirectory(folderPath, zipPath, CompressionLevel.Optimal, false);
}

將指定文件壓縮為Zip文件

/// <summary>
/// 將指定文件壓縮為Zip文件
/// </summary>
/// <param name="filePath">文件地址 D:/1.txt </param>
/// <param name="zipPath">zip地址 D:/1.zip </param>
public static void CompressFileZip(string filePath, string zipPath)
{
 
    FileInfo fileInfo = new FileInfo(filePath);
    string dirPath = fileInfo.DirectoryName?.Replace("\\", "/") + "/";
    string tempPath = dirPath + Guid.NewGuid() + "_temp/";
    if (!Directory.Exists(tempPath))
    {
        Directory.CreateDirectory(tempPath);
    }
    fileInfo.CopyTo(tempPath + fileInfo.Name);
    CompressDirectoryZip(tempPath, zipPath);
    DirectoryInfo directory = new(path);
    if (directory.Exists)
    {
        //將文件夾屬性設置為普通,如:只讀文件夾設置為普通
        directory.Attributes = FileAttributes.Normal;
 
        directory.Delete(true);
    }
}

解壓Zip文件到指定目錄(壓縮單個文件的邏輯其實就是先將我們要壓縮的文件復制到一個臨時目錄,然后對臨時目錄執行了壓縮動作,壓縮完成之后又刪除了臨時目錄)

/// <summary>
/// 解壓Zip文件到指定目錄
/// </summary>
/// <param name="zipPath">zip地址 D:/1.zip</param>
/// <param name="folderPath">文件夾地址 D:/1/</param>
public static void DecompressZip(string zipPath, string folderPath)
{
    DirectoryInfo directoryInfo = new(folderPath);
 
    if (!directoryInfo.Exists)
    {
        directoryInfo.Create();
    }
 
    ZipFile.ExtractToDirectory(zipPath, folderPath);
}
復制

資料參考

  • C# 壓縮或解壓_WenyueQ°的博客-CSDN博客_c# 解壓[3]

  • .NET中zip的壓縮和解壓 - Asharp - 博客園[4]

  • 使用C#和System.IO.Packaging以編程方式從Zip存檔中提取文件 | 碼農家園[5]

  • C# 使用原生 System.IO.Compression 實現 zip 的壓縮與解壓_大哥手下留情的博客-CSDN博客[6]

引用鏈接

[1] SharpZipLib: http://www.icsharpcode.net/opensource/sharpziplib/
[2] DotNetZip: http://dotnetzip.codeplex.com/
[3] C# 壓縮或解壓_WenyueQ°的博客-CSDN博客_c# 解壓: https://blog.csdn.net/u014325666/article/details/126298552
[4] .NET中zip的壓縮和解壓 - Asharp - 博客園: https://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html
[5] 使用C#和System.IO.Packaging以編程方式從Zip存檔中提取文件 | 碼農家園: https://www.codenong.com/507751/
[6] C# 使用原生 System.IO.Compression 實現 zip 的壓縮與解壓_大哥手下留情的博客-CSDN博客: https://blog.csdn.net/dageliuqing/article/details/127177937


該文章在 2025/6/3 15:22:34 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 乱码在线观看 | 国产福利久 | 国产凸凹视频熟女a | 中文字幕亚洲无限码 | 人妖欧美精品二区 | 国产精品秘入口免 | 日韩视频一区 | www.五月天激情 | 精品国产自永久 | 果冻剧精品传媒入口 | 日韩二区三 | 精品区在线观看 | 国产h精品在线观看 | 日韩欧美亚洲精品 | 国产日韩一区 | 欧美专区在线观看 | 国产精品高清99 | 国产素人自拍 | 日韩交换精品 | 日韩色片在线观看 | 成人九九九 | 日本人視頻網站一 | 韩国日本免费不 | 九九中文字幕国产 | 91视频免费福利 | 伦理在线电影 | 日本欧美国产婷婷 | 97成人免费视频 | 岛国大片网站 | 国产精品黄在线 | 国产精九九 | 成人亚洲欧美综合 | 国产一区二区免费 | 国产女主播 | 国内性爱精品亚洲 | 国产精品福利免费 | 日韩欧美亚洲国产 | 午夜家庭影院 | 国产亚洲蜜 | 区三区免费中文字幕 | 另类图片欧美小 |