void *minizip_unpack_to_memory(void *ptr, int len, int *unpack_len) { char *data = (char*)ptr; if (data[0]!='P'||data[1]!='K') goto unpack_to_mem_fail; zlib_filefunc_def pzlib_filefunc_def; fill_memory_filefunc(&pzlib_filefunc_def); char memname[128]; sprintf(memname,"%lx+%lx", (unsigned long)ptr, (unsigned long)len); unzFile hFile = unzOpen2(memname,&pzlib_filefunc_def); if (unzGoToFirstFile(hFile) != UNZ_OK) goto unpack_to_mem_fail; if (unzOpenCurrentFile(hFile) != UNZ_OK) goto unpack_to_mem_fail; unsigned char *tot = 0; unsigned int n_tot=0, n_read=0; unsigned char buf[1024]; while ((n_read = unzReadCurrentFile(hFile, buf, 1024)) > 0) { if (tot==0) tot = malloc(n_read); else tot = realloc(tot,n_tot+n_read); if (tot==0) goto unpack_to_mem_fail; memcpy(tot+n_tot,buf,n_read); n_tot+=n_read; } unzClose(hFile); *unpack_len = n_tot; return tot; unpack_to_mem_fail: return 0; }
void Zips::add(vector<char> &data) { zlib_filefunc_def ff; fill_memory_filefunc(&ff); zmemdata dta; dta.data = (char*)&data.front(); dta.size = data.size(); unzFile zp = unzOpen2((const char*)&dta, &ff); OX_ASSERT(zp); if (!zp) return; _zps.push_back(zpitem()); zpitem& item = _zps.back(); item.handle = zp; swap(item.data, data); read(zp); }
void Zips::add(const unsigned char* data, unsigned int size) { zlib_filefunc_def ff; fill_memory_filefunc(&ff); zmemdata dta; dta.data = (char*)data; dta.size = size; unzFile zp = unzOpen2((const char*)&dta, &ff); OX_ASSERT(zp); if (!zp) return; zpitem item; item.handle = zp; _zps.push_back(item); read(zp); }
extern "C" HZIP OpenArchiveW(WCHAR *lpstrZipFile,char *lpPassword,int dwPasswordLen,bool bZipInMem,int dwFileSize) { void *r=NULL; if (lpstrZipFile) { ZIPDECOMPRESSION *p=(ZIPDECOMPRESSION*)_alloc(sizeof(ZIPDECOMPRESSION)); p->bHandleType=HT_DECOMPRESSOR; if (p) { zlib_filefunc64_def ffunc; p->bInMem=bZipInMem; if (!bZipInMem) fill_fopen64_filefunc(&ffunc); else { fill_memory_filefunc(&ffunc); char filePath[30]; wsprintfA(filePath,"%x+%x",(int)lpstrZipFile,dwFileSize); lpstrZipFile=(WCHAR*)filePath; } if (((lpPassword) && (dwPasswordLen > 0))) { p->bEncrypted=true; memcpy(p->szPassword,lpPassword,dwPasswordLen); } p->hZip=unzOpen2_64(lpstrZipFile,&ffunc); if (p->hZip) r=p; } if (!r) MemFree(p); } else ArchSetLastError(ARCH_INVALID_PARAMETER); return r; }
// Static. MgByteReader * WriteKmz(const std::string& kml) { zlib_filefunc_def fdef; fill_memory_filefunc(&fdef); char buff[256]; int outsize = kml.length() + 256;; char* outbuff = (char*)malloc(outsize); ourmemory_t *mem = (ourmemory_t *)malloc(sizeof(*mem)); mem->base = outbuff; mem->size = outsize; mem->limit = 0; mem->cur_offset = 0; sprintf(buff,"%p",mem); zipFile zipfile_ = zipOpen2(buff, 0,0,&fdef); if (!zipfile_) { return false; } zipOpenNewFileInZip(zipfile_, "doc.kml", 0, 0, 0, 0, 0, 0, Z_DEFLATED, Z_DEFAULT_COMPRESSION); //zipWriteInFileInZip(zipfile_, (void* const)kml.data(), zipWriteInFileInZip(zipfile_, static_cast<const void*>(kml.data()), static_cast<unsigned int>(kml.size())); zipCloseFileInZip(zipfile_); zipClose(zipfile_, 0); MgByteReader *breader = new MgByteReader((unsigned char*)outbuff,mem->limit,MgMimeType::Kmz); free(outbuff); free(mem); return breader; }
enum ApkResult apk_uncompress_internal(AndroidApk *apk, char **buffer, size_t *size) { zlib_filefunc_def filefunc; fill_memory_filefunc(&filefunc); char *old_buffer = *buffer; char path[1024]; sprintf(path, "%x+%x", *buffer, *size); /* Decompress a single file in a .zip from memory */ unz_file_info info; unzFile *unz = unzOpen2(path, &filefunc); unzGoToFirstFile(unz); unzGetCurrentFileInfo(unz, &info, NULL, 0, NULL, 0, NULL, 0); *size = info.uncompressed_size; *buffer = malloc(*size); unzOpenCurrentFile(unz); unzReadCurrentFile(unz, *buffer, *size); unzCloseCurrentFile(unz); unzClose(unz); free(old_buffer); return APK_OK; }
int minizip_unpack_to_disk(char *path, void *ptr, int len) { // check the magic char *data = (char*)ptr; if (data[0]!='P'||data[1]!='K') goto unpack_to_disk_fail; // setup the memory image zlib_filefunc_def pzlib_filefunc_def; fill_memory_filefunc(&pzlib_filefunc_def); char memname[128]; sprintf(memname,"%lx+%lx", (unsigned long)ptr, (unsigned long)len); unzFile hFile = unzOpen2(memname,&pzlib_filefunc_def); // get info unz_global_info globalInfo = {0}; if (!unzGetGlobalInfo(hFile, &globalInfo )==UNZ_OK ) goto unpack_to_disk_fail; int entries = globalInfo.number_entry; // do the unpacking if (unzGoToFirstFile(hFile) != UNZ_OK) goto unpack_to_disk_fail; int i=0; while (1) { char fname[1024]; char filename_inzip[256]={0}; unz_file_info64 file_info; if (unzGetCurrentFileInfo64(hFile,&file_info, filename_inzip,sizeof(filename_inzip), NULL,0,NULL,0)!=UNZ_OK) goto unpack_to_disk_fail; sprintf(fname,"%s%c%s", path, #ifdef WIN32 '\\' #else '/' #endif , filename_inzip); int fname_len = strlen(filename_inzip); if (filename_inzip[fname_len-1]=='/'||filename_inzip[fname_len-1]=='\\') { // it's a directory _minizip_mkdir(fname); } else { // it's a file if (unzOpenCurrentFile(hFile) != UNZ_OK) goto unpack_to_disk_fail; FILE *fd =0; int n_tot=0, n_read=0; unsigned char buf[32768]; while ((n_read = unzReadCurrentFile(hFile, buf, 32768)) > 0) { if (!n_tot&&!fd&&!_minizip_file_exists(fname)) { DMSG("fopen %s", fname); fd = fopen(fname,"wb"); } if (fd) { DMSG("fwrite %i", n_read); fwrite(buf,n_read,1,fd); } n_tot+=n_read; } if (fd) { DMSG("fclose"); fclose(fd); } DMSG("%s [%i]",fname,n_tot); unzCloseCurrentFile(hFile); } if (++i==entries) break; if (unzGoToNextFile(hFile)!=UNZ_OK) goto unpack_to_disk_fail; } // cleanup unzClose(hFile); return 0; unpack_to_disk_fail: return 1; }