【转】如何使用.NET 4.5中的ZipArchive


 今天 在 http://www.infoq.com/cn/news/2012/06/Net-Zip 看到 .NET 4.5中提供的压缩类,在此做下备份。

 

 http://dotnetzip.codeplex.com/

 

One of the missing feature of .NET framework was a support for Zip file manipulation such as reading the zip archive, adding files, extracting files, etc. and we were using some third party libraries such as excellent the DotNetZip. In .NET 4.5, we have an extensive support for manipulating .zip files.

First thing that you should do is to add System.IO.Compression assembly as reference to your project. You may also want to reference System.IO.Compression.FileSystem assembly to access three extension methods (from theZipFileExtensions class) for the ZipArchive class: CreateEntryFromFileCreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file.

image

Let’s cover the bits and pieces that we get from System.IO.Compression assembly at first. The below sample shows how to read a zip archive easily with ZipArchive class:

static void Main(string[] args) {    
  const string zipFilePath = @"C:\apps\Sample Pictures.zip"
     using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open)) 
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read)) {  
        foreach (var zipArchiveEntry in archive.Entries)  
           Console.WriteLine(                 "FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName             ); 
    }
 }

In this sample, we are opening the zip archive and iterate through the collection of entries. When we run the application, we should see the list of files inside the zip archive:

image

It’s also so easy to add a new file to the zip archive:

static void Main(string[] args) {  
    const string zipFilePath = @"C:\apps\Sample Pictures.zip";   
   using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))  
   using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update)) {  
        ZipArchiveEntry readMeEntry = archive.CreateEntry("ReadMe.txt");    
     using (StreamWriter writer = new StreamWriter(readMeEntry.Open())) {   
          writer.WriteLine("Lorem ipsum dolor sit amet...");        
     writer.Write("Proin rutrum, massa sed molestie porta, urna...");   
      }      
    foreach (var zipArchiveEntry in archive.Entries)  
           Console.WriteLine(        
         "FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName    
         );  
   }
 }

In this sample, we are adding a file named ReadMe.txt at the root of archive and then we are writing some text into that file.

Extracting files is into a folder is so easy as well. You need reference the System.IO.Compression.FileSystem assembly along with System.IO.Compression assembly as mentioned before for this sample:

static void Main(string[] args) {  
    const string zipFilePath = @"C:\apps\Sample Pictures.zip"
    const string dirToExtract = @"C:\apps\Sample Pictures\";  
    using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open)) 
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update)) 
        archive.ExtractToDirectory(dirToExtract);
 }

There are some other handy APIs as well but it is so easy to discover them by yourself. Enjoy Smile 

转载于:https://www.cnblogs.com/chenqingwei/archive/2012/06/19/2555457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值