首页 文章详情

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

DotNet NB | 4 2023-11-11 22:52 0 0 0
UniSMS (合一短信)

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


推荐阅读:
遥遥领先,开源一个 .NET 构建的个人网盘
.NET中的数组在内存中如何布局?
.NET Web新人入门必学项目EarthChat
改进版 .NET 雪花算法组件
推荐一个基于 .NET 开源的消息通知项目
推荐10个.Net通用权限管理开源项目

点击下方卡片关注DotNet NB

一起交流学习

▲ 点击上方卡片关注DotNet NB,一起交流学习

请在公众号后台

回复 【路线图】获取.NET 2023开发者路线图
回复 【原创内容】获取公众号原创内容
回复 【峰会视频】获取.NET Conf开发者大会视频
回复 【个人简介】获取作者个人简介
回复 【年终总结】获取作者年终总结
回复 加群加入DotNet NB 交流学习群

长按识别下方二维码,或点击阅读原文。和我一起,交流学习,分享心得。



good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter