参考:https://blog.csdn.net/zhaotianff/article/details/141156035
using System.IO.Compression;
namespace XCG.Commons
{
    public class ZipUtil
    {
        /// <summary>
        /// 压缩zip
        /// </summary>
        /// <param name="inputFolder">要压缩的目录</param>
        /// <param name="destinationZipPath">压缩后zip存放地址</param>
        public static void CreateZip(string inputFolder, string destinationZipPath)
        {
            if (Directory.Exists(inputFolder))
            {
                ZipFile.CreateFromDirectory(inputFolder, destinationZipPath);
            }
        }

        /// <summary>
        /// 解压zip
        /// </summary>
        /// <param name="zipFilePath">zip压缩包地址</param>
        /// <param name="extractPath">解压到此目录</param>
        public static void ExtractZip(string zipFilePath, string extractPath)
        {
            if (File.Exists(zipFilePath))
            {
                ZipFile.ExtractToDirectory(zipFilePath, extractPath);
            }
        }
    }
}

 测试代码

 [HttpGet]
 public void GenRarZip()
 {
     //获取项目路径
     var webRootPath = this._env.ContentRootPath;
     var fileDir = $"UploadFile\\123\\";
     var dirPath = Path.Combine(webRootPath, fileDir);
     //压缩包存放地址
     var tarDir = $"ZipFile\\123.zip";
     var tarPath = Path.Combine(webRootPath, tarDir);
     ZipUtil.CreateZip(dirPath, tarPath);
 }

 

posted on 2024-10-14 10:04  邢帅杰  阅读(15)  评论(0编辑  收藏  举报