转载来自:https://www.cnblogs.com/linuxAndMcu/p/14508503.html
ZIP 主要是用于简单的压缩和解压,引入比较方便,而且极其易使用,方便用户操作,下载下来就四个文件:zip.h、zip.cpp、unzip.h、unzip.cpp。
下载地址:http://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win
使用的时候直接 include 就行了。下面是一些示例:
/*For unzipping, add "unzip.cpp" to your project. Then, for example*/ 添加压缩的封装函数: void AddFileToZip(zipFile zf, const char* fileNameInZip, const char* srcFile) 使用: zipFile zf; 参考: C++ ZIP压缩文件,使用zlib库和codeproject上的zip_utils C++ zip压缩库使用
#include "unzip.h"
//
HZIP hz = OpenZip("c:\\stuff.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
for (int i=0; i
{ GetZipItem(hz,i,&ze);
UnzipItem(hz,i,ze.name);
}
CloseZip(hz);
/*For zipping, add "zip.cpp" to your project. (You can add just one of
zip/unzip, or both; they function independently and also co-exist.)*/
#include "zip.h"
//
HZIP hz = CreateZip("c:\\simple1.zip",0);
ZipAdd(hz,"znsimple.bmp", "c:\\simple.bmp");
ZipAdd(hz,"znsimple.txt", "c:\\simple.txt");
CloseZip(hz);
{
FILE* srcfp = NULL;
// 初始化写入zip的文件信息
zip_fileinfo zi;
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
zi.dosDate = 0;
zi.internal_fa = 0;
zi.external_fa = 0;
// 如果srcFile为空,加入空目录
char new_file_name[WRITEBUFFERSIZE];
memset(new_file_name, 0, sizeof(new_file_name));
strcat(new_file_name, fileNameInZip);
if (srcFile == NULL)
{
strcat(new_file_name, "/");
}
// 在zip文件中创建新文件
zipOpenNewFileInZip(zf, new_file_name, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
if (srcFile != NULL)
{
// 打开源文件
srcfp = fopen(srcFile, "rb");
if (srcfp == NULL)
{
zipCloseFileInZip(zf); //关闭zip文件
return;
}
// 读入源文件并写入zip文件
int size_buf = 0;
void* buf=NULL;
size_buf = WRITEBUFFERSIZE;
buf = (void*)malloc(size_buf);
int numBytes = 0;
while( !feof(srcfp) )
{
numBytes = fread(buf, 1, size_buf, srcfp);
zipWriteInFileInZip(zf, buf, numBytes);
if( ferror(srcfp) )
break;
}
// 关闭源文件
fclose(srcfp);
}
// 关闭zip文件
zipCloseFileInZip(zf);
}
AddFileToZip(zf,"zip里的名字","需要压缩的源文件");
zipClose(zf,0);三、扩展:流行的一些C++压缩&解压库