ZipArchive 的使用
新建一个项目,首先添加 System.IO.Compression.FileSystem 引用。
解压文件
using System.IO.Compression; namespace cl { static void Main() { string zipPath = @"c:\example\result.zip"; string extractPath = @"c:\example\extract"; ZipFile.ExtractToDirectory(zipPath, extractPath); } }
压缩单个文件
using System; using System.IO.Compression; namespace cl { sealed class ZipCreater { static void Main() { using (var zip = ZipFile.Open("ZipCreater.zip", ZipArchiveMode.Create)) { zip.CreateEntryFromFile(@"C:\work\ZipCreater.cs", "ZipCreater.cs"); zip.CreateEntryFromFile("ZipCreater.exe", "ZipCreater.exe"); } } } }
压缩文件夹
using System; using System.IO.Compression; namespace cl { sealed class Zip { static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Usage: Zip zip-file-name directory-name"); return; } try { ZipFile.CreateFromDirectory(args[1], args[0]); } catch (Exception ex) { Console.Error.WriteLine(ex.Message); } } } }
参考文章:
浅谈 ZipArchive 类 - 银河 - 博客园
http://www.cnblogs.com/skyivben/archive/2012/03/09/2388482.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步