最近刚好用到,个人觉得还是比较好用的:
以下来自网络示例:
Example 1 - create a Zip file from existing files
// We place the file "simple.bmp" inside, but inside
// the zipfile it will actually be called "znsimple.bmp".
// Similarly the textfile.
HZIP hz = CreateZip("simple1.zip",0);
ZipAdd(hz,"znsimple.bmp", "simple.bmp");
ZipAdd(hz,"znsimple.txt", "simple.txt");
CloseZip(hz);
Example 2 - unzip a Zip file using the names it has inside it
HZIP hz = OpenZip("\\simple1.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
// -1 gives overall information about the zipfile
for (int zi=0; zi<numitems; zi++)
{ ZIPENTRY ze; GetZipItem(hz,zi,&ze); // fetch individual details
UnzipItem(hz, zi, ze.name); // e.g. the item's name.
}
CloseZip(hz);
Example 3- unzip from resource directly into memory
This technique is useful for small games, where you want to keep all resources bundled up inside the executable, but restricting the size.
Suppose we used a .rc with 1 RCDATA “file.zip” to embed the Zip file as a resource.
HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
HANDLE hglob = LoadResource(hInstance,hrsrc);
void *zipbuf = LockResource(hglob);
unsigned int ziplen = SizeofResource(hInstance,hrsrc);
hz = OpenZip(zipbuf, ziplen, 0);
ZIPENTRY ze; int i; FindZipItem(hz,"sample.jpg",true,&i,&ze);
// that lets us search for an item by filename.
// Now we unzip it to a membuffer.
char *ibuf = new char[ze.unc_size];
UnzipItem(hz,i, ibuf, ze.unc_size);
...
delete[] ibuf;
CloseZip(hz);
// note: no need to free resources obtained through Find/Load/LockResource
Example 4 - unzip chunk by chunk to a membuffer
Normally when you call UnzipItem(…), it gives the return-code ZR_OK. But if you gave it too small a buffer so that it couldn’t fit it all in, then it returns ZR_MORE.
char buf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
while (zr==ZR_MORE)
{ zr = UnzipItem(hz,i, buf,1024);
unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
... maybe write the buffer to a disk file here
totsize+=bufsize;
}
下载地址:Zip Utils
本文提供了四个关于Zip文件操作的实际代码示例,包括创建、解压、从资源直接读取到内存以及分块解压到内存缓冲区等常见应用场景。
8910

被折叠的 条评论
为什么被折叠?



