static cell_t Zip_AddFile(IPluginContext *pCtx, const cell_t *params) { Handle_t handle = static_cast<Handle_t>(params[1]); HandleError err; HandleSecurity sec; cell_t ret = true; sec.pOwner = NULL; sec.pIdentity = myself->GetIdentity(); zipFile zf; err = handlesys->ReadHandle(handle, g_ZipFileType, &sec, (void **)&zf); if (HandleError_None != err) { return pCtx->ThrowNativeError("Invalid Zip file handle %x (error %d)", handle, err); } char path[PLATFORM_MAX_PATH]; char *filename; pCtx->LocalToString(params[2], &filename); g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", filename); zip_fileinfo zi; memset(&zi, 0, sizeof(zi)); int zipErr = zipOpenNewFileInZip64(zf, filename, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION, isLargeFile(path)); if (ZIP_OK != zipErr) { g_pSM->LogError(myself, "Could not open new file %s in zip (%d)", filename, err); return false; } FILE *fp = fopen64(path, "rb"); if (NULL == fp) { g_pSM->LogError(myself, "fopen64(%s) failed", path); ret = false; } if (ret) { char buf[4096]; size_t bytesRead = 0; do { zipErr = ZIP_OK; bytesRead = fread(buf, 1, sizeof(buf), fp); if (bytesRead < sizeof(buf) && feof(fp) != 0) { zipErr = ZIP_ERRNO; } if (bytesRead > 0) { zipErr = zipWriteInFileInZip(zf, buf, bytesRead); } if (ZIP_ERRNO == zipErr && EAGAIN == errno) zipErr = ZIP_OK; } while (ZIP_OK == zipErr && bytesRead > 0); if (ZIP_OK != zipErr) { if (ZIP_ERRNO == zipErr) g_pSM->LogError(myself, "Failed to write to zip archive (%s)", strerror(errno)); else g_pSM->LogError(myself, "Failed to write to zip archive (%d)", zipErr); ret = false; } } if (fp) fclose(fp); zipCloseFileInZip(zf); return ret; }
int minizip_zip(const char * lpszzipfilename, const char * lpszfilename, const char * lpszpassword, int nmode, int nLevel) { int nret = -1; int nstatus = 0; // int opt_compress_level = Z_DEFAULT_COMPRESSION; if (nLevel != Z_DEFAULT_COMPRESSION && nLevel != Z_BEST_COMPRESSION && nLevel != Z_BEST_SPEED && nLevel != Z_NO_COMPRESSION) { return -10086; } int nzipmode = APPEND_STATUS_CREATE; if (nmode ==0) { nzipmode = APPEND_STATUS_CREATE; } else { nzipmode = APPEND_STATUS_ADDINZIP; } // Create archive zipfilename zipFile zf = zipOpen64(lpszzipfilename, nzipmode); if (zf == NULL) { nstatus = ERROR_CREATE_ZIP; } int size_buf = WRITE_BUFFER_SIZE; Bytef* buf = (Bytef*) malloc(size_buf); // Get information about the file on disk so we can store it in zip zip_fileinfo zi; memset(&zi,0x00,sizeof(zip_fileinfo)); getFileTime(lpszfilename, &zi.tmz_date, &zi.dosDate); unsigned long crcFile = 0; if (nstatus == ZIP_OK) { nstatus = getCRC32(lpszfilename, buf, size_buf, &crcFile); } int zip64 = isLargeFile(lpszfilename); // Construct the filename that our file will be stored in the zip as. const char *savefilenameinzip = lpszfilename; { const char *tmpptr = NULL; const char *lastslash = 0; for (tmpptr = savefilenameinzip; *tmpptr; tmpptr++) { if (*tmpptr == '\\' || *tmpptr == '/') { lastslash = tmpptr; } } if (lastslash != NULL) { savefilenameinzip = lastslash + 1; } } if ((lpszpassword) && (lpszpassword[0] ==0)) { lpszpassword = NULL; } // Create zip file nstatus = zipOpenNewFileInZip3_64(zf, savefilenameinzip, &zi, NULL, 0, NULL, 0, NULL /* comment*/, Z_DEFLATED, nLevel, 0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, lpszpassword, crcFile, zip64); // Add file to zip FILE *fin = NULL; if (nstatus == ZIP_OK) { fin = fopen64(lpszfilename, "rb"); if (fin == NULL) { nstatus = ERROR_FILE_NOT_FOUND; } } int size_read = 0; if (nstatus == ZIP_OK) { // Read contents of file and write it to zip do { size_read = (int) fread(buf, 1, size_buf, fin); if ((size_read < size_buf) && (feof(fin) == 0)) { nstatus = ERROR_WHILE_READ; } if (size_read > 0) { nstatus = zipWriteInFileInZip(zf, buf, size_read); } } while ((nstatus == ZIP_OK) && (size_read > 0)); } if (fin) { fclose(fin); } if (nstatus >= 0) { nstatus = zipCloseFileInZip(zf); } zipClose(zf, NULL); // Release memory free(buf); if (nstatus == UNZ_OK) { nret = 0; } return nret; }
int main( int argc, char *argv[]) { int i; int opt_overwrite=0; int opt_compress_level=Z_DEFAULT_COMPRESSION; int opt_exclude_path=0; int zipfilenamearg = 0; char filename_try[MAXFILENAME+16]; int zipok; int err=0; int size_buf=0; void* buf=NULL; const char* password=NULL; do_banner(); if (argc==1) { do_help(); return 0; } else { for (i=1;i<argc;i++) { if ((*argv[i])=='-') { const char *p=argv[i]+1; while ((*p)!='\0') { char c=*(p++);; if ((c=='o') || (c=='O')) opt_overwrite = 1; if ((c=='a') || (c=='A')) opt_overwrite = 2; if ((c>='0') && (c<='9')) opt_compress_level = c-'0'; if ((c=='j') || (c=='J')) opt_exclude_path = 1; if (((c=='p') || (c=='P')) && (i+1<argc)) { password=argv[i+1]; i++; } } } else { if (zipfilenamearg == 0) { zipfilenamearg = i ; } } } } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { printf("Error allocating memory\n"); return ZIP_INTERNALERROR; } if (zipfilenamearg==0) { zipok=0; } else { int i,len; int dot_found=0; zipok = 1 ; strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); /* strncpy doesnt append the trailing NULL, of the string is too long. */ filename_try[ MAXFILENAME ] = '\0'; len=(int)strlen(filename_try); for (i=0;i<len;i++) if (filename_try[i]=='.') dot_found=1; if (dot_found==0) strcat(filename_try,".zip"); if (opt_overwrite==2) { /* if the file don't exist, we not append file */ if (check_exist_file(filename_try)==0) opt_overwrite=1; } else if (opt_overwrite==0) if (check_exist_file(filename_try)!=0) { char rep=0; do { char answer[128]; int ret; printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try); ret = scanf("%1s",answer); if (ret != 1) { exit(EXIT_FAILURE); } rep = answer[0] ; if ((rep>='a') && (rep<='z')) rep -= 0x20; } while ((rep!='Y') && (rep!='N') && (rep!='A')); if (rep=='N') zipok = 0; if (rep=='A') opt_overwrite = 2; } } if (zipok==1) { zipFile zf; int errclose; # ifdef USEWIN32IOAPI zlib_filefunc64_def ffunc; fill_win32_filefunc64A(&ffunc); zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); # else zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0); # endif if (zf == NULL) { printf("error opening %s\n",filename_try); err= ZIP_ERRNO; } else printf("creating %s\n",filename_try); for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) { if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) && ((argv[i][1]=='o') || (argv[i][1]=='O') || (argv[i][1]=='a') || (argv[i][1]=='A') || (argv[i][1]=='p') || (argv[i][1]=='P') || ((argv[i][1]>='0') || (argv[i][1]<='9'))) && (strlen(argv[i]) == 2))) { FILE * fin; int size_read; const char* filenameinzip = argv[i]; const char *savefilenameinzip; zip_fileinfo zi; unsigned long crcFile=0; int zip64 = 0; zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; zi.dosDate = 0; zi.internal_fa = 0; zi.external_fa = 0; filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); /* err = zipOpenNewFileInZip(zf,filenameinzip,&zi, NULL,0,NULL,0,NULL / * comment * /, (opt_compress_level != 0) ? Z_DEFLATED : 0, opt_compress_level); */ if ((password != NULL) && (err==ZIP_OK)) err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); zip64 = isLargeFile(filenameinzip); /* The path name saved, should not include a leading slash. */ /*if it did, windows/xp and dynazip couldn't read the zip file. */ savefilenameinzip = filenameinzip; while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' ) { savefilenameinzip++; } /*should the zip file contain any path at all?*/ if( opt_exclude_path ) { const char *tmpptr; const char *lastslash = 0; for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++) { if( *tmpptr == '\\' || *tmpptr == '/') { lastslash = tmpptr; } } if( lastslash != NULL ) { savefilenameinzip = lastslash+1; // base filename follows last slash. } } /**/ err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi, NULL,0,NULL,0,NULL /* comment*/, (opt_compress_level != 0) ? Z_DEFLATED : 0, opt_compress_level,0, /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password,crcFile, zip64); if (err != ZIP_OK) printf("error in opening %s in zipfile\n",filenameinzip); else { fin = FOPEN_FUNC(filenameinzip,"rb"); if (fin==NULL) { err=ZIP_ERRNO; printf("error in opening %s for reading\n",filenameinzip); } } if (err == ZIP_OK) do { err = ZIP_OK; size_read = (int)fread(buf,1,size_buf,fin); if (size_read < size_buf) if (feof(fin)==0) { printf("error in reading %s\n",filenameinzip); err = ZIP_ERRNO; } if (size_read>0) { err = zipWriteInFileInZip (zf,buf,size_read); if (err<0) { printf("error in writing %s in the zipfile\n", filenameinzip); } } } while ((err == ZIP_OK) && (size_read>0)); if (fin) fclose(fin); if (err<0) err=ZIP_ERRNO; else { err = zipCloseFileInZip(zf); if (err!=ZIP_OK) printf("error in closing %s in the zipfile\n", filenameinzip); } } } errclose = zipClose(zf,NULL); if (errclose != ZIP_OK) printf("error in closing %s\n",filename_try); } else { do_help(); } free(buf); return 0; }
int BLZip::Add( std::string contentPath, std::string zipPath, int flags ) { int compressLevel = 0; int zip64 = 0; char * password = NULL; zip_fileinfo zi; FILE * fin = NULL; int size_read; int size_buf=0; void* buf=NULL; int err; unsigned long crcFile=0; if( !this->zf ) return ZIP_PARAMERROR; size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { printf("Error allocating memory\n"); return ZIP_INTERNALERROR; } if( !zipPath.compare( "" ) ) { zipPath.assign( contentPath ); } if( flags & ZipFlag_Faster ) compressLevel = 1; if( flags & ZipFlag_Better ) compressLevel = 9; err = getFileCrc( contentPath.c_str(), buf, size_buf, &crcFile ); zip64 = isLargeFile( contentPath.c_str() ); err = zipOpenNewFileInZip3_64(this->zf,zipPath.c_str(),&zi, NULL,0,NULL,0,NULL /* comment*/, (compressLevel != 0) ? Z_DEFLATED : 0, compressLevel,0, /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password,crcFile, zip64); if (err != ZIP_OK) printf("error in opening %s in zipfile\n", contentPath.c_str() ); else { fin = FOPEN_FUNC( (char *)contentPath.c_str(), "rb" ); if (fin==NULL) { err=ZIP_ERRNO; printf("error in opening %s for reading\n", contentPath.c_str()); } } if (err == ZIP_OK) do { err = ZIP_OK; size_read = (int)fread(buf,1,size_buf,fin); if (size_read < size_buf) if (feof(fin)==0) { printf("error in reading %s\n", contentPath.c_str() ); err = ZIP_ERRNO; } if (size_read>0) { err = zipWriteInFileInZip (this->zf,buf,size_read); if (err<0) { printf("error in writing %s in the zipfile\n", contentPath.c_str() ); } } } while ((err == ZIP_OK) && (size_read>0)); if (fin) fclose(fin); /* and close the file */ if( err == ZIP_OK ) { err = zipCloseFileInZip(this->zf); } free( buf ); return ZIP_OK; }