前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#压缩解压文件处理方案

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

作者头像
郑子铭
发布2023-11-08 13:41:22
2210
发布2023-11-08 13:41:22
举报

1、通过 System.IO.Compression 命名空间中新增的ZipArchive、ZipFile等类实现。

不需要安装第三方的组件包,微软官方的实现,推荐使用

代码语言:javascript
复制
//压缩
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的简单使用

压缩文件

代码语言:javascript
复制
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");
}

更新文件,无需解压

代码语言:javascript
复制
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();
}

提取文件

代码语言:javascript
复制
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文件

代码语言:javascript
复制
/// <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文件

代码语言:javascript
复制
/// <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文件到指定目录(压缩单个文件的逻辑其实就是先将我们要压缩的文件复制到一个临时目录,然后对临时目录执行了压缩动作,压缩完成之后又删除了临时目录)

代码语言:javascript
复制
/// <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

推荐阅读:

遥遥领先,开源一个 .NET 构建的个人网盘

.NET中的数组在内存中如何布局?

.NET Web新人入门必学项目EarthChat

改进版 .NET 雪花算法组件

推荐一个基于 .NET 开源的消息通知项目

推荐10个.Net通用权限管理开源项目

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-11-08,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 DotNet NB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、通过 System.IO.Compression 命名空间中新增的ZipArchive、ZipFile等类实现。
  • 2、第三方类库(DotNetZip的使用)
    • SharpZipLib的简单使用
      • DotNetZip的简单使用
      • 3、原生 System.IO.Compression 实现 zip 的压缩与解压
      • 资料参考
        • 引用链接
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
        http://www.vxiaotou.com