• 【转】如何使用.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 

  • 相关阅读:
    人脸识别完整项目实战(1):目录大纲篇
    《分布式数据仓库最佳实践》学员答疑实录(2)
    知识图谱完整项目实战(附源码)(3)
    人脸识别完整项目实战(14):实时人脸特征点标定程序设计
    知识图谱完整项目实战(附源码)(2)
    sqlserver查询数据表中每个分类最新的一条记录
    WPF datagrid combobox 使用枚举
    中控考勤机开发-专业性门禁终端
    临时保存
    开源WPF控件库MaterialDesignInXAML推荐
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/2555457.html
Copyright © 2020-2023  润新知