ZipArchive 的使用

来源:互联网 发布:绵阳自驾租车软件 编辑:程序博客网 时间:2024/05/23 23:09

新建一个项目,首先添加 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); }    }  }}


原创粉丝点击