首页 > 编程语言 >C# 使用原生 System.IO.Compression 实现 zip 的压缩与解压

C# 使用原生 System.IO.Compression 实现 zip 的压缩与解压

时间:2022-11-17 22:13:14浏览次数:66  
标签:Compression zip C# 压缩 zipPath directoryInfo tempPath string

C# 使用原生 System.IO.Compression 实现 zip 的压缩与解压

 

zip 是一个非常常见的压缩包格式,本文主要用于说明如何使用代码 文件或文件夹压缩为 zip压缩包及其解压操作,
我们采用的是 微软官方的实现,所以也不需要安装第三方的组件包。

使用的时候记得 using System.IO.Compression;

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

其中 CompressionLevel 是个枚举,支持下面四种类型

枚举注解
Optimal 0 压缩操作应以最佳方式平衡压缩速度和输出大小。
Fastest 1 即使结果文件未可选择性地压缩,压缩操作也应尽快完成。
NoCompression 2 该文件不应执行压缩。
SmallestSize 3 压缩操作应尽可能小地创建输出,即使该操作需要更长的时间才能完成。

我方法这里直接固定了采用 CompressionLevel.Optimal,大家可以根据个人需求自行调整。

/// <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(tempPath);
    if (directory.Exists)
    {
        //将文件夹属性设置为普通,如:只读文件夹设置为普通
        directory.Attributes = FileAttributes.Normal;

        directory.Delete(true);
    }
}

压缩单个文件的逻辑其实就是先将我们要压缩的文件复制到一个临时目录,然后对临时目录执行了压缩动作,压缩完成之后又删除了临时目录。

/// <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# 使用原生 System.IO.Compression 实现 zip 的压缩与解压 就讲解完了,有任何不明白的,可以在文章下面评论或者私信我,欢迎大家积极的讨论交流,有兴趣的朋友可以关注我目前在维护的一个 .NET 基础框架项目,项目地址如下
https://github.com/berkerdong/NetEngine.git
https://gitee.com/berkerdong/NetEngine.git

标签:Compression,zip,C#,压缩,zipPath,directoryInfo,tempPath,string
From: https://www.cnblogs.com/sexintercourse/p/16901181.html

相关文章

  • MAC安装NVM
    curl-o-https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh|bash nvm:commandnotfountcd~/.nvmopen.bash_profile###addexportNVM......
  • LeetCode刷题(6)【栈】有效的括号(C语言)
    有效的括号20.有效的括号-力扣(LeetCode)(leetcode-cn.com)​思路:是左括号,就入栈,是右括号,就与栈顶的左括号判断是否匹配,如果匹配,继续,不匹配就终止。从第79行开始,前面都是......
  • 【C++】如果你准备学习C++,并且有C语言的基础,我希望你能简单的过一遍知识点。
    相关视频——黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili(1-83)我的小站——半生瓜のblog我知道这个视频早已经被很多人学习并且记录​笔记,......
  • LeetCode刷题(8)【栈&队列】用栈实现队列(C语言)
    用栈实现队列232.用栈实现队列-力扣(LeetCode)(leetcode-cn.com)类似题目——用队列实现栈​​LeetCode刷题(7)【栈&队列】用队列实现栈(C语言)_半生瓜のblog-CSDN博客​......
  • 【树】之二叉树(C语言)(含图解)
    树&二叉树​​树​​​​树的概念及结构​​​​树的概念​​​​树的要求​​​​树的表示​​​​现实应用​​​​二叉树​​​​概念​​​​特殊的二叉树​​​​注意......
  • LeetCode刷题(9)【树】前序&深度&平衡(C语言)
    ​二叉树的前序遍历144.二叉树的前序遍历-力扣(LeetCode)(leetcode-cn.com)本题中,对于C++或者Java等语言,返回的是它们的数据结构库里面的数据结构,而C语言没有,这也就是如果......
  • NowCoder刷题(1)【树】二叉树的遍历(含图解)
    二叉树的遍历(IO型)二叉树遍历_牛客题霸_牛客网(nowcoder.com)题目描述如图所示的这棵树前序输出结果为A-B-D-#-#-E-#-#-C-#-#还原过程示例1示例2——前序遍历还原代码实现......
  • C++学习------cstdint头文件的源码学习02---宏定义
    续接上文讲解常见整型的上下限这里事先定义了如下整形的上下限占用位数有符号/无符号上限下限8有符号127-1288无符号255016有符号32767-3276816无符号65535032有符号2147483......
  • javascript-代码随想录训练营day2
    59.螺旋矩阵Ⅱ题目链接:https://leetcode.cn/problems/spiral-matrix-ii/题目描述:给你一个正整数n,生成一个包含1到n2所有元素,且元素按顺时针顺序螺旋排列的nxn......
  • zynq系列之-----PS端iic使用
    本文主要讲述zynq的iic使用。此IIC只能作为主站,作为从站的不适合本文。Iic的接口在ps端。(iic的接口在pl端的情况下,不适合本文)使用软件版本:vivado2018.3pl端设置:转载:xi......