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