舟遥遥以轻飏:博客吧

来源:百度文库 编辑:九乡新闻网 时间:2024/04/26 05:18:42
<<  日语能力测试三级考试主要句型  | 首页 | 深入研究虚函数和vtable  >>
不使用任何lib和dll,压缩/解压缩文件(zip格式)
时间: 2004-11-03
http://www.vccode.com/file_show.php?id=1935&select=1935
作者:Hans Dietrich  译自:CodeProject
摘要
这篇文章介绍XZip和XUnzip两个文件,功能是不需要在你的工程中加入任何的.lib或.dll文件,完成压缩和解压缩的功能(zip格式)。
首先,让我感谢Lucian Wischik的工作,他把从Info-ZIP得到的很多.c和.h文件转化对应的.cpp和.h文件,这些文件是XZip的基础。
XZip和XUnzip的功能
大多数的函数在XZip演示工程中都有使用演示。这里列出其中重要的一些函数:
CreateZip() - 创建zip档案文件 ///////////////////////////////////////////////////////////////// // // CreateZip() // // Purpose: Create a zip archive file // // Parameters: z - archive file name if flags is ZIP_FILENAME; // for other uses see below // len - for memory (ZIP_MEMORY) should be the buffer // size; for other uses, should be 0 // flags - indicates usage, see below; for files, this // will be ZIP_FILENAME // // Returns: HZIP - non-zero if zip archive created ok, otherwise 0 //
ZipAdd() - 向zip档案中添加一个文件 ///////////////////////////////////////////////////////////////// // // ZipAdd() // // Purpose: Add a file to a zip archive // // Parameters: hz - handle to an open zip archive // dstzn - name used inside the zip archive to identify // the file // src - for a file (ZIP_FILENAME) this specifies the // filename to be added to the archive; for // other uses, see below // len - for memory (ZIP_MEMORY) this specifies the // buffer length; for other uses, this should // be 0 // flags - indicates usage, see below; for files, this // will be ZIP_FILENAME // // Returns: ZRESULT - ZR_OK if success, otherwise some other value //
OpenZip() - 打开一个已存在的zip档案文件 ///////////////////////////////////////////////////////////////// // // OpenZip() // // Purpose: Open an existing zip archive file // // Parameters: z - archive file name if flags is ZIP_FILENAME; // for other uses see below // len - for memory (ZIP_MEMORY) should be the buffer // size; for other uses, should be 0 // flags - indicates usage, see below; for files, this // will be ZIP_FILENAME // // Returns: HZIP - non-zero if zip archive opened ok, otherwise 0 //
GetZipItem() - 得到打开的zip档案文件中的相关条目的信息 ///////////////////////////////////////////////////////////////// // // GetZipItem() // // Purpose: Get information about an item in an open zip archive // // Parameters: hz - handle of open zip archive // index - index number (0 based) of item in zip // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW // struct (if Unicode) // // Returns: ZRESULT - ZR_OK if success, otherwise some other value //
FindZipItem() - 通过名称查找条目并返回相关信息 ///////////////////////////////////////////////////////////////// // // FindZipItem() // // Purpose: Find item by name and return information about it // // Parameters: hz - handle of open zip archive // name - name of file to look for inside zip archive // ic - TRUE = case insensitive // index - pointer to index number returned, or -1 // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW // struct (if Unicode) // // Returns: ZRESULT - ZR_OK if success, otherwise some other value //
UnzipItem() - 通过索引找到相关条目并对其解压缩 ///////////////////////////////////////////////////////////////// // // UnzipItem() // // Purpose: Find item by index and unzip it // // Parameters: hz - handle of open zip archive // index - index number of file to unzip // dst - target file name of unzipped file // len - for memory (ZIP_MEMORY. length of buffer; // otherwise 0 // flags - indicates usage, see below; for files, this // will be ZIP_FILENAME // // Returns: ZRESULT - ZR_OK if success, otherwise some other value //
CloseZip() - 关闭已存在的档案条目 ///////////////////////////////////////////////////////////////// // // CloseZip() // // Purpose: Close an open zip archive // // Parameters: hz - handle to an open zip archive // // Returns: ZRESULT - ZR_OK if success, otherwise some other value //
使用方法
为了在你的工程中使用XZip,首先你需要在你的工程中添加以下文件:
XZip.cpp
XZip.h
XUnzip.cpp
XUnzip.h
如果你在已包含XZip的工程中使用了precompiled headers,那么你必须改变C/C++ Precompiled Headers的设置,使用Not using precompiled headers。(见常见问题2,soarlove注)
接下来,在工程的适当文件中加入XZip.h和XUnzip.h声明。现在,你已经做好了使用XZip准备。在使用中有许多的注意事项,在使用函数时,请仔细查看相关函数中的说明。
演示程序
XZipTest.exe演示程序演示了在程序中使用XZip和XUnzip中API的方法。比如:

常见问题
我可以在非MFC工程中使用XZip吗? 可以。这写函数可以在任何的Win32程序中使用。
当我尝试把XZip.cpp文件引入我的MFC工程时,在XZip.cpp(2918)得到一个编译错误: fatal error C1010: unexpected end of file while looking for precompiled header directive。怎样修复? 当你在工程中使用precompiled headers时, 那么你必须改变C/C++ Precompiled Headers的设置,使用Not using precompiled headers。如下图:

当我编译演示代码时,得到如下错误error   LINK : fatal error LNK1104: cannot open file "mfc42u.lib" Error executing link.exe。怎样修复? Visual C++ v6.0在默认安装中没有安装Unicode库文件,所以产生了找不到mfc42u.lib或mfc42ud.lib的错误。修复方法,1.你可以使用VC++安装CD安装Unicode库文件。2.在Build | Set Active Configuration中选择一个非Unicode编译配置。

我不需要使用Zip/Unzip函数,是否可以排斥(exclude)XZip.cpp/XUnzip.cpp文件? 可以,你只需要在你的工程中引入include你需要部分的.h/.cpp文件
我可以在我们的共享/商业软件中使用XZip吗? 可以,你可以使用XZip,不需要支付任何的费用,但是你需要遵守在XZip.cpp文件中的关于使用Info-ZIP的限制。
1
2
3
4
5
6
7
8
9
10
11
12