LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#压缩解压文件处理方案

admin
2025年6月3日 10:17 本文热度 364

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