ZIP_API ZRESULT ZipAddStreamLength(HZIP zip_handle, const TCHAR* destination_file_name, HANDLE handle, unsigned int length){return ZipAddHandle(zip_handle, destination_file_name, handle, length);}
void main() { CreateFiles(); HZIP hz; DWORD writ; // EXAMPLE 1 - create a zipfile from existing files hz = CreateZip(_T("\\simple1.zip"),0); ZipAdd(hz,_T("znsimple.bmp"), _T("\\simple.bmp")); ZipAdd(hz,_T("znsimple.txt"), _T("\\simple.txt")); CloseZip(hz); _tprintf(_T("Created '\\simple1.zip'\n")); // EXAMPLE 2 - unzip it with the names suggested in the zip hz = OpenZip(_T("\\simple1.zip"),0); SetUnzipBaseDir(hz,_T("\\")); ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index; for (int zi=0; zi<numitems; zi++) { GetZipItem(hz,zi,&ze); UnzipItem(hz,zi,ze.name); } CloseZip(hz); _tprintf(_T("Unzipped 'znsimple.bmp' and 'znsimple.txt' from 'simple1.zip'\n")); // EXAMPLE 3 - create an auto-allocated pagefile-based zip file from various sources // the second argument says how much address space to reserve for it. We can // afford to be generous: no address-space is allocated unless it's actually needed. hz = CreateZip(0,100000,"password"); // adding a conventional file... ZipAdd(hz,_T("znsimple.txt"), _T("\\simple.txt")); // adding something from memory... char buf[1000]; for (int zj=0; zj<1000; zj++) buf[zj]=(char)(zj&0x7F); ZipAdd(hz,_T("simple.dat"), buf,1000); // adding something from a pipe... #ifndef UNDER_CE HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,NULL,0); DWORD WINAPI Ex3ThreadFunc(void *dat); HANDLE hthread = CreateThread(0,0,Ex3ThreadFunc,(void*)hwrite,0,0); ZipAddHandle(hz,_T("simple3.dat"), hread,1000); // the '1000' is optional, but it makes for nicer zip files if sizes are known in advance. WaitForSingleObject(hthread,INFINITE); CloseHandle(hthread); CloseHandle(hread); // the thread will close hwrite #endif // // and now that the zip is created in pagefile, let's do something with it: void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen); HANDLE hfz = CreateFile(_T("\\simple3 - pwd is 'password'.zip"),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); WriteFile(hfz,zbuf,zlen,&writ,NULL); CloseHandle(hfz); CloseZip(hz); _tprintf(_T("Created 'simple3.zip' via pagefile\n")); // EXAMPLE 4 - unzip directly from resource into a file // resource RT_RCDATA/#1 happens to be a zip file... HINSTANCE hInstance=GetModuleHandle(0); 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); SetUnzipBaseDir(hz,_T("\\")); int i; FindZipItem(hz,_T("simple.jpg"),true,&i,&ze); // - unzip to a file - UnzipItem(hz,i,ze.name); // - unzip to a membuffer - char *ibuf = new char[ze.unc_size]; UnzipItem(hz,i, ibuf, ze.unc_size); delete[] ibuf; // - unzip to a fixed membuff, bit by bit - char fbuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0; while (zr==ZR_MORE) { zr = UnzipItem(hz,i, fbuf,1024); unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize; // now do something with this chunk which we just unzipped... totsize+=bufsize; } // - finished - CloseZip(hz); // note: no need to free resources obtained through Find/Load/LockResource _tprintf(_T("Unzipped 'simple.jpg' from resource zipfile\n")); DeleteFile(_T("\\simple.txt")); DeleteFile(_T("\\simple.bmp")); _tprintf(_T("Deleted 'simple.txt' and 'simple.bmp'\n")); }
ZIP_API ZRESULT ZipAddStream(HZIP zip_handle, const TCHAR* destination_file_name, HANDLE handle){return ZipAddHandle(zip_handle, destination_file_name, handle);}