zoukankan      html  css  js  c++  java
  • 【转】如何使用.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 

  • 相关阅读:
    linux 下查看文件个数及大小
    weblogic日志小结
    Excel数据通过plsql导入到Oracle
    Linux查看外网IP
    linux挂载/卸载优盘
    git版本回退
    linux修改文件所属用户、用户组
    retry.RetryInvocationHandler (RetryInvocationHandler.java:invoke(140))
    Hadoop切换namenode为active
    Netty使用LineBasedFrameDecoder解决TCP粘包/拆包
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/2555457.html
Copyright © 2011-2022 走看看