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