OLD | NEW |
1 /* | 1 /* |
2 minizip.c | 2 minizip.c |
3 Version 1.01e, February 12th, 2005 | 3 Version 1.1, February 14h, 2010 |
| 4 sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizi
p.html ) |
4 | 5 |
5 Copyright (C) 1998-2005 Gilles Vollant | 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 ) |
6 */ | 13 */ |
7 | 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 |
8 #include <stdio.h> | 31 #include <stdio.h> |
9 #include <stdlib.h> | 32 #include <stdlib.h> |
10 #include <string.h> | 33 #include <string.h> |
11 #include <time.h> | 34 #include <time.h> |
12 #include <errno.h> | 35 #include <errno.h> |
13 #include <fcntl.h> | 36 #include <fcntl.h> |
14 | 37 |
15 #ifdef unix | 38 #ifdef unix |
16 # include <unistd.h> | 39 # include <unistd.h> |
17 # include <utime.h> | 40 # include <utime.h> |
18 # include <sys/types.h> | 41 # include <sys/types.h> |
19 # include <sys/stat.h> | 42 # include <sys/stat.h> |
20 #else | 43 #else |
21 # include <direct.h> | 44 # include <direct.h> |
22 # include <io.h> | 45 # include <io.h> |
23 #endif | 46 #endif |
24 | 47 |
25 #include "zip.h" | 48 #include "zip.h" |
26 | 49 |
27 #ifdef WIN32 | 50 #ifdef _WIN32 |
28 #define USEWIN32IOAPI | 51 #define USEWIN32IOAPI |
29 #include "iowin32.h" | 52 #include "iowin32.h" |
30 #endif | 53 #endif |
31 | 54 |
32 | 55 |
33 | 56 |
34 #define WRITEBUFFERSIZE (16384) | 57 #define WRITEBUFFERSIZE (16384) |
35 #define MAXFILENAME (256) | 58 #define MAXFILENAME (256) |
36 | 59 |
37 #ifdef WIN32 | 60 #ifdef _WIN32 |
38 uLong filetime(f, tmzip, dt) | 61 uLong filetime(f, tmzip, dt) |
39 char *f; /* name of file to get info on */ | 62 char *f; /* name of file to get info on */ |
40 tm_zip *tmzip; /* return value: access, modific. and creation ti
mes */ | 63 tm_zip *tmzip; /* return value: access, modific. and creation ti
mes */ |
41 uLong *dt; /* dostime */ | 64 uLong *dt; /* dostime */ |
42 { | 65 { |
43 int ret = 0; | 66 int ret = 0; |
44 { | 67 { |
45 FILETIME ftLocal; | 68 FILETIME ftLocal; |
46 HANDLE hFind; | 69 HANDLE hFind; |
47 WIN32_FIND_DATA ff32; | 70 WIN32_FIND_DATAA ff32; |
48 | 71 |
49 hFind = FindFirstFile(f,&ff32); | 72 hFind = FindFirstFileA(f,&ff32); |
50 if (hFind != INVALID_HANDLE_VALUE) | 73 if (hFind != INVALID_HANDLE_VALUE) |
51 { | 74 { |
52 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); | 75 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); |
53 FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); | 76 FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); |
54 FindClose(hFind); | 77 FindClose(hFind); |
55 ret = 1; | 78 ret = 1; |
56 } | 79 } |
57 } | 80 } |
58 return ret; | 81 return ret; |
59 } | 82 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 #endif | 135 #endif |
113 | 136 |
114 | 137 |
115 | 138 |
116 | 139 |
117 int check_exist_file(filename) | 140 int check_exist_file(filename) |
118 const char* filename; | 141 const char* filename; |
119 { | 142 { |
120 FILE* ftestexist; | 143 FILE* ftestexist; |
121 int ret = 1; | 144 int ret = 1; |
122 ftestexist = fopen(filename,"rb"); | 145 ftestexist = fopen64(filename,"rb"); |
123 if (ftestexist==NULL) | 146 if (ftestexist==NULL) |
124 ret = 0; | 147 ret = 0; |
125 else | 148 else |
126 fclose(ftestexist); | 149 fclose(ftestexist); |
127 return ret; | 150 return ret; |
128 } | 151 } |
129 | 152 |
130 void do_banner() | 153 void do_banner() |
131 { | 154 { |
132 printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\
n"); | 155 printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vol
lant\n"); |
133 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); | 156 printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html
\n\n"); |
134 } | 157 } |
135 | 158 |
136 void do_help() | 159 void do_help() |
137 { | 160 { |
138 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_t
o_add]\n\n" \ | 161 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [fi
les_to_add]\n\n" \ |
139 " -o Overwrite existing file.zip\n" \ | 162 " -o Overwrite existing file.zip\n" \ |
140 " -a Append to existing file.zip\n" \ | 163 " -a Append to existing file.zip\n" \ |
141 " -0 Store only\n" \ | 164 " -0 Store only\n" \ |
142 " -1 Compress faster\n" \ | 165 " -1 Compress faster\n" \ |
143 " -9 Compress better\n\n"); | 166 " -9 Compress better\n\n" \ |
| 167 " -j exclude path. store only the file name.\n\n"); |
144 } | 168 } |
145 | 169 |
146 /* calculate the CRC32 of a file, | 170 /* calculate the CRC32 of a file, |
147 because to encrypt a file, we need known the CRC32 of the file before */ | 171 because to encrypt a file, we need known the CRC32 of the file before */ |
148 int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigne
d long* result_crc) | 172 int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigne
d long* result_crc) |
149 { | 173 { |
150 unsigned long calculate_crc=0; | 174 unsigned long calculate_crc=0; |
151 int err=ZIP_OK; | 175 int err=ZIP_OK; |
152 FILE * fin = fopen(filenameinzip,"rb"); | 176 FILE * fin = fopen64(filenameinzip,"rb"); |
153 unsigned long size_read = 0; | 177 unsigned long size_read = 0; |
154 unsigned long total_read = 0; | 178 unsigned long total_read = 0; |
155 if (fin==NULL) | 179 if (fin==NULL) |
156 { | 180 { |
157 err = ZIP_ERRNO; | 181 err = ZIP_ERRNO; |
158 } | 182 } |
159 | 183 |
160 if (err == ZIP_OK) | 184 if (err == ZIP_OK) |
161 do | 185 do |
162 { | 186 { |
163 err = ZIP_OK; | 187 err = ZIP_OK; |
164 size_read = (int)fread(buf,1,size_buf,fin); | 188 size_read = (int)fread(buf,1,size_buf,fin); |
165 if (size_read < size_buf) | 189 if (size_read < size_buf) |
166 if (feof(fin)==0) | 190 if (feof(fin)==0) |
167 { | 191 { |
168 printf("error in reading %s\n",filenameinzip); | 192 printf("error in reading %s\n",filenameinzip); |
169 err = ZIP_ERRNO; | 193 err = ZIP_ERRNO; |
170 } | 194 } |
171 | 195 |
172 if (size_read>0) | 196 if (size_read>0) |
173 calculate_crc = crc32(calculate_crc,buf,size_read); | 197 calculate_crc = crc32(calculate_crc,buf,size_read); |
174 total_read += size_read; | 198 total_read += size_read; |
175 | 199 |
176 } while ((err == ZIP_OK) && (size_read>0)); | 200 } while ((err == ZIP_OK) && (size_read>0)); |
177 | 201 |
178 if (fin) | 202 if (fin) |
179 fclose(fin); | 203 fclose(fin); |
180 | 204 |
181 *result_crc=calculate_crc; | 205 *result_crc=calculate_crc; |
182 printf("file %s crc %x\n",filenameinzip,calculate_crc); | 206 printf("file %s crc %lx\n", filenameinzip, calculate_crc); |
183 return err; | 207 return err; |
184 } | 208 } |
185 | 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 |
186 int main(argc,argv) | 233 int main(argc,argv) |
187 int argc; | 234 int argc; |
188 char *argv[]; | 235 char *argv[]; |
189 { | 236 { |
190 int i; | 237 int i; |
191 int opt_overwrite=0; | 238 int opt_overwrite=0; |
192 int opt_compress_level=Z_DEFAULT_COMPRESSION; | 239 int opt_compress_level=Z_DEFAULT_COMPRESSION; |
| 240 int opt_exclude_path=0; |
193 int zipfilenamearg = 0; | 241 int zipfilenamearg = 0; |
194 char filename_try[MAXFILENAME+16]; | 242 char filename_try[MAXFILENAME+16]; |
195 int zipok; | 243 int zipok; |
196 int err=0; | 244 int err=0; |
197 int size_buf=0; | 245 int size_buf=0; |
198 void* buf=NULL; | 246 void* buf=NULL; |
199 const char* password=NULL; | 247 const char* password=NULL; |
200 | 248 |
201 | 249 |
202 do_banner(); | 250 do_banner(); |
(...skipping 12 matching lines...) Expand all Loading... |
215 | 263 |
216 while ((*p)!='\0') | 264 while ((*p)!='\0') |
217 { | 265 { |
218 char c=*(p++);; | 266 char c=*(p++);; |
219 if ((c=='o') || (c=='O')) | 267 if ((c=='o') || (c=='O')) |
220 opt_overwrite = 1; | 268 opt_overwrite = 1; |
221 if ((c=='a') || (c=='A')) | 269 if ((c=='a') || (c=='A')) |
222 opt_overwrite = 2; | 270 opt_overwrite = 2; |
223 if ((c>='0') && (c<='9')) | 271 if ((c>='0') && (c<='9')) |
224 opt_compress_level = c-'0'; | 272 opt_compress_level = c-'0'; |
| 273 if ((c=='j') || (c=='J')) |
| 274 opt_exclude_path = 1; |
225 | 275 |
226 if (((c=='p') || (c=='P')) && (i+1<argc)) | 276 if (((c=='p') || (c=='P')) && (i+1<argc)) |
227 { | 277 { |
228 password=argv[i+1]; | 278 password=argv[i+1]; |
229 i++; | 279 i++; |
230 } | 280 } |
231 } | 281 } |
232 } | 282 } |
233 else | 283 else |
| 284 { |
234 if (zipfilenamearg == 0) | 285 if (zipfilenamearg == 0) |
| 286 { |
235 zipfilenamearg = i ; | 287 zipfilenamearg = i ; |
| 288 } |
| 289 } |
236 } | 290 } |
237 } | 291 } |
238 | 292 |
239 size_buf = WRITEBUFFERSIZE; | 293 size_buf = WRITEBUFFERSIZE; |
240 buf = (void*)malloc(size_buf); | 294 buf = (void*)malloc(size_buf); |
241 if (buf==NULL) | 295 if (buf==NULL) |
242 { | 296 { |
243 printf("Error allocating memory\n"); | 297 printf("Error allocating memory\n"); |
244 return ZIP_INTERNALERROR; | 298 return ZIP_INTERNALERROR; |
245 } | 299 } |
246 | 300 |
247 if (zipfilenamearg==0) | 301 if (zipfilenamearg==0) |
| 302 { |
248 zipok=0; | 303 zipok=0; |
| 304 } |
249 else | 305 else |
250 { | 306 { |
251 int i,len; | 307 int i,len; |
252 int dot_found=0; | 308 int dot_found=0; |
253 | 309 |
254 zipok = 1 ; | 310 zipok = 1 ; |
255 strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); | 311 strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); |
256 /* strncpy doesnt append the trailing NULL, of the string is too long. *
/ | 312 /* strncpy doesnt append the trailing NULL, of the string is too long. *
/ |
257 filename_try[ MAXFILENAME ] = '\0'; | 313 filename_try[ MAXFILENAME ] = '\0'; |
258 | 314 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 if (rep=='A') | 351 if (rep=='A') |
296 opt_overwrite = 2; | 352 opt_overwrite = 2; |
297 } | 353 } |
298 } | 354 } |
299 | 355 |
300 if (zipok==1) | 356 if (zipok==1) |
301 { | 357 { |
302 zipFile zf; | 358 zipFile zf; |
303 int errclose; | 359 int errclose; |
304 # ifdef USEWIN32IOAPI | 360 # ifdef USEWIN32IOAPI |
305 zlib_filefunc_def ffunc; | 361 zlib_filefunc64_def ffunc; |
306 fill_win32_filefunc(&ffunc); | 362 fill_win32_filefunc64A(&ffunc); |
307 zf = zipOpen2(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); | 363 zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); |
308 # else | 364 # else |
309 zf = zipOpen(filename_try,(opt_overwrite==2) ? 2 : 0); | 365 zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0); |
310 # endif | 366 # endif |
311 | 367 |
312 if (zf == NULL) | 368 if (zf == NULL) |
313 { | 369 { |
314 printf("error opening %s\n",filename_try); | 370 printf("error opening %s\n",filename_try); |
315 err= ZIP_ERRNO; | 371 err= ZIP_ERRNO; |
316 } | 372 } |
317 else | 373 else |
318 printf("creating %s\n",filename_try); | 374 printf("creating %s\n",filename_try); |
319 | 375 |
320 for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) | 376 for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) |
321 { | 377 { |
322 if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) && | 378 if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) && |
323 ((argv[i][1]=='o') || (argv[i][1]=='O') || | 379 ((argv[i][1]=='o') || (argv[i][1]=='O') || |
324 (argv[i][1]=='a') || (argv[i][1]=='A') || | 380 (argv[i][1]=='a') || (argv[i][1]=='A') || |
325 (argv[i][1]=='p') || (argv[i][1]=='P') || | 381 (argv[i][1]=='p') || (argv[i][1]=='P') || |
326 ((argv[i][1]>='0') || (argv[i][1]<='9'))) && | 382 ((argv[i][1]>='0') || (argv[i][1]<='9'))) && |
327 (strlen(argv[i]) == 2))) | 383 (strlen(argv[i]) == 2))) |
328 { | 384 { |
329 FILE * fin; | 385 FILE * fin; |
330 int size_read; | 386 int size_read; |
331 const char* filenameinzip = argv[i]; | 387 const char* filenameinzip = argv[i]; |
| 388 const char *savefilenameinzip; |
332 zip_fileinfo zi; | 389 zip_fileinfo zi; |
333 unsigned long crcFile=0; | 390 unsigned long crcFile=0; |
| 391 int zip64 = 0; |
334 | 392 |
335 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = | 393 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = |
336 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year =
0; | 394 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year =
0; |
337 zi.dosDate = 0; | 395 zi.dosDate = 0; |
338 zi.internal_fa = 0; | 396 zi.internal_fa = 0; |
339 zi.external_fa = 0; | 397 zi.external_fa = 0; |
340 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); | 398 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); |
341 | 399 |
342 /* | 400 /* |
343 err = zipOpenNewFileInZip(zf,filenameinzip,&zi, | 401 err = zipOpenNewFileInZip(zf,filenameinzip,&zi, |
344 NULL,0,NULL,0,NULL / * comment * /, | 402 NULL,0,NULL,0,NULL / * comment * /, |
345 (opt_compress_level != 0) ? Z_DEFLATED : 0, | 403 (opt_compress_level != 0) ? Z_DEFLATED : 0, |
346 opt_compress_level); | 404 opt_compress_level); |
347 */ | 405 */ |
348 if ((password != NULL) && (err==ZIP_OK)) | 406 if ((password != NULL) && (err==ZIP_OK)) |
349 err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); | 407 err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); |
350 | 408 |
351 err = zipOpenNewFileInZip3(zf,filenameinzip,&zi, | 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 follo
ws last slash. |
| 434 } |
| 435 } |
| 436 |
| 437 /**/ |
| 438 err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi, |
352 NULL,0,NULL,0,NULL /* comment*/, | 439 NULL,0,NULL,0,NULL /* comment*/, |
353 (opt_compress_level != 0) ? Z_DEFLATED : 0, | 440 (opt_compress_level != 0) ? Z_DEFLATED : 0, |
354 opt_compress_level,0, | 441 opt_compress_level,0, |
355 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEG
Y, */ | 442 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEG
Y, */ |
356 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, | 443 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, |
357 password,crcFile); | 444 password,crcFile, zip64); |
358 | 445 |
359 if (err != ZIP_OK) | 446 if (err != ZIP_OK) |
360 printf("error in opening %s in zipfile\n",filenameinzip); | 447 printf("error in opening %s in zipfile\n",filenameinzip); |
361 else | 448 else |
362 { | 449 { |
363 fin = fopen(filenameinzip,"rb"); | 450 fin = fopen64(filenameinzip,"rb"); |
364 if (fin==NULL) | 451 if (fin==NULL) |
365 { | 452 { |
366 err=ZIP_ERRNO; | 453 err=ZIP_ERRNO; |
367 printf("error in opening %s for reading\n",filenameinzip
); | 454 printf("error in opening %s for reading\n",filenameinzip
); |
368 } | 455 } |
369 } | 456 } |
370 | 457 |
371 if (err == ZIP_OK) | 458 if (err == ZIP_OK) |
372 do | 459 do |
373 { | 460 { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 printf("error in closing %s\n",filename_try); | 498 printf("error in closing %s\n",filename_try); |
412 } | 499 } |
413 else | 500 else |
414 { | 501 { |
415 do_help(); | 502 do_help(); |
416 } | 503 } |
417 | 504 |
418 free(buf); | 505 free(buf); |
419 return 0; | 506 return 0; |
420 } | 507 } |
OLD | NEW |