C#-Zip解压与压缩示例

// 添加NuGet包:System.IO.Compression.ZipFile

/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描    述:Zip相关的工具类                                                   
*│ 作    者:执笔小白                                              
*│ 版    本:1.0                                       
*│ 创建时间:2022-04-8 15:40:56                            
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Blog.Core.Common.Helper                               
*│ 类    名:ZipFileHelper                                     
*└──────────────────────────────────────────────────────────────┘
*/
using System.IO.Compression;

namespace Blog.Core.Common.Helper
{
    public class ZipFileHelper
    {
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="sourcePath">指定文件夹</param>
        /// <param name="zipFilePath">压缩到zip文件路径</param>
        /// <param name="err"></param>
        /// <returns></returns>
        public static bool CreateZipFile(string sourcePath, string zipFilePath)
        {
            ZipFile.CreateFromDirectory(sourcePath, zipFilePath);

            return true;
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="zipFilePath">压缩文件路径</param>
        /// <param name="unZipDir">解压文件存放路径。为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
        /// <param name="err">出错信息</param>
        /// <returns></returns>
        public static bool UnZipFile(string zipFilePath, string unZipDir)
        {
            ZipFile.ExtractToDirectory(zipFilePath, unZipDir, Encoding.GetEncoding("GBK"), true);
            
            return true;
        }
    }
}
posted @   ꧁执笔小白꧂  阅读(421)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
历史上的今天:
2021-04-09 html改cshtml方案
点击右上角即可分享
微信分享提示