annotate src/zlib-1.2.8/contrib/minizip/miniunz.c @ 43:5ea0608b923f

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