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

 

posted @   小丸  阅读(2641)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示