- Download source code-1.81 KB
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 inreceivated 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 feature des 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-inSystem. 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 useUnzipto
Method:
Collapse Copy code
//Unzip archive to diskSimpleunzipper. unzipto (@"C: \ zipfile.zip",@"C: \ Foo \ bar");
This will unzip the files in'C: \ zipfile.zipAnd place them in'C: \ Foo \ bar'Folder, creating a sub-folder structure on disk matching that of the zip-file.
TheUnzipto
Method also acceptsStream
As input:
Collapse Copy code
//Unzip from stream to diskUsing(VAR stream = file. openread (@"C: \ zipfile.zip") {Simpleunzipper. unzipto (stream,@"C: \ Foo \ bar");}
To get the raw decompressed data from a zip file, useUnzip
Methods:
Collapse Copy code
//Unzip archive manuallyForeach(Var fileInSimpleunzipper. Unzip (@"C: \ zipfile.zip") {Console. writeline (file. Name );//Do something with file. stream here...}
TheUnzip
Methods returnIenumerable<Uncompressedfile>
, Where eachUncompressedfile
HasName
Property (the file name) andStream
Property (the decompressed file data). Note thatUncompressedfile
Might be a directory, in which case,Name
Is a directory andStream
Has a length of zero.
Reprinted: http://www.codeproject.com/KB/files/Simple_Unzipper.aspx