1 /* 2 minizip.c 3 Version 1.1, February 14h, 2010 4 sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 5 6 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 7 8 Modifications of Unzip for Zip64 9 Copyright (C) 2007-2008 Even Rouault 10 11 Modifications for Zip64 support on both zip and unzip 12 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 13 */ 14 15 16 #ifndef _WIN32 17 #ifndef __USE_FILE_OFFSET64 18 #define __USE_FILE_OFFSET64 19 #endif 20 #ifndef __USE_LARGEFILE64 21 #define __USE_LARGEFILE64 22 #endif 23 #ifndef _LARGEFILE64_SOURCE 24 #define _LARGEFILE64_SOURCE 25 #endif 26 #ifndef _FILE_OFFSET_BIT 27 #define _FILE_OFFSET_BIT 64 28 #endif 29 #endif 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <time.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 38 #ifdef unix 39 # include <unistd.h> 40 # include <utime.h> 41 # include <sys/types.h> 42 # include <sys/stat.h> 43 #else 44 # include <direct.h> 45 # include <io.h> 46 #endif 47 48 #include "zip.h" 49 50 #ifdef _WIN32 51 #define USEWIN32IOAPI 52 #include "iowin32.h" 53 #endif 54 55 56 57 #define WRITEBUFFERSIZE (16384) 58 #define MAXFILENAME (256) 59 60 #ifdef _WIN32 61 uLong filetime(f, tmzip, dt) 62 char *f; /* name of file to get info on */ 63 tm_zip *tmzip; /* return value: access, modific. and creation times */ 64 uLong *dt; /* dostime */ 65 { 66 int ret = 0; 67 { 68 FILETIME ftLocal; 69 HANDLE hFind; 70 WIN32_FIND_DATAA ff32; 71 72 hFind = FindFirstFileA(f,&ff32); 73 if (hFind != INVALID_HANDLE_VALUE) 74 { 75 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); 76 FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); 77 FindClose(hFind); 78 ret = 1; 79 } 80 } 81 return ret; 82 } 83 #else 84 #ifdef unix 85 uLong filetime(f, tmzip, dt) 86 char *f; /* name of file to get info on */ 87 tm_zip *tmzip; /* return value: access, modific. and creation times */ 88 uLong *dt; /* dostime */ 89 { 90 int ret=0; 91 struct stat s; /* results of stat() */ 92 struct tm* filedate; 93 time_t tm_t=0; 94 95 if (strcmp(f,"-")!=0) 96 { 97 char name[MAXFILENAME+1]; 98 int len = strlen(f); 99 if (len > MAXFILENAME) 100 len = MAXFILENAME; 101 102 strncpy(name, f,MAXFILENAME-1); 103 /* strncpy doesnt append the trailing NULL, of the string is too long. */ 104 name[ MAXFILENAME ] = '\0'; 105 106 if (name[len - 1] == '/') 107 name[len - 1] = '\0'; 108 /* not all systems allow stat'ing a file with / appended */ 109 if (stat(name,&s)==0) 110 { 111 tm_t = s.st_mtime; 112 ret = 1; 113 } 114 } 115 filedate = localtime(&tm_t); 116 117 tmzip->tm_sec = filedate->tm_sec; 118 tmzip->tm_min = filedate->tm_min; 119 tmzip->tm_hour = filedate->tm_hour; 120 tmzip->tm_mday = filedate->tm_mday; 121 tmzip->tm_mon = filedate->tm_mon ; 122 tmzip->tm_year = filedate->tm_year; 123 124 return ret; 125 } 126 #else 127 uLong filetime(f, tmzip, dt) 128 char *f; /* name of file to get info on */ 129 tm_zip *tmzip; /* return value: access, modific. and creation times */ 130 uLong *dt; /* dostime */ 131 { 132 return 0; 133 } 134 #endif 135 #endif 136 137 138 139 140 int check_exist_file(filename) 141 const char* filename; 142 { 143 FILE* ftestexist; 144 int ret = 1; 145 ftestexist = fopen64(filename,"rb"); 146 if (ftestexist==NULL) 147 ret = 0; 148 else 149 fclose(ftestexist); 150 return ret; 151 } 152 153 void do_banner() 154 { 155 printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n"); 156 printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n"); 157 } 158 159 void do_help() 160 { 161 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \ 162 " -o Overwrite existing file.zip\n" \ 163 " -a Append to existing file.zip\n" \ 164 " -0 Store only\n" \ 165 " -1 Compress faster\n" \ 166 " -9 Compress better\n\n" \ 167 " -j exclude path. store only the file name.\n\n"); 168 } 169 170 /* calculate the CRC32 of a file, 171 because to encrypt a file, we need known the CRC32 of the file before */ 172 int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc) 173 { 174 unsigned long calculate_crc=0; 175 int err=ZIP_OK; 176 FILE * fin = fopen64(filenameinzip,"rb"); 177 unsigned long size_read = 0; 178 unsigned long total_read = 0; 179 if (fin==NULL) 180 { 181 err = ZIP_ERRNO; 182 } 183 184 if (err == ZIP_OK) 185 do 186 { 187 err = ZIP_OK; 188 size_read = (int)fread(buf,1,size_buf,fin); 189 if (size_read < size_buf) 190 if (feof(fin)==0) 191 { 192 printf("error in reading %s\n",filenameinzip); 193 err = ZIP_ERRNO; 194 } 195 196 if (size_read>0) 197 calculate_crc = crc32(calculate_crc,buf,size_read); 198 total_read += size_read; 199 200 } while ((err == ZIP_OK) && (size_read>0)); 201 202 if (fin) 203 fclose(fin); 204 205 *result_crc=calculate_crc; 206 printf("file %s crc %lx\n", filenameinzip, calculate_crc); 207 return err; 208 } 209 210 int isLargeFile(const char* filename) 211 { 212 int largeFile = 0; 213 ZPOS64_T pos = 0; 214 FILE* pFile = fopen64(filename, "rb"); 215 216 if(pFile != NULL) 217 { 218 int n = fseeko64(pFile, 0, SEEK_END); 219 220 pos = ftello64(pFile); 221 222 printf("File : %s is %lld bytes\n", filename, pos); 223 224 if(pos >= 0xffffffff) 225 largeFile = 1; 226 227 fclose(pFile); 228 } 229 230 return largeFile; 231 } 232 233 int main(argc,argv) 234 int argc; 235 char *argv[]; 236 { 237 int i; 238 int opt_overwrite=0; 239 int opt_compress_level=Z_DEFAULT_COMPRESSION; 240 int opt_exclude_path=0; 241 int zipfilenamearg = 0; 242 char filename_try[MAXFILENAME+16]; 243 int zipok; 244 int err=0; 245 int size_buf=0; 246 void* buf=NULL; 247 const char* password=NULL; 248 249 250 do_banner(); 251 if (argc==1) 252 { 253 do_help(); 254 return 0; 255 } 256 else 257 { 258 for (i=1;i<argc;i++) 259 { 260 if ((*argv[i])=='-') 261 { 262 const char *p=argv[i]+1; 263 264 while ((*p)!='\0') 265 { 266 char c=*(p++);; 267 if ((c=='o') || (c=='O')) 268 opt_overwrite = 1; 269 if ((c=='a') || (c=='A')) 270 opt_overwrite = 2; 271 if ((c>='0') && (c<='9')) 272 opt_compress_level = c-'0'; 273 if ((c=='j') || (c=='J')) 274 opt_exclude_path = 1; 275 276 if (((c=='p') || (c=='P')) && (i+1<argc)) 277 { 278 password=argv[i+1]; 279 i++; 280 } 281 } 282 } 283 else 284 { 285 if (zipfilenamearg == 0) 286 { 287 zipfilenamearg = i ; 288 } 289 } 290 } 291 } 292 293 size_buf = WRITEBUFFERSIZE; 294 buf = (void*)malloc(size_buf); 295 if (buf==NULL) 296 { 297 printf("Error allocating memory\n"); 298 return ZIP_INTERNALERROR; 299 } 300 301 if (zipfilenamearg==0) 302 { 303 zipok=0; 304 } 305 else 306 { 307 int i,len; 308 int dot_found=0; 309 310 zipok = 1 ; 311 strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); 312 /* strncpy doesnt append the trailing NULL, of the string is too long. */ 313 filename_try[ MAXFILENAME ] = '\0'; 314 315 len=(int)strlen(filename_try); 316 for (i=0;i<len;i++) 317 if (filename_try[i]=='.') 318 dot_found=1; 319 320 if (dot_found==0) 321 strcat(filename_try,".zip"); 322 323 if (opt_overwrite==2) 324 { 325 /* if the file don't exist, we not append file */ 326 if (check_exist_file(filename_try)==0) 327 opt_overwrite=1; 328 } 329 else 330 if (opt_overwrite==0) 331 if (check_exist_file(filename_try)!=0) 332 { 333 char rep=0; 334 do 335 { 336 char answer[128]; 337 int ret; 338 printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try); 339 ret = scanf("%1s",answer); 340 if (ret != 1) 341 { 342 exit(EXIT_FAILURE); 343 } 344 rep = answer[0] ; 345 if ((rep>='a') && (rep<='z')) 346 rep -= 0x20; 347 } 348 while ((rep!='Y') && (rep!='N') && (rep!='A')); 349 if (rep=='N') 350 zipok = 0; 351 if (rep=='A') 352 opt_overwrite = 2; 353 } 354 } 355 356 if (zipok==1) 357 { 358 zipFile zf; 359 int errclose; 360 # ifdef USEWIN32IOAPI 361 zlib_filefunc64_def ffunc; 362 fill_win32_filefunc64A(&ffunc); 363 zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); 364 # else 365 zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0); 366 # endif 367 368 if (zf == NULL) 369 { 370 printf("error opening %s\n",filename_try); 371 err= ZIP_ERRNO; 372 } 373 else 374 printf("creating %s\n",filename_try); 375 376 for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) 377 { 378 if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) && 379 ((argv[i][1]=='o') || (argv[i][1]=='O') || 380 (argv[i][1]=='a') || (argv[i][1]=='A') || 381 (argv[i][1]=='p') || (argv[i][1]=='P') || 382 ((argv[i][1]>='0') || (argv[i][1]<='9'))) && 383 (strlen(argv[i]) == 2))) 384 { 385 FILE * fin; 386 int size_read; 387 const char* filenameinzip = argv[i]; 388 const char *savefilenameinzip; 389 zip_fileinfo zi; 390 unsigned long crcFile=0; 391 int zip64 = 0; 392 393 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = 394 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; 395 zi.dosDate = 0; 396 zi.internal_fa = 0; 397 zi.external_fa = 0; 398 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); 399 400 /* 401 err = zipOpenNewFileInZip(zf,filenameinzip,&zi, 402 NULL,0,NULL,0,NULL / * comment * /, 403 (opt_compress_level != 0) ? Z_DEFLATED : 0, 404 opt_compress_level); 405 */ 406 if ((password != NULL) && (err==ZIP_OK)) 407 err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); 408 409 zip64 = isLargeFile(filenameinzip); 410 411 /* The path name saved, should not include a leading slash. */ 412 /*if it did, windows/xp and dynazip couldn't read the zip file. */ 413 savefilenameinzip = filenameinzip; 414 while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' ) 415 { 416 savefilenameinzip++; 417 } 418 419 /*should the zip file contain any path at all?*/ 420 if( opt_exclude_path ) 421 { 422 const char *tmpptr; 423 const char *lastslash = 0; 424 for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++) 425 { 426 if( *tmpptr == '\\' || *tmpptr == '/') 427 { 428 lastslash = tmpptr; 429 } 430 } 431 if( lastslash != NULL ) 432 { 433 savefilenameinzip = lastslash+1; // base filename follows last slash. 434 } 435 } 436 437 /**/ 438 err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi, 439 NULL,0,NULL,0,NULL /* comment*/, 440 (opt_compress_level != 0) ? Z_DEFLATED : 0, 441 opt_compress_level,0, 442 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ 443 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 444 password,crcFile, zip64); 445 446 if (err != ZIP_OK) 447 printf("error in opening %s in zipfile\n",filenameinzip); 448 else 449 { 450 fin = fopen64(filenameinzip,"rb"); 451 if (fin==NULL) 452 { 453 err=ZIP_ERRNO; 454 printf("error in opening %s for reading\n",filenameinzip); 455 } 456 } 457 458 if (err == ZIP_OK) 459 do 460 { 461 err = ZIP_OK; 462 size_read = (int)fread(buf,1,size_buf,fin); 463 if (size_read < size_buf) 464 if (feof(fin)==0) 465 { 466 printf("error in reading %s\n",filenameinzip); 467 err = ZIP_ERRNO; 468 } 469 470 if (size_read>0) 471 { 472 err = zipWriteInFileInZip (zf,buf,size_read); 473 if (err<0) 474 { 475 printf("error in writing %s in the zipfile\n", 476 filenameinzip); 477 } 478 479 } 480 } while ((err == ZIP_OK) && (size_read>0)); 481 482 if (fin) 483 fclose(fin); 484 485 if (err<0) 486 err=ZIP_ERRNO; 487 else 488 { 489 err = zipCloseFileInZip(zf); 490 if (err!=ZIP_OK) 491 printf("error in closing %s in the zipfile\n", 492 filenameinzip); 493 } 494 } 495 } 496 errclose = zipClose(zf,NULL); 497 if (errclose != ZIP_OK) 498 printf("error in closing %s\n",filename_try); 499 } 500 else 501 { 502 do_help(); 503 } 504 505 free(buf); 506 return 0; 507 } 508