zoukankan      html  css  js  c++  java
  • Simple zip archive unzipper(转载)

    Introduction

    If you sometimes find yourself in a situation where you need to unzip some files but really don't like the thought of having to roll out the big artillery (i.e., third-party libraries), then this simple zip archive unzipper might be the thing for you. It consists of just a single source file of about 200 lines of code, and is thus easily incorporated into your application. This way, you don't have to worry about third-party library dependencies and related deployment issues; simply compile the simple zip archive unzipper into your application, and you are ready to go.

    The library supports zip-files with no compression, and zip-files compressed with the Deflate algorithm. This includes zip-files generated by the Windows Zip Shell extension (Send To -> Compressed (zipped) folder), zip-files generated by WinZip (with default compression settings at least), and zip-files generated by the Info-Zip tools.

    Background

    Internally, the library only handles some simple parsing of the zip file headers. All the gory details of actually decompressing the data is left to the built-in System.IO.Compression.DeflateStream. As this class is available beginning with .NET 2.0, the library compiles and runs on the .NET 2.0, 3.0, and 3.5 platforms.

    Using the code

    The library contains just one class, SimpleUnZipper, with a few static methods. To unzip a file on disk to a directory on disk, simply use the UnZipTo method:

    Collapse Copy Code
    // Unzip archive to disk
    SimpleUnZipper.UnZipTo(@"c:\zipfile.zip", @"c:\foo\bar");

    This will unzip the files in 'c:\zipfile.zip' and place them in the 'c:\foo\bar' folder, creating a sub-folder structure on disk matching that of the zip-file.

    The UnZipTo method also accepts a Stream as input:

    Collapse Copy Code
    // Unzip from stream to disk
    using (var stream = File.OpenRead(@"c:\zipfile.zip"))
    {
        SimpleUnZipper.UnZipTo(stream, @"c:\foo\bar");
    }

    To get the raw decompressed data from a zip file, use the UnZip methods:

    Collapse Copy Code
    // Unzip archive manually
    foreach (var file in SimpleUnZipper.UnZip(@"c:\zipfile.zip"))
    {
        Console.WriteLine(file.Name);
        // Do something with file.Stream here...
    }

    The UnZip methods return an IEnumerable<UncompressedFile>, where each UncompressedFile has a Name property (the file name) and a Stream property (the decompressed file data). Note that an UncompressedFile might be a directory, in which case, the Name is a directory and the Stream has a length of zero.

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    Anders M. Mikkelsen


    Member
    M.Sc, Computer Science, Daimi, DK
    Occupation: Architect
    Company: Vestas Control Systems A/S
    Location: Denmark Denmark

    转载:http://www.codeproject.com/KB/files/Simple_Unzipper.aspx

  • 相关阅读:
    【算法导论】第8章线性时间排序_计数排序、基数排序、桶排序
    c语言中字符串分割函数及实现
    【算法导论】第12章二叉查找树
    【算法导论】第6章堆排序及利用堆建立最小优先级队列
    【算法导论】第11章散列表
    atoi、itoa,strcpy,strcmp,memcpy等实现
    采用链地址法处理冲突构造哈希表
    【算法导论】第7章快速排序
    Warshall传递闭包算法的学习与实现
    矩阵的加、减、乘、除、求逆运算的实现
  • 原文地址:https://www.cnblogs.com/wuhenke/p/1733016.html
Copyright © 2011-2022 走看看