文件的压缩与解压XZip,XUnzip

简介: 参考http://www.codeproject.com/KB/cpp/xzipunzip.aspx CreateZip() –创建一个空的 zip 文件 HZIP CreateZip(void *z, unsigned int ...

参考http://www.codeproject.com/KB/cpp/xzipunzip.aspx

CreateZip() –创建一个空的 zip 文件

HZIP CreateZip(void *z, unsigned int len, DWORD flags);

// Parameters: z - 压缩文件名
// len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小; // 其他情况,这个值应当是 0 // flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME // // Returns: HZIP - 非 0 值为成功, 非则返回 0

ZipAdd() 将一个文件添加进压缩文件

// Parameters: hz - 创建或 打开的zip文件句柄
// dstzn - 在zip文件中显示的文件名
// src - 对于(ZIP_FILENAME)的情况,这是将要添加进zip文件的文件名
// len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小;
// 其他情况,这个值应当是 0
// flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME
// // Returns: ZRESULT - 成功则返回 ZR_OK
OpenZip() 打开一个已经存在的压缩文件
// Parameters: z - 压缩文件名
// len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小;
// 其他情况,这个值应当是 0
// flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME
// // Returns: HZIP - 非 0 值为成功, 非则返回 0

GetZipItem() 得到压缩文件里面元素的信息

// Parameters: hz - 创建或 打开的zip文件句柄
// index - zip文件里的元素下标(从0开始)
// ze - 一个指向ZIPENTRY 结构体的指针(ANSI) 或者 ZIPENTRYW (Unicode) // // Returns: ZRESULT - 成功则返回 ZR_OK

FindZipItem() 在压缩文件里查找某元素

// Parameters: hz - 创建或 打开的zip文件句柄
// name - 你要在zip文件中查找的元素名字
// ic - 值为 TRUE 不区分大小写
// index - 返回要查找元素的下标,否则是-1 // ze - 返回指向ZIPENTRY 结构体的指针(ANSI) 或者 ZIPENTRYW (Unicode)
// // Returns: ZRESULT - 成功则返回 ZR_OK

UnzipItem() 解压压缩文件里的某元素

// Parameters: hz - 创建或 打开的zip文件句柄
// index - 要解压元素的下标
// dst - 解压出来文件的名字
// len - 对于压缩内存内容(ZIP_MEMORY) 这个值是压缩内存的大小;
// 其他情况,这个值应当是 0
// flags - 如果是压缩文件,请用这个标志 ZIP_FILENAME
// // Returns: ZRESULT - 成功则返回 ZR_OK

CloseZip() 关闭压缩文件

// Parameters: hz - 要操作的zip文件句柄
// // Returns: ZRESULT - 成功则返回 ZR_OK

 

注意,不能一次压缩整个目录,可以相对路径来压缩目录


void CZipDlg::OnOK()

{

// TODO: Add extra validation here

//********** 压缩文件 ******************************

HZIP hz = CreateZip("test.zip",0,ZIP_FILENAME);

ZipAdd(hz,"NameInZip.txt", "ReadMe.txt",0,ZIP_FILENAME);

ZipAdd(hz,"zip.h", "打包压缩文件zip.h",0,ZIP_FILENAME);

CloseZip(hz);

//********** 压缩目录和文件 **************************

HZIP hz1 = CreateZip("test1.zip",0,ZIP_FILENAME);

//空目录

ZipAdd(hz1,"aa", "Debug",0,ZIP_FOLDER);//ZIP_FOLDER

//有两个文件的目录

ZipAdd(hz1,"res\\NameInZip.txt", "ReadMe.txt",0,ZIP_FILENAME);

ZipAdd(hz1,"res\\zip.h", "打包压缩文件zip.h",0,ZIP_FILENAME);

CloseZip(hz1);

// CDialog::OnOK();

}



不使用任何lib和dll,压缩/解压缩文件(zip格式)   


发布者:   soarlove   ——> 查看soarlove在VCCode发布的所有文章     文章类型:翻译     
发布日期:2003.05.18         
升级次数:0     
今日浏览:2     
总浏览:502     

-------------------------------------------------------------------------------- 
  
评价等级:               
  代码下载     
2位用户为此文章评分,平均分为5.0   


作者: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的限制。 
目录
相关文章
|
前端开发 算法 API
直接在前端做 zip 压缩/解压
前段时间研究前端是如何解析 excel 表格的时候了解到 jszip 这个库,可以直接在前端对 zip 包进行压缩和解压缩,今天稍微水一篇。
|
4月前
|
算法 编译器 API
C++ MiniZip实现目录压缩与解压
Zlib是一个开源的数据压缩库,提供了一种通用的数据压缩和解压缩算法。它最初由`Jean-Loup Gailly`和`Mark Adler`开发,旨在成为一个高效、轻量级的压缩库,其被广泛应用于许多领域,包括网络通信、文件压缩、数据库系统等。其压缩算法是基于`DEFLATE`算法,这是一种无损数据压缩算法,通常能够提供相当高的压缩比。在Zlib项目中的`contrib`目录下有一个`minizip`子项目,minizip实际上不是`zlib`库的一部分,而是一个独立的开源库,用于处理ZIP压缩文件格式。它提供了对ZIP文件的创建和解压的简单接口。minizip在很多情况下与`zlib`一起使用
116 0
C++ MiniZip实现目录压缩与解压
|
8月前
|
Linux
Linux常用的压缩与解压
1、gzip压缩解压 因为都是系统自带的我们不需要安装; ls 看一下目录当前的文件,创建一个 touch 123.txt 文件; 原文件消失,压缩解压 gzip 进行压缩,(“ gzip 123.txt ”),这样就压缩完毕了; gzip 进行解压,(“ gzip -d 123.txt.gz”),这样就解压完毕了; 原文件保留,gzip -k 123.txt、gzip -dk 123.txt.gz ; 2、bzip2压缩解压 因为都是系统自带的我们不需要安装; 原文件消失,压缩解压 bzip2进行压缩,(“ bzip2 123.txt ”),这样就压缩完毕了; bzip2 进行解压,(“
55 0
|
Linux 数据安全/隐私保护 Windows