陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

C# 压缩解压帮助类ZipHelper

2021年11月9日 1010点热度 0人点赞 0条评论

先nuget搜索安装System.IO.Compression.ZipFile

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;

namespace Build_Exe
{
    public class ZipHelper
    {

        /// <summary>
        /// 压缩单文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="savePath">压缩包路径</param>
        /// <param name="isReserved">是否保存原文件</param>
        public static void CompressSingleFile(string filePath, string savePath, bool isReserved = true)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException($"{filePath}不存在.");
            }
            FileInfo file = new FileInfo(filePath);
            string filename = Path.GetFileNameWithoutExtension(filePath);
            string tempDir = Path.Combine(file.DirectoryName, "temp_Zip");
            if (!File.Exists(tempDir))
            {
                Directory.CreateDirectory(tempDir);
            }
            //默认保留原文件
            if (isReserved)
            {
                file.CopyTo(Path.Combine(tempDir, file.Name));
            }
            else
            {
                file.MoveTo(Path.Combine(tempDir, file.Name));
            }
            
            ZipFile.CreateFromDirectory(tempDir, savePath);
            DeleteDir(tempDir);
        }

        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <param name="savePath">压缩包路径</param>
        /// <param name="isReserved">是否保存原文件</param>
        public static void CompressSingleDir(string dirPath, string savePath, bool isReserved = true)
        {
            ZipFile.CreateFromDirectory(dirPath, savePath);
            if (!isReserved)
            {
                DeleteDir(dirPath);
            }
        }

        /// <summary>
        /// 多文件压缩
        /// </summary>
        /// <param name="filePaths">文件夹路径数组</param>
        /// <param name="savePath">压缩包路径</param>
        /// <param name="isReserved">是否保存原文件</param>
        public static void CompressMultiFile(string[] filePaths, string savePath, bool isReserved = true)
        {
            string tempDir = Path.Combine(Path.GetDirectoryName(savePath), "temp_Zip");
            if (!File.Exists(tempDir))
            {
                Directory.CreateDirectory(tempDir);
            }
            foreach (string filePath in filePaths)
            {
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException($"{filePath}不存在.");
                }
                FileInfo file = new FileInfo(filePath);
                string filename = Path.GetFileNameWithoutExtension(filePath);
                //默认保留原文件
                if (isReserved)
                {
                    file.CopyTo(Path.Combine(tempDir, file.Name));
                }
                else
                {
                    file.MoveTo(Path.Combine(tempDir, file.Name));
                }
            }
            ZipFile.CreateFromDirectory(tempDir, savePath);
            DeleteDir(tempDir);
        }

        /// <summary>
        /// 文件解压
        /// </summary>
        /// <param name="zipPath">压缩包路径</param>
        /// <param name="saveDir">解压保存文件夹</param>
        /// <param name="isReserved">是否保存压缩包</param>
        public static void DeCompressMulti(string zipPath, string saveDir, bool isReserved = true)
        {
            ZipFile.ExtractToDirectory(zipPath, saveDir);
            if (!isReserved)
            {
                File.Delete(zipPath);
            }
        }
        /// <summary>
        /// 删除文件夹及文件夹下文件
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        private static void DeleteDir(string dirPath)
        {
            //去除文件夹和子文件的只读属性
            //去除文件夹的只读属性
            DirectoryInfo fileInfo = new DirectoryInfo(dirPath);
            fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
            //去除文件的只读属性
            File.SetAttributes(dirPath, FileAttributes.Normal);
            //判断文件夹是否还存在
            if (Directory.Exists(dirPath))
            {
                foreach (string file in Directory.GetFileSystemEntries(dirPath))
                {
                    if (File.Exists(file))
                    {
                        //如果有子文件删除文件
                        File.Delete(file);
                    }
                    else
                    {
                        //循环递归删除子文件夹
                        DeleteDir(file);
                    }
                }
                //删除空文件夹
                Directory.Delete(dirPath);
            }
        }
    }
}

 

标签: 暂无
最后更新:2021年11月9日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
nginx python centos linux js winform C# DevExpress
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
python快速把office文档execl或者word等转成pdf NPOI读写execl django学习笔记 主程序ui线程异常处理方案和只允许同时运行一个进程的方法 drools开源规则引擎部署方案-在centos的docker部署 paddleX 模型标注后数据集转换

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS