解压缩功能的实现--使用XZip与XUnzip

本文档介绍了如何在MFC程序中使用XZip库进行解压缩操作。首先,通过添加XZip到项目并引用头文件来集成库。接着,详细解释了CreateZip、ZipAdd、OpenZip等关键函数的用法,如创建压缩文件、添加文件到压缩文件、打开压缩文件等。还提到了如何通过管道、文件、内存等方式操作压缩文件,并提供了简易界面的实现示例。
摘要由CSDN通过智能技术生成

使用的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值