日韩欧美人妻无码精品白浆,夜夜嗨AV免费入口,国产欧美官网在线看,高校回应聋哑女生因长相完美被质疑

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

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

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

1、通過(guò) System.IO.Compression 命名空間中新增的ZipArchive、ZipFile等類(lèi)實(shí)現(xiàn)。

不需要安裝第三方的組件包,微軟官方的實(shí)現(xiàn),推薦使用

//壓縮
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、第三方類(lèi)庫(kù)(DotNetZip的使用)

  • SharpZipLib[1]

  • DotNetZip[2]

SharpZipLib的簡(jiǎn)單使用

DotNetZip的簡(jiǎn)單使用

壓縮文件

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");
}

更新文件,無(wú)需解壓

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 實(shí)現(xiàn) zip 的壓縮與解壓

不需要安裝第三方的組件包,微軟官方的實(shí)現(xiàn),需要添加命名空間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)
    {
        //將文件夾屬性設(shè)置為普通,如:只讀文件夾設(shè)置為普通
        directory.Attributes = FileAttributes.Normal;
 
        directory.Delete(true);
    }
}

解壓Zip文件到指定目錄(壓縮單個(gè)文件的邏輯其實(shí)就是先將我們要壓縮的文件復(fù)制到一個(gè)臨時(shí)目錄,然后對(duì)臨時(shí)目錄執(zhí)行了壓縮動(dòng)作,壓縮完成之后又刪除了臨時(shí)目錄)

/// <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);
}
復(fù)制

資料參考

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

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

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

  • C# 使用原生 System.IO.Compression 實(shí)現(xiàn) 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存檔中提取文件 | 碼農(nóng)家園: https://www.codenong.com/507751/
[6] C# 使用原生 System.IO.Compression 實(shí)現(xiàn) zip 的壓縮與解壓_大哥手下留情的博客-CSDN博客: https://blog.csdn.net/dageliuqing/article/details/127177937


該文章在 2025/6/3 15:22:34 編輯過(guò)
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved