常用函数:
HZIP OpenZip(const TCHAR* fn, const char* password)
ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY* ze);
ZRESULT FindZipItem(HZIP hz, const TCHAR* name, bool ic, int* index, ZIPENTRY* ze);
ZRESULT UnzipItem(HZIP hz, int index, const TCHAR* fn);
ZRESULT UnzipItem(HZIP hz, int index, void* z, UINT len);
ZRESULT UnzipItemHandle(HZIP hz, int index, HANDLE h);
ZRESULT SetUnzipBaseDir(HZIP hz, const TCHAR* dir);
ZRESULT CloseZip(HZIP hz);
UINT FormatZipMessage(ZRESULT code, TCHAR* buf, UINT len);
示例
SetCurrentDirectory("c:\\docs\\stuff");
HZIP hz = OpenZip("c:\\stuff.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
for (int i=0; i<numitems; i++)
{ GetZipItem(hz,i,&ze);
UnzipItem(hz,i,ze.name);
}
CloseZip(hz);
HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
HANDLE hglob = LoadResource(hInstance,hrsrc);
void*zipbuf=LockResource(hglob);
unsigned int ziplen=SizeofResource(hInstance,hrsrc);
HZIP hz = OpenZip(zipbuf, ziplen, 0);
- unzip to a membuffer -
ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze);
char*ibuf = new char[ze.unc_size];
UnzipItem(hz,i, ibuf, ze.unc_size);
delete[] ibuf;
- unzip to a fixed membuff -
ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze);
char ibuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
while (zr==ZR_MORE)
{ zr = UnzipItem(hz,i, ibuf,1024);
unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
totsize+=bufsize;
}
- unzip to a pipe -
HANDLE hwrite; HANDLE hthread=CreateWavReaderThread(&hwrite);
int i; ZIPENTRY ze; FindZipItem(hz,"sound.wav",true,&i,&ze);
UnzipItemHandle(hz,i, hwrite);
CloseHandle(hwrite);
WaitForSingleObject(hthread,INFINITE);
CloseHandle(hwrite); CloseHandle(hthread);
- finished -
CloseZip(hz);
// note: no need to free resources obtained through Find/Load/LockResource
SetCurrentDirectory("c:\\docs\\pipedzipstuff");
HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,0,0);
CreateZipWriterThread(hwrite);
HZIP hz = OpenZipHandle(hread,0);
for (int i=0; ; i++)
{ ZIPENTRY ze;
ZRESULT zr=GetZipItem(hz,i,&ze); if (zr!=ZR_OK) break; // no more
UnzipItem(hz,i, ze.name);
}
CloseZip(hz);