# zip-unzip **Repository Path**: wjr22/zip-unzip ## Basic Information - **Project Name**: zip-unzip - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-04-29 - **Last Updated**: 2023-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # zip-unzip 基于C语言 跨平台zip/unzip ## zip (1) 传统用法,从现有文件创建zip文件 HZIP hz = CreateZip("c:\\simple1.zip",0); ZipAdd(hz,"znsimple.bmp", "c:\\simple.bmp"); ZipAdd(hz,"znsimple.txt", "c:\\simple.txt"); CloseZip(hz); (2) 内存使用,从各种来源创建一个自动分配的基于内存的zip文件 HZIP hz = CreateZip(0,100000, 0); // adding a conventional file... ZipAdd(hz,"src1.txt", "c:\\src1.txt"); // adding something from memory... char buf[1000]; for (int i=0; i<1000; i++) buf[i]=(char)(i&0x7F); ZipAdd(hz,"file.dat", buf,1000); // adding something from a pipe... HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,NULL,0); HANDLE hthread = CreateThread(0,0,ThreadFunc,(void*)hwrite,0,0); ZipAdd(hz,"unz3.dat", hread,1000); // the '1000' is optional. WaitForSingleObject(hthread,INFINITE); CloseHandle(hthread); CloseHandle(hread); // and now that the zip is created, let's do something with it: void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen); HANDLE hfz = CreateFile("test2.zip",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); LDWORD writ; WriteFile(hfz,zbuf,zlen,&writ,NULL); CloseHandle(hfz); CloseZip(hz); (3) 句柄用于文件句柄和管道 HANDLE hzread,hzwrite; CreatePipe(&hzread,&hzwrite,0,0); HANDLE hthread = CreateThread(0,0,ZipReceiverThread,(void*)hzread,0,0); HZIP hz = CreateZipHandle(hzwrite,0); // ... add to it CloseZip(hz); CloseHandle(hzwrite); WaitForSingleObject(hthread,INFINITE); CloseHandle(hthread); ## unzip (1) 传统方法 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