使用的XZip版本1.3 源程序可在Codeproject上获得,本教程实例代码下载
- 建立基于对话框的MFC程序(XZip是不依赖MFC)
- 添加XZip到项目,并添加头文件引用,至此可以使用XZip。
在编译的时候,若有报错如下:
对XUnzip.cpp与XZip不适用预编译头
重新编译,无报错。
几个常用函数简介:
- CreateZip()--创建压缩文件。
<pre class="cpp" name="code">HZIP CreateZip(void *z,
unsigned int len,
DWORD flags);
// 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
调用CreateZip来创建一个压缩文件,在创建压缩文件的时候,根据flags来判断文件存储位置
管道中:CreateZip(hpip_write,0,ZIP_HANDLE);
文件中(根据文件句柄):CreateZip(hfile, 0,ZIP_HANDLE);
文件中(文件路径):CreateZip("c:\\test.zip", 0,ZIP_FILENAME);
内存中:CreateZip(buf, len,ZIP_MEMORY);
页面文件:CreateZip(0, len,ZIP_MEMORY);
- ZipAdd()--在压缩文件中添加文件
ZRESULT ZipAdd(HZIP hz,
const TCHAR *dstzn,
void *src,
unsigned int len,
DWORD flags);
// 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
每一个需要添加进hz的文件都需要调用ZipAdd();
dstzn存在hz中的文件的名字。被加到hz中的文件可以从管道中来:ZipAdd(hz,"file.dat", hpipe_read,0,ZIP_HANDLE);
从文件(文件句柄)中来: ZipAdd(hz,"file.dat", hfile,0,ZIP_HANDLE);
从文件(路径)中来:ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat",0,ZIP_FILENAME);
从内存中来: ZipAdd(hz,"subdir\\file.dat", buf,len,ZIP_MEMORY);
或者添加文件夹:ZipAdd(hz,"subdir", 0,0,ZIP_FOLDER);
- 打开zip文件--OpenZip()
HZIP OpenZip(void *z, unsigned int len, DWORD flags);
// 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
// OpenZip - opens a zip file and returns a handle with which you can
// subsequently examine its contents. You can open a zip file from:
// from a pipe: OpenZip(hpipe_read,0, ZIP_HANDLE);
// from a file (by handle): OpenZip(hfile,0, ZIP_HANDLE);
// from a file (by name): OpenZip("c:\\test.zip",0, ZIP_FILENAME);
// from a memory block: Ope