zoukankan      html  css  js  c++  java
  • SharpZipLib 的使用

    SharpZipLib 是一个免费的Zip操作类库,可以利用它对 ZIP 等多种格式进行压缩与解压。下载网址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx。目前的版本为0.86。

    1、创建zip文件,并添加文件:

    using (ZipFile zip = ZipFile.Create(@"E:\test.zip")) 

        zip.BeginUpdate(); 
        zip.Add(
    @"E:\文件1.txt"); 
        zip.Add(
    @"E:\文件2.txt"); 
        zip.CommitUpdate(); 

     

    2、将文件夹压缩为文件

    (new FastZip()).CreateZip(@"E:\test.zip"@"E:\test\"true"");

     

    最后一个参数是使用正则表达式表示的过滤文件规则。CreateZip方法有3个重载版本,其中有目录过滤参数、文件过滤参数及用于指定是否进行子目录递归的一个bool类型的参数。

    3、将文件添加到已有zip文件中

    using (ZipFile zip = new ZipFile(@"E:\test.zip"))
    {
        zip.BeginUpdate();
        zip.Add(
    @"E:\test.doc");
        zip.CommitUpdate();
    }

     

    4、列出zip文件中文件

    using (ZipFile zip = new ZipFile(@"E:\test.zip"))
    {
        
    string list = string.Empty;
        
    foreach (ZipEntry entry in zip)
        {
            list 
    += entry.Name + "\r\n";
        }
        MessageBox.Show(list);
    }

     

    5、删除zip文件中的一个文件

    using (ZipFile zip = new ZipFile(@"E:\test.zip"))
    {
        zip.BeginUpdate();
        zip.Delete(
    @"test.doc");
        zip.Delete(
    @"test22.txt");
        zip.CommitUpdate();
    }

     

     6、解压zip文件中文件到指定目录下

    (new FastZip()).ExtractZip(@"E:\test.zip"@"E:\test\""");

     

    7、常用类

    ZipInputStream、GZipInputStream用于解压缩Deflate、GZip格式流,ZipOutputStream、GZipOutputStream用于压缩Deflate、GZip格式流。
    StreamUtil类包含了几个Stream处理辅助方法:
      1) Copy(Stream, Stream, Byte[])用于从一个Stream对象中复制数据到另一Stream对象。有多个重写。
      2) ReadFully(Stream, Byte [])用于从Stream对象中读取所有的byte数据。有多个重写。

    声明:本文转自:http://www.cnblogs.com/colder/archive/2011/04/07/2008185.html

     

  • 相关阅读:
    LeetCode "Median of Two Sorted Arrays"
    LeetCode "Distinct Subsequences"
    LeetCode "Permutation Sequence"

    LeetCode "Linked List Cycle II"
    LeetCode "Best Time to Buy and Sell Stock III"
    LeetCode "4Sum"
    LeetCode "3Sum closest"
    LeetCode "3Sum"
    LeetCode "Container With Most Water"
  • 原文地址:https://www.cnblogs.com/nikyxxx/p/2620579.html
Copyright © 2011-2022 走看看