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