ZIP 压缩文件的类库
public static class ZipHelper { /// <summary> /// 压缩一个文件 /// </summary> /// <param name="srcPath">源文件全路径</param> /// <param name="destPath">压缩后文件全路径</param> public static void ZipFile(string srcPath, string destPath) { using (var zip = new ZipFile()) { Ionic.Zip.ZipEntry e = zip.AddFile(srcPath, @"\"); e.Comment = "Added by Cheeso's CreateZip utility."; zip.Comment = String.Format("This zip archive was created by the CreateZip example application on machine '{0}'", Dns.GetHostName()); zip.Save(destPath); } } /// <summary> /// 压缩一个目录下的所有文件 /// </summary> /// <param name="filePath">文件根目录</param> /// <param name="savePath">保存为zip包目录</param> public static void ZipDirFile(string filePath, string savePath) { using (ZipFile zip = new ZipFile()) { String[] filenames = Directory.GetFiles(filePath); foreach (String filename in filenames) { Ionic.Zip.ZipEntry e = zip.AddFile(filename, @"\"); e.Comment = "Added by Cheeso's CreateZip utility."; } zip.Comment = String.Format("This zip archive was created by the CreateZip example application on machine '{0}'", Dns.GetHostName()); zip.Save(savePath); } } public static byte[] ZipFiles(string[] filesPath) { using (ZipFile zip = new ZipFile()) { foreach (String filename in filesPath) { Ionic.Zip.ZipEntry e = zip.AddFile(filename, @"\"); //e.Comment = "Added by Cheeso's CreateZip utility."; } zip.Comment = String.Format("This zip archive was created by the application on machine '{0}'", Dns.GetHostName()); using (MemoryStream stream = new MemoryStream()) { zip.Save(stream); return stream.ToArray(); } } } //压缩文件夹:文件夹下包含文件夹,filePath:压缩的文件路径,savePath:压缩后保存的文件路径 public static void ZipDir(string filePath, string savePath) { using (ZipFile zip = new ZipFile()) { zip.StatusMessageTextWriter = Console.Out; zip.AddDirectory(filePath, @"\"); zip.Save(savePath); } } /// <summary> /// 解压缩,结果包含在文件夹内 /// </summary> /// <param name="zipFilePath">zip包源路径</param> /// <param name="savePath">解压后的文件夹保存路径</param> public static void UnZipFile(string zipFilePath, string savePath) { try { using (var s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); string serverFolder = savePath; Directory.CreateDirectory(serverFolder + "/" + directoryName); if (fileName != String.Empty) { using (FileStream streamWriter = File.Create((serverFolder + "/" + theEntry.Name))) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } } s.Close(); } } catch (Exception ex) { throw ex; } } /// <summary> /// 解压缩,结果不包含文件夹 /// </summary> /// <param name="zipFilePath">zip包源路径</param> /// <param name="savePath">解压后的文件保存路径</param> public static void UnzipFileWithoutFolder(string zipFilePath, string savePath) { var s = new ZipInputStream(File.OpenRead(zipFilePath)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string fileName = Path.GetFileName(theEntry.Name); if (fileName != String.Empty) { FileStream streamWriter = File.Create((savePath)); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); } }
初心商城:初心商城
作者:韩迎龙(Kencery) MVC/.NET群:159227188如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏一杯咖啡,本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构