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