3 Version 1.1, February 14h, 2010
4 sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
6 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
8 Modifications of Unzip for Zip64
9 Copyright (C) 2007-2008 Even Rouault
11 Modifications for Zip64 support on both zip and unzip
12 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
15 #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
16 #ifndef __USE_FILE_OFFSET64
17 #define __USE_FILE_OFFSET64
19 #ifndef __USE_LARGEFILE64
20 #define __USE_LARGEFILE64
22 #ifndef _LARGEFILE64_SOURCE
23 #define _LARGEFILE64_SOURCE
25 #ifndef _FILE_OFFSET_BIT
26 #define _FILE_OFFSET_BIT 64
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)
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)
61 #define CASESENSITIVITY (0)
62 #define WRITEBUFFERSIZE (8192)
63 #define MAXFILENAME (256)
71 /* MODIFICATION Replace all stdout prints with this function for better control */
72 static int minizip_printf( const char * format, ... )
79 mini unzip, demo of unzip package
82 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
84 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
89 /* change_file_date : change the date/time of a file
90 filename : the filename of the file where date/time must be modified
91 dosdate : the new date at the MSDos format (4 bytes)
92 tmu_date : the SAME new date at the tm_unz format */
93 void change_file_date(filename,dosdate,tmu_date)
100 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
102 hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
103 0,NULL,OPEN_EXISTING,0,NULL);
104 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
105 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
106 LocalFileTimeToFileTime(&ftLocal,&ftm);
107 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
110 #ifdef unix || __APPLE__
113 newdate.tm_sec = tmu_date.tm_sec;
114 newdate.tm_min=tmu_date.tm_min;
115 newdate.tm_hour=tmu_date.tm_hour;
116 newdate.tm_mday=tmu_date.tm_mday;
117 newdate.tm_mon=tmu_date.tm_mon;
118 if (tmu_date.tm_year > 1900)
119 newdate.tm_year=tmu_date.tm_year - 1900;
121 newdate.tm_year=tmu_date.tm_year ;
124 ut.actime=ut.modtime=mktime(&newdate);
131 /* mymkdir and change_file_date are not 100 % portable
132 As I don't know well Unix, I wait feedback for the unix portion */
139 ret = _mkdir(dirname);
141 ret = mkdir (dirname,0775);
143 ret = mkdir (dirname,0775);
145 #error Unknown platform
155 int len = (int)strlen(newdir);
160 buffer = (char*)malloc(len+1);
163 minizip_printf("Error allocating memory\n");
164 return UNZ_INTERNALERROR;
166 strcpy(buffer,newdir);
168 if (buffer[len-1] == '/') {
169 buffer[len-1] = '\0';
171 if (mymkdir(buffer) == 0)
182 while(*p && *p != '\\' && *p != '/')
186 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
188 minizip_printf("couldn't create directory %s\n",buffer);
200 static void do_banner()
203 minizip_printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
204 minizip_printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
208 static void do_help()
210 minizip_printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
211 " -e Extract without pathname (junk paths)\n" \
212 " -x Extract with pathname\n" \
215 " -d directory to extract into\n" \
216 " -o overwrite files without prompting\n" \
217 " -p extract crypted file using password\n\n");
220 void Display64BitsSize(ZPOS64_T n, int size_char)
222 /* to avoid compatibility problem , we do here the conversion */
228 number[offset]=(char)((n%10)+'0');
229 if (number[offset] != '0')
237 int size_display_string = 19-pos_string;
238 while (size_char > size_display_string)
245 minizip_printf("%s",&number[pos_string]);
252 unz_global_info64 gi;
255 err = unzGetGlobalInfo64(uf,&gi);
257 minizip_printf("error %d with zipfile in unzGetGlobalInfo \n",err);
258 minizip_printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
259 minizip_printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
260 for (i=0;i<gi.number_entry;i++)
262 char filename_inzip[256];
263 unz_file_info64 file_info;
265 const char *string_method;
267 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
270 minizip_printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
273 if (file_info.uncompressed_size>0)
274 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
276 /* display a '*' if the file is crypted */
277 if ((file_info.flag & 1) != 0)
280 if (file_info.compression_method==0)
281 string_method="Stored";
283 if (file_info.compression_method==Z_DEFLATED)
285 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
287 string_method="Defl:N";
289 string_method="Defl:X";
290 else if ((iLevel==2) || (iLevel==3))
291 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
294 if (file_info.compression_method==Z_BZIP2ED)
296 string_method="BZip2 ";
299 string_method="Unkn. ";
301 Display64BitsSize(file_info.uncompressed_size,7);
302 minizip_printf(" %6s%c",string_method,charCrypt);
303 Display64BitsSize(file_info.compressed_size,7);
304 minizip_printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
306 (uLong)file_info.tmu_date.tm_mon + 1,
307 (uLong)file_info.tmu_date.tm_mday,
308 (uLong)file_info.tmu_date.tm_year % 100,
309 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
310 (uLong)file_info.crc,filename_inzip);
311 if ((i+1)<gi.number_entry)
313 err = unzGoToNextFile(uf);
316 minizip_printf("error %d with zipfile in unzGoToNextFile\n",err);
326 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
328 const int* popt_extract_without_path;
330 const char* password;
332 char filename_inzip[256];
333 char* filename_withoutpath;
340 unz_file_info64 file_info;
342 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
346 minizip_printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
350 size_buf = WRITEBUFFERSIZE;
351 buf = (void*)malloc(size_buf);
354 minizip_printf("Error allocating memory\n");
355 return UNZ_INTERNALERROR;
358 p = filename_withoutpath = filename_inzip;
361 if (((*p)=='/') || ((*p)=='\\'))
362 filename_withoutpath = p+1;
366 if ((*filename_withoutpath)=='\0')
368 if ((*popt_extract_without_path)==0)
370 minizip_printf("creating directory: %s\n",filename_inzip);
371 mymkdir(filename_inzip);
376 const char* write_filename;
379 if ((*popt_extract_without_path)==0)
380 write_filename = filename_inzip;
382 write_filename = filename_withoutpath;
384 err = unzOpenCurrentFilePassword(uf,password);
387 minizip_printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
390 if (((*popt_overwrite)==0) && (err==UNZ_OK))
394 ftestexist = FOPEN_FUNC(write_filename,"rb");
395 if (ftestexist!=NULL)
403 minizip_printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
404 ret = scanf("%1s",answer);
410 if ((rep>='a') && (rep<='z'))
413 while ((rep!='Y') && (rep!='N') && (rep!='A'));
423 if ((skip==0) && (err==UNZ_OK))
425 fout=FOPEN_FUNC(write_filename,"wb");
426 /* some zipfile don't contain directory alone before file */
427 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
428 (filename_withoutpath!=(char*)filename_inzip))
430 char c=*(filename_withoutpath-1);
431 *(filename_withoutpath-1)='\0';
432 makedir(write_filename);
433 *(filename_withoutpath-1)=c;
434 fout=FOPEN_FUNC(write_filename,"wb");
439 minizip_printf("error opening %s\n",write_filename);
445 minizip_printf(" extracting: %s\n",write_filename);
449 err = unzReadCurrentFile(uf,buf,size_buf);
452 minizip_printf("error %d with zipfile in unzReadCurrentFile\n",err);
456 if (fwrite(buf,err,1,fout)!=1)
458 minizip_printf("error in writing extracted file\n");
468 change_file_date(write_filename,file_info.dosDate,
474 err = unzCloseCurrentFile (uf);
477 minizip_printf("error %d with zipfile in unzCloseCurrentFile\n",err);
481 unzCloseCurrentFile(uf); /* don't lose the error */
489 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
491 int opt_extract_without_path;
493 const char* password;
496 unz_global_info64 gi;
500 err = unzGetGlobalInfo64(uf,&gi);
502 minizip_printf("error %d with zipfile in unzGetGlobalInfo \n",err);
504 for (i=0;i<gi.number_entry;i++)
506 if (do_extract_currentfile(uf,&opt_extract_without_path,
511 if ((i+1)<gi.number_entry)
513 err = unzGoToNextFile(uf);
516 minizip_printf("error %d with zipfile in unzGoToNextFile\n",err);
525 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
527 const char* filename;
528 int opt_extract_without_path;
530 const char* password;
533 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
535 minizip_printf("file %s not found in the zipfile\n",filename);
539 if (do_extract_currentfile(uf,&opt_extract_without_path,
548 int miniunz(argc,argv)
552 const char *zipfilename=NULL;
553 const char *filename_to_extract=NULL;
554 const char *password=NULL;
555 char filename_try[MAXFILENAME+16] = "";
559 int opt_do_extract=1;
560 int opt_do_extract_withoutpath=0;
562 int opt_extractdir=0;
563 const char *dirname=NULL;
578 const char *p=argv[i]+1;
583 if ((c=='l') || (c=='L'))
585 if ((c=='v') || (c=='V'))
587 if ((c=='x') || (c=='X'))
589 if ((c=='e') || (c=='E'))
590 opt_do_extract = opt_do_extract_withoutpath = 1;
591 if ((c=='o') || (c=='O'))
593 if ((c=='d') || (c=='D'))
599 if (((c=='p') || (c=='P')) && (i+1<argc))
608 if (zipfilename == NULL)
609 zipfilename = argv[i];
610 else if ((filename_to_extract==NULL) && (!opt_extractdir))
611 filename_to_extract = argv[i] ;
616 if (zipfilename!=NULL)
619 # ifdef USEWIN32IOAPI
620 zlib_filefunc64_def ffunc;
623 strncpy(filename_try, zipfilename,MAXFILENAME-1);
624 /* strncpy doesnt append the trailing NULL, of the string is too long. */
625 filename_try[ MAXFILENAME ] = '\0';
627 # ifdef USEWIN32IOAPI
628 fill_win32_filefunc64A(&ffunc);
629 uf = unzOpen2_64(zipfilename,&ffunc);
631 uf = unzOpen64(zipfilename);
635 strcat(filename_try,".zip");
636 # ifdef USEWIN32IOAPI
637 uf = unzOpen2_64(filename_try,&ffunc);
639 uf = unzOpen64(filename_try);
646 minizip_printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
649 minizip_printf("%s opened\n",filename_try);
652 ret_value = do_list(uf);
653 else if (opt_do_extract==1)
656 if (opt_extractdir && _chdir(dirname))
658 if (opt_extractdir && chdir(dirname))
661 minizip_printf("Error changing into %s, aborting\n", dirname);
662 return -1; /*exit(-1); */
665 if (filename_to_extract == NULL)
666 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
668 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);