先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); } } } }
文章评论