OLD | NEW |
(Empty) | |
| 1 /* |
| 2 miniunz.c |
| 3 Version 1.1, February 14h, 2010 |
| 4 sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizi
p.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 #ifndef _WIN32 |
| 16 #ifndef __USE_FILE_OFFSET64 |
| 17 #define __USE_FILE_OFFSET64 |
| 18 #endif |
| 19 #ifndef __USE_LARGEFILE64 |
| 20 #define __USE_LARGEFILE64 |
| 21 #endif |
| 22 #ifndef _LARGEFILE64_SOURCE |
| 23 #define _LARGEFILE64_SOURCE |
| 24 #endif |
| 25 #ifndef _FILE_OFFSET_BIT |
| 26 #define _FILE_OFFSET_BIT 64 |
| 27 #endif |
| 28 #endif |
| 29 |
| 30 #include <stdio.h> |
| 31 #include <stdlib.h> |
| 32 #include <string.h> |
| 33 #include <time.h> |
| 34 #include <errno.h> |
| 35 #include <fcntl.h> |
| 36 |
| 37 #ifdef unix |
| 38 # include <unistd.h> |
| 39 # include <utime.h> |
| 40 #else |
| 41 # include <direct.h> |
| 42 # include <io.h> |
| 43 #endif |
| 44 |
| 45 #include "unzip.h" |
| 46 |
| 47 #define CASESENSITIVITY (0) |
| 48 #define WRITEBUFFERSIZE (8192) |
| 49 #define MAXFILENAME (256) |
| 50 |
| 51 #ifdef _WIN32 |
| 52 #define USEWIN32IOAPI |
| 53 #include "iowin32.h" |
| 54 #endif |
| 55 /* |
| 56 mini unzip, demo of unzip package |
| 57 |
| 58 usage : |
| 59 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir] |
| 60 |
| 61 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.T
XT |
| 62 if it exists |
| 63 */ |
| 64 |
| 65 |
| 66 /* change_file_date : change the date/time of a file |
| 67 filename : the filename of the file where date/time must be modified |
| 68 dosdate : the new date at the MSDos format (4 bytes) |
| 69 tmu_date : the SAME new date at the tm_unz format */ |
| 70 void change_file_date(filename,dosdate,tmu_date) |
| 71 const char *filename; |
| 72 uLong dosdate; |
| 73 tm_unz tmu_date; |
| 74 { |
| 75 #ifdef _WIN32 |
| 76 HANDLE hFile; |
| 77 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite; |
| 78 |
| 79 hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE, |
| 80 0,NULL,OPEN_EXISTING,0,NULL); |
| 81 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite); |
| 82 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal); |
| 83 LocalFileTimeToFileTime(&ftLocal,&ftm); |
| 84 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm); |
| 85 CloseHandle(hFile); |
| 86 #else |
| 87 #ifdef unix |
| 88 struct utimbuf ut; |
| 89 struct tm newdate; |
| 90 newdate.tm_sec = tmu_date.tm_sec; |
| 91 newdate.tm_min=tmu_date.tm_min; |
| 92 newdate.tm_hour=tmu_date.tm_hour; |
| 93 newdate.tm_mday=tmu_date.tm_mday; |
| 94 newdate.tm_mon=tmu_date.tm_mon; |
| 95 if (tmu_date.tm_year > 1900) |
| 96 newdate.tm_year=tmu_date.tm_year - 1900; |
| 97 else |
| 98 newdate.tm_year=tmu_date.tm_year ; |
| 99 newdate.tm_isdst=-1; |
| 100 |
| 101 ut.actime=ut.modtime=mktime(&newdate); |
| 102 utime(filename,&ut); |
| 103 #endif |
| 104 #endif |
| 105 } |
| 106 |
| 107 |
| 108 /* mymkdir and change_file_date are not 100 % portable |
| 109 As I don't know well Unix, I wait feedback for the unix portion */ |
| 110 |
| 111 int mymkdir(dirname) |
| 112 const char* dirname; |
| 113 { |
| 114 int ret=0; |
| 115 #ifdef _WIN32 |
| 116 ret = _mkdir(dirname); |
| 117 #else |
| 118 #ifdef unix |
| 119 ret = mkdir (dirname,0775); |
| 120 #endif |
| 121 #endif |
| 122 return ret; |
| 123 } |
| 124 |
| 125 int makedir (newdir) |
| 126 char *newdir; |
| 127 { |
| 128 char *buffer ; |
| 129 char *p; |
| 130 int len = (int)strlen(newdir); |
| 131 |
| 132 if (len <= 0) |
| 133 return 0; |
| 134 |
| 135 buffer = (char*)malloc(len+1); |
| 136 if (buffer==NULL) |
| 137 { |
| 138 printf("Error allocating memory\n"); |
| 139 return UNZ_INTERNALERROR; |
| 140 } |
| 141 strcpy(buffer,newdir); |
| 142 |
| 143 if (buffer[len-1] == '/') { |
| 144 buffer[len-1] = '\0'; |
| 145 } |
| 146 if (mymkdir(buffer) == 0) |
| 147 { |
| 148 free(buffer); |
| 149 return 1; |
| 150 } |
| 151 |
| 152 p = buffer+1; |
| 153 while (1) |
| 154 { |
| 155 char hold; |
| 156 |
| 157 while(*p && *p != '\\' && *p != '/') |
| 158 p++; |
| 159 hold = *p; |
| 160 *p = 0; |
| 161 if ((mymkdir(buffer) == -1) && (errno == ENOENT)) |
| 162 { |
| 163 printf("couldn't create directory %s\n",buffer); |
| 164 free(buffer); |
| 165 return 0; |
| 166 } |
| 167 if (hold == 0) |
| 168 break; |
| 169 *p++ = hold; |
| 170 } |
| 171 free(buffer); |
| 172 return 1; |
| 173 } |
| 174 |
| 175 void do_banner() |
| 176 { |
| 177 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\
n"); |
| 178 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); |
| 179 } |
| 180 |
| 181 void do_help() |
| 182 { |
| 183 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [fil
e_to_extr.] [-d extractdir]\n\n" \ |
| 184 " -e Extract without pathname (junk paths)\n" \ |
| 185 " -x Extract with pathname\n" \ |
| 186 " -v list files\n" \ |
| 187 " -l list files\n" \ |
| 188 " -d directory to extract into\n" \ |
| 189 " -o overwrite files without prompting\n" \ |
| 190 " -p extract crypted file using password\n\n"); |
| 191 } |
| 192 |
| 193 void Display64BitsSize(ZPOS64_T n, int size_char) |
| 194 { |
| 195 /* to avoid compatibility problem , we do here the conversion */ |
| 196 char number[21]; |
| 197 int offset=19; |
| 198 int pos_string = 19; |
| 199 number[20]=0; |
| 200 for (;;) { |
| 201 number[offset]=(char)((n%10)+'0'); |
| 202 if (number[offset] != '0') |
| 203 pos_string=offset; |
| 204 n/=10; |
| 205 if (offset==0) |
| 206 break; |
| 207 offset--; |
| 208 } |
| 209 { |
| 210 int size_display_string = 19-pos_string; |
| 211 while (size_char > size_display_string) |
| 212 { |
| 213 size_char--; |
| 214 printf(" "); |
| 215 } |
| 216 } |
| 217 |
| 218 printf("%s",&number[pos_string]); |
| 219 } |
| 220 |
| 221 int do_list(uf) |
| 222 unzFile uf; |
| 223 { |
| 224 uLong i; |
| 225 unz_global_info64 gi; |
| 226 int err; |
| 227 |
| 228 err = unzGetGlobalInfo64(uf,&gi); |
| 229 if (err!=UNZ_OK) |
| 230 printf("error %d with zipfile in unzGetGlobalInfo \n",err); |
| 231 printf(" Length Method Size Ratio Date Time CRC-32 Name\n")
; |
| 232 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n")
; |
| 233 for (i=0;i<gi.number_entry;i++) |
| 234 { |
| 235 char filename_inzip[256]; |
| 236 unz_file_info64 file_info; |
| 237 uLong ratio=0; |
| 238 const char *string_method; |
| 239 char charCrypt=' '; |
| 240 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filena
me_inzip),NULL,0,NULL,0); |
| 241 if (err!=UNZ_OK) |
| 242 { |
| 243 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); |
| 244 break; |
| 245 } |
| 246 if (file_info.uncompressed_size>0) |
| 247 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompress
ed_size); |
| 248 |
| 249 /* display a '*' if the file is crypted */ |
| 250 if ((file_info.flag & 1) != 0) |
| 251 charCrypt='*'; |
| 252 |
| 253 if (file_info.compression_method==0) |
| 254 string_method="Stored"; |
| 255 else |
| 256 if (file_info.compression_method==Z_DEFLATED) |
| 257 { |
| 258 uInt iLevel=(uInt)((file_info.flag & 0x6)/2); |
| 259 if (iLevel==0) |
| 260 string_method="Defl:N"; |
| 261 else if (iLevel==1) |
| 262 string_method="Defl:X"; |
| 263 else if ((iLevel==2) || (iLevel==3)) |
| 264 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ |
| 265 } |
| 266 else |
| 267 if (file_info.compression_method==Z_BZIP2ED) |
| 268 { |
| 269 string_method="BZip2 "; |
| 270 } |
| 271 else |
| 272 string_method="Unkn. "; |
| 273 |
| 274 Display64BitsSize(file_info.uncompressed_size,7); |
| 275 printf(" %6s%c",string_method,charCrypt); |
| 276 Display64BitsSize(file_info.compressed_size,7); |
| 277 printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", |
| 278 ratio, |
| 279 (uLong)file_info.tmu_date.tm_mon + 1, |
| 280 (uLong)file_info.tmu_date.tm_mday, |
| 281 (uLong)file_info.tmu_date.tm_year % 100, |
| 282 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_m
in, |
| 283 (uLong)file_info.crc,filename_inzip); |
| 284 if ((i+1)<gi.number_entry) |
| 285 { |
| 286 err = unzGoToNextFile(uf); |
| 287 if (err!=UNZ_OK) |
| 288 { |
| 289 printf("error %d with zipfile in unzGoToNextFile\n",err); |
| 290 break; |
| 291 } |
| 292 } |
| 293 } |
| 294 |
| 295 return 0; |
| 296 } |
| 297 |
| 298 |
| 299 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password) |
| 300 unzFile uf; |
| 301 const int* popt_extract_without_path; |
| 302 int* popt_overwrite; |
| 303 const char* password; |
| 304 { |
| 305 char filename_inzip[256]; |
| 306 char* filename_withoutpath; |
| 307 char* p; |
| 308 int err=UNZ_OK; |
| 309 FILE *fout=NULL; |
| 310 void* buf; |
| 311 uInt size_buf; |
| 312 |
| 313 unz_file_info64 file_info; |
| 314 uLong ratio=0; |
| 315 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_i
nzip),NULL,0,NULL,0); |
| 316 |
| 317 if (err!=UNZ_OK) |
| 318 { |
| 319 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); |
| 320 return err; |
| 321 } |
| 322 |
| 323 size_buf = WRITEBUFFERSIZE; |
| 324 buf = (void*)malloc(size_buf); |
| 325 if (buf==NULL) |
| 326 { |
| 327 printf("Error allocating memory\n"); |
| 328 return UNZ_INTERNALERROR; |
| 329 } |
| 330 |
| 331 p = filename_withoutpath = filename_inzip; |
| 332 while ((*p) != '\0') |
| 333 { |
| 334 if (((*p)=='/') || ((*p)=='\\')) |
| 335 filename_withoutpath = p+1; |
| 336 p++; |
| 337 } |
| 338 |
| 339 if ((*filename_withoutpath)=='\0') |
| 340 { |
| 341 if ((*popt_extract_without_path)==0) |
| 342 { |
| 343 printf("creating directory: %s\n",filename_inzip); |
| 344 mymkdir(filename_inzip); |
| 345 } |
| 346 } |
| 347 else |
| 348 { |
| 349 const char* write_filename; |
| 350 int skip=0; |
| 351 |
| 352 if ((*popt_extract_without_path)==0) |
| 353 write_filename = filename_inzip; |
| 354 else |
| 355 write_filename = filename_withoutpath; |
| 356 |
| 357 err = unzOpenCurrentFilePassword(uf,password); |
| 358 if (err!=UNZ_OK) |
| 359 { |
| 360 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); |
| 361 } |
| 362 |
| 363 if (((*popt_overwrite)==0) && (err==UNZ_OK)) |
| 364 { |
| 365 char rep=0; |
| 366 FILE* ftestexist; |
| 367 ftestexist = fopen64(write_filename,"rb"); |
| 368 if (ftestexist!=NULL) |
| 369 { |
| 370 fclose(ftestexist); |
| 371 do |
| 372 { |
| 373 char answer[128]; |
| 374 int ret; |
| 375 |
| 376 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll:
",write_filename); |
| 377 ret = scanf("%1s",answer); |
| 378 if (ret != 1) |
| 379 { |
| 380 exit(EXIT_FAILURE); |
| 381 } |
| 382 rep = answer[0] ; |
| 383 if ((rep>='a') && (rep<='z')) |
| 384 rep -= 0x20; |
| 385 } |
| 386 while ((rep!='Y') && (rep!='N') && (rep!='A')); |
| 387 } |
| 388 |
| 389 if (rep == 'N') |
| 390 skip = 1; |
| 391 |
| 392 if (rep == 'A') |
| 393 *popt_overwrite=1; |
| 394 } |
| 395 |
| 396 if ((skip==0) && (err==UNZ_OK)) |
| 397 { |
| 398 fout=fopen64(write_filename,"wb"); |
| 399 |
| 400 /* some zipfile don't contain directory alone before file */ |
| 401 if ((fout==NULL) && ((*popt_extract_without_path)==0) && |
| 402 (filename_withoutpath!=(char*)filename_inzip)) |
| 403 { |
| 404 char c=*(filename_withoutpath-1); |
| 405 *(filename_withoutpath-1)='\0'; |
| 406 makedir(write_filename); |
| 407 *(filename_withoutpath-1)=c; |
| 408 fout=fopen64(write_filename,"wb"); |
| 409 } |
| 410 |
| 411 if (fout==NULL) |
| 412 { |
| 413 printf("error opening %s\n",write_filename); |
| 414 } |
| 415 } |
| 416 |
| 417 if (fout!=NULL) |
| 418 { |
| 419 printf(" extracting: %s\n",write_filename); |
| 420 |
| 421 do |
| 422 { |
| 423 err = unzReadCurrentFile(uf,buf,size_buf); |
| 424 if (err<0) |
| 425 { |
| 426 printf("error %d with zipfile in unzReadCurrentFile\n",err); |
| 427 break; |
| 428 } |
| 429 if (err>0) |
| 430 if (fwrite(buf,err,1,fout)!=1) |
| 431 { |
| 432 printf("error in writing extracted file\n"); |
| 433 err=UNZ_ERRNO; |
| 434 break; |
| 435 } |
| 436 } |
| 437 while (err>0); |
| 438 if (fout) |
| 439 fclose(fout); |
| 440 |
| 441 if (err==0) |
| 442 change_file_date(write_filename,file_info.dosDate, |
| 443 file_info.tmu_date); |
| 444 } |
| 445 |
| 446 if (err==UNZ_OK) |
| 447 { |
| 448 err = unzCloseCurrentFile (uf); |
| 449 if (err!=UNZ_OK) |
| 450 { |
| 451 printf("error %d with zipfile in unzCloseCurrentFile\n",err); |
| 452 } |
| 453 } |
| 454 else |
| 455 unzCloseCurrentFile(uf); /* don't lose the error */ |
| 456 } |
| 457 |
| 458 free(buf); |
| 459 return err; |
| 460 } |
| 461 |
| 462 |
| 463 int do_extract(uf,opt_extract_without_path,opt_overwrite,password) |
| 464 unzFile uf; |
| 465 int opt_extract_without_path; |
| 466 int opt_overwrite; |
| 467 const char* password; |
| 468 { |
| 469 uLong i; |
| 470 unz_global_info64 gi; |
| 471 int err; |
| 472 FILE* fout=NULL; |
| 473 |
| 474 err = unzGetGlobalInfo64(uf,&gi); |
| 475 if (err!=UNZ_OK) |
| 476 printf("error %d with zipfile in unzGetGlobalInfo \n",err); |
| 477 |
| 478 for (i=0;i<gi.number_entry;i++) |
| 479 { |
| 480 if (do_extract_currentfile(uf,&opt_extract_without_path, |
| 481 &opt_overwrite, |
| 482 password) != UNZ_OK) |
| 483 break; |
| 484 |
| 485 if ((i+1)<gi.number_entry) |
| 486 { |
| 487 err = unzGoToNextFile(uf); |
| 488 if (err!=UNZ_OK) |
| 489 { |
| 490 printf("error %d with zipfile in unzGoToNextFile\n",err); |
| 491 break; |
| 492 } |
| 493 } |
| 494 } |
| 495 |
| 496 return 0; |
| 497 } |
| 498 |
| 499 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,passwo
rd) |
| 500 unzFile uf; |
| 501 const char* filename; |
| 502 int opt_extract_without_path; |
| 503 int opt_overwrite; |
| 504 const char* password; |
| 505 { |
| 506 int err = UNZ_OK; |
| 507 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK) |
| 508 { |
| 509 printf("file %s not found in the zipfile\n",filename); |
| 510 return 2; |
| 511 } |
| 512 |
| 513 if (do_extract_currentfile(uf,&opt_extract_without_path, |
| 514 &opt_overwrite, |
| 515 password) == UNZ_OK) |
| 516 return 0; |
| 517 else |
| 518 return 1; |
| 519 } |
| 520 |
| 521 |
| 522 int main(argc,argv) |
| 523 int argc; |
| 524 char *argv[]; |
| 525 { |
| 526 const char *zipfilename=NULL; |
| 527 const char *filename_to_extract=NULL; |
| 528 const char *password=NULL; |
| 529 char filename_try[MAXFILENAME+16] = ""; |
| 530 int i; |
| 531 int ret_value=0; |
| 532 int opt_do_list=0; |
| 533 int opt_do_extract=1; |
| 534 int opt_do_extract_withoutpath=0; |
| 535 int opt_overwrite=0; |
| 536 int opt_extractdir=0; |
| 537 const char *dirname=NULL; |
| 538 unzFile uf=NULL; |
| 539 |
| 540 do_banner(); |
| 541 if (argc==1) |
| 542 { |
| 543 do_help(); |
| 544 return 0; |
| 545 } |
| 546 else |
| 547 { |
| 548 for (i=1;i<argc;i++) |
| 549 { |
| 550 if ((*argv[i])=='-') |
| 551 { |
| 552 const char *p=argv[i]+1; |
| 553 |
| 554 while ((*p)!='\0') |
| 555 { |
| 556 char c=*(p++);; |
| 557 if ((c=='l') || (c=='L')) |
| 558 opt_do_list = 1; |
| 559 if ((c=='v') || (c=='V')) |
| 560 opt_do_list = 1; |
| 561 if ((c=='x') || (c=='X')) |
| 562 opt_do_extract = 1; |
| 563 if ((c=='e') || (c=='E')) |
| 564 opt_do_extract = opt_do_extract_withoutpath = 1; |
| 565 if ((c=='o') || (c=='O')) |
| 566 opt_overwrite=1; |
| 567 if ((c=='d') || (c=='D')) |
| 568 { |
| 569 opt_extractdir=1; |
| 570 dirname=argv[i+1]; |
| 571 } |
| 572 |
| 573 if (((c=='p') || (c=='P')) && (i+1<argc)) |
| 574 { |
| 575 password=argv[i+1]; |
| 576 i++; |
| 577 } |
| 578 } |
| 579 } |
| 580 else |
| 581 { |
| 582 if (zipfilename == NULL) |
| 583 zipfilename = argv[i]; |
| 584 else if ((filename_to_extract==NULL) && (!opt_extractdir)) |
| 585 filename_to_extract = argv[i] ; |
| 586 } |
| 587 } |
| 588 } |
| 589 |
| 590 if (zipfilename!=NULL) |
| 591 { |
| 592 |
| 593 # ifdef USEWIN32IOAPI |
| 594 zlib_filefunc64_def ffunc; |
| 595 # endif |
| 596 |
| 597 strncpy(filename_try, zipfilename,MAXFILENAME-1); |
| 598 /* strncpy doesnt append the trailing NULL, of the string is too long. *
/ |
| 599 filename_try[ MAXFILENAME ] = '\0'; |
| 600 |
| 601 # ifdef USEWIN32IOAPI |
| 602 fill_win32_filefunc64A(&ffunc); |
| 603 uf = unzOpen2_64(zipfilename,&ffunc); |
| 604 # else |
| 605 uf = unzOpen64(zipfilename); |
| 606 # endif |
| 607 if (uf==NULL) |
| 608 { |
| 609 strcat(filename_try,".zip"); |
| 610 # ifdef USEWIN32IOAPI |
| 611 uf = unzOpen2_64(filename_try,&ffunc); |
| 612 # else |
| 613 uf = unzOpen64(filename_try); |
| 614 # endif |
| 615 } |
| 616 } |
| 617 |
| 618 if (uf==NULL) |
| 619 { |
| 620 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); |
| 621 return 1; |
| 622 } |
| 623 printf("%s opened\n",filename_try); |
| 624 |
| 625 if (opt_do_list==1) |
| 626 ret_value = do_list(uf); |
| 627 else if (opt_do_extract==1) |
| 628 { |
| 629 #ifdef _WIN32 |
| 630 if (opt_extractdir && _chdir(dirname)) |
| 631 #else |
| 632 if (opt_extractdir && chdir(dirname)) |
| 633 #endif |
| 634 { |
| 635 printf("Error changing into %s, aborting\n", dirname); |
| 636 exit(-1); |
| 637 } |
| 638 |
| 639 if (filename_to_extract == NULL) |
| 640 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite
, password); |
| 641 else |
| 642 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extra
ct_withoutpath, opt_overwrite, password); |
| 643 } |
| 644 |
| 645 unzClose(uf); |
| 646 |
| 647 return ret_value; |
| 648 } |
OLD | NEW |