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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ZLIB库 包装的压缩解压缩文件的源码 VS2005 工程创建 /* */ class ZIPWRAP_EXP CZipper { public: CZipper(); virtual ~CZipper(); // simple interface static bool ZipFile(const char* szFilePath); // saves as same name with .zip static bool ZipFolder(const char* szFilePath, bool ignoreself = false); // saves as same name with .zip bool AddFolderToZipFile(const char*foldername, const char* rootfolder); bool AddFileToZipFile(const char*filename, const char*relfolder = NULL, const char* comment = NULL); bool AddFolderOnlyPathToFile(const char* foldername, const char* comment = NULL); bool OpenZipFile(const char* zipfilename, bool append = false); bool CloseZipFile(const char* global_comment = NULL); private: void* zipfile_;/* = NULL */ }; /* */ #define MAX_COMMENT (255) /* tm_unz contain date/time info */ typedef struct UZ_s { unsigned int tm_sec; /* seconds after the minute - [0,59] */ unsigned int tm_min; /* minutes after the hour - [0,59] */ unsigned int tm_hour; /* hours since midnight - [0,23] */ unsigned int tm_mday; /* day of the month - [1,31] */ unsigned int tm_mon; /* months since January - [0,11] */ unsigned int tm_year; /* years - [1980..2044] */ } UZ_s; // create our own fileinfo struct to hide the underlying implementation struct UZ_FileInfo { char szFileName[260 + 1]; char szComment[255 + 1]; unsigned long dwVersion; unsigned long dwVersionNeeded; unsigned long dwFlags; unsigned long dwCompressionMethod; unsigned long dwDosDate; unsigned long dwCRC; unsigned long dwCompressedSize; unsigned long dwUncompressedSize; unsigned long dwInternalAttrib; unsigned long dwExternalAttrib; bool bFolder; UZ_s tmu_date; }; class ZIPWRAP_EXP CUnZipper { public: CUnZipper(); virtual ~CUnZipper(); // simple interface static bool UnZip( const char* filename, const char* dstfolder, bool ingorepath = false, const char* password = NULL); bool OpenUnZipFile(const char* filename); bool CloseUnZipFile(); bool UnZipTo( const char* dstfolder, bool ingorepath = false, const char* password = NULL); int GetFileCount(); bool GotoFirstFile(); bool GotoNextFile(); bool GotoZipFile(int index); bool GotoZipFile(const char* zipfilename); bool GetCurrentFileInfo(UZ_FileInfo&fileinfo;); bool UnCurrentZipFile(const char* dstfolder, bool ingorepath = false, const char* password = NULL); bool UnOneZipFile(const char* filename, const char* dstfolder, bool ingorepath = false, const char* password = NULL); bool UnOneZipFile(int index, const char* dstfolder, bool ingorepath = false, const char* password = NULL); private: void* unzipfile_; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值