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)
60 #define CASESENSITIVITY (0)
61 #define WRITEBUFFERSIZE (8192)
62 #define MAXFILENAME (256)
69 mini unzip, demo of unzip package
72 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
74 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
79 /* change_file_date : change the date/time of a file
80 filename : the filename of the file where date/time must be modified
81 dosdate : the new date at the MSDos format (4 bytes)
82 tmu_date : the SAME new date at the tm_unz format */
83 void change_file_date(filename,dosdate,tmu_date)
90 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
92 hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
93 0,NULL,OPEN_EXISTING,0,NULL);
94 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
95 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
96 LocalFileTimeToFileTime(&ftLocal,&ftm);
97 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
100 #ifdef unix || __APPLE__
103 newdate.tm_sec = tmu_date.tm_sec;
104 newdate.tm_min=tmu_date.tm_min;
105 newdate.tm_hour=tmu_date.tm_hour;
106 newdate.tm_mday=tmu_date.tm_mday;
107 newdate.tm_mon=tmu_date.tm_mon;
108 if (tmu_date.tm_year > 1900)
109 newdate.tm_year=tmu_date.tm_year - 1900;
111 newdate.tm_year=tmu_date.tm_year ;
114 ut.actime=ut.modtime=mktime(&newdate);
121 /* mymkdir and change_file_date are not 100 % portable
122 As I don't know well Unix, I wait feedback for the unix portion */
129 ret = _mkdir(dirname);
131 ret = mkdir (dirname,0775);
133 ret = mkdir (dirname,0775);
143 int len = (int)strlen(newdir);
148 buffer = (char*)malloc(len+1);
151 printf("Error allocating memory\n");
152 return UNZ_INTERNALERROR;
154 strcpy(buffer,newdir);
156 if (buffer[len-1] == '/') {
157 buffer[len-1] = '\0';
159 if (mymkdir(buffer) == 0)
170 while(*p && *p != '\\' && *p != '/')
174 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
176 printf("couldn't create directory %s\n",buffer);
190 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
191 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
196 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
197 " -e Extract without pathname (junk paths)\n" \
198 " -x Extract with pathname\n" \
201 " -d directory to extract into\n" \
202 " -o overwrite files without prompting\n" \
203 " -p extract crypted file using password\n\n");
206 void Display64BitsSize(ZPOS64_T n, int size_char)
208 /* to avoid compatibility problem , we do here the conversion */
214 number[offset]=(char)((n%10)+'0');
215 if (number[offset] != '0')
223 int size_display_string = 19-pos_string;
224 while (size_char > size_display_string)
231 printf("%s",&number[pos_string]);
238 unz_global_info64 gi;
241 err = unzGetGlobalInfo64(uf,&gi);
243 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
244 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
245 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
246 for (i=0;i<gi.number_entry;i++)
248 char filename_inzip[256];
249 unz_file_info64 file_info;
251 const char *string_method;
253 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
256 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
259 if (file_info.uncompressed_size>0)
260 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
262 /* display a '*' if the file is crypted */
263 if ((file_info.flag & 1) != 0)
266 if (file_info.compression_method==0)
267 string_method="Stored";
269 if (file_info.compression_method==Z_DEFLATED)
271 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
273 string_method="Defl:N";
275 string_method="Defl:X";
276 else if ((iLevel==2) || (iLevel==3))
277 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
280 if (file_info.compression_method==Z_BZIP2ED)
282 string_method="BZip2 ";
285 string_method="Unkn. ";
287 Display64BitsSize(file_info.uncompressed_size,7);
288 printf(" %6s%c",string_method,charCrypt);
289 Display64BitsSize(file_info.compressed_size,7);
290 printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
292 (uLong)file_info.tmu_date.tm_mon + 1,
293 (uLong)file_info.tmu_date.tm_mday,
294 (uLong)file_info.tmu_date.tm_year % 100,
295 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
296 (uLong)file_info.crc,filename_inzip);
297 if ((i+1)<gi.number_entry)
299 err = unzGoToNextFile(uf);
302 printf("error %d with zipfile in unzGoToNextFile\n",err);
312 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
314 const int* popt_extract_without_path;
316 const char* password;
318 char filename_inzip[256];
319 char* filename_withoutpath;
326 unz_file_info64 file_info;
328 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
332 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
336 size_buf = WRITEBUFFERSIZE;
337 buf = (void*)malloc(size_buf);
340 printf("Error allocating memory\n");
341 return UNZ_INTERNALERROR;
344 p = filename_withoutpath = filename_inzip;
347 if (((*p)=='/') || ((*p)=='\\'))
348 filename_withoutpath = p+1;
352 if ((*filename_withoutpath)=='\0')
354 if ((*popt_extract_without_path)==0)
356 printf("creating directory: %s\n",filename_inzip);
357 mymkdir(filename_inzip);
362 const char* write_filename;
365 if ((*popt_extract_without_path)==0)
366 write_filename = filename_inzip;
368 write_filename = filename_withoutpath;
370 err = unzOpenCurrentFilePassword(uf,password);
373 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
376 if (((*popt_overwrite)==0) && (err==UNZ_OK))
380 ftestexist = FOPEN_FUNC(write_filename,"rb");
381 if (ftestexist!=NULL)
389 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
390 ret = scanf("%1s",answer);
396 if ((rep>='a') && (rep<='z'))
399 while ((rep!='Y') && (rep!='N') && (rep!='A'));
409 if ((skip==0) && (err==UNZ_OK))
411 fout=FOPEN_FUNC(write_filename,"wb");
412 /* some zipfile don't contain directory alone before file */
413 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
414 (filename_withoutpath!=(char*)filename_inzip))
416 char c=*(filename_withoutpath-1);
417 *(filename_withoutpath-1)='\0';
418 makedir(write_filename);
419 *(filename_withoutpath-1)=c;
420 fout=FOPEN_FUNC(write_filename,"wb");
425 printf("error opening %s\n",write_filename);
431 printf(" extracting: %s\n",write_filename);
435 err = unzReadCurrentFile(uf,buf,size_buf);
438 printf("error %d with zipfile in unzReadCurrentFile\n",err);
442 if (fwrite(buf,err,1,fout)!=1)
444 printf("error in writing extracted file\n");
454 change_file_date(write_filename,file_info.dosDate,
460 err = unzCloseCurrentFile (uf);
463 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
467 unzCloseCurrentFile(uf); /* don't lose the error */
475 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
477 int opt_extract_without_path;
479 const char* password;
482 unz_global_info64 gi;
486 err = unzGetGlobalInfo64(uf,&gi);
488 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
490 for (i=0;i<gi.number_entry;i++)
492 if (do_extract_currentfile(uf,&opt_extract_without_path,
497 if ((i+1)<gi.number_entry)
499 err = unzGoToNextFile(uf);
502 printf("error %d with zipfile in unzGoToNextFile\n",err);
511 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
513 const char* filename;
514 int opt_extract_without_path;
516 const char* password;
519 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
521 printf("file %s not found in the zipfile\n",filename);
525 if (do_extract_currentfile(uf,&opt_extract_without_path,
538 const char *zipfilename=NULL;
539 const char *filename_to_extract=NULL;
540 const char *password=NULL;
541 char filename_try[MAXFILENAME+16] = "";
545 int opt_do_extract=1;
546 int opt_do_extract_withoutpath=0;
548 int opt_extractdir=0;
549 const char *dirname=NULL;
564 const char *p=argv[i]+1;
569 if ((c=='l') || (c=='L'))
571 if ((c=='v') || (c=='V'))
573 if ((c=='x') || (c=='X'))
575 if ((c=='e') || (c=='E'))
576 opt_do_extract = opt_do_extract_withoutpath = 1;
577 if ((c=='o') || (c=='O'))
579 if ((c=='d') || (c=='D'))
585 if (((c=='p') || (c=='P')) && (i+1<argc))
594 if (zipfilename == NULL)
595 zipfilename = argv[i];
596 else if ((filename_to_extract==NULL) && (!opt_extractdir))
597 filename_to_extract = argv[i] ;
602 if (zipfilename!=NULL)
605 # ifdef USEWIN32IOAPI
606 zlib_filefunc64_def ffunc;
609 strncpy(filename_try, zipfilename,MAXFILENAME-1);
610 /* strncpy doesnt append the trailing NULL, of the string is too long. */
611 filename_try[ MAXFILENAME ] = '\0';
613 # ifdef USEWIN32IOAPI
614 fill_win32_filefunc64A(&ffunc);
615 uf = unzOpen2_64(zipfilename,&ffunc);
617 uf = unzOpen64(zipfilename);
621 strcat(filename_try,".zip");
622 # ifdef USEWIN32IOAPI
623 uf = unzOpen2_64(filename_try,&ffunc);
625 uf = unzOpen64(filename_try);
632 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
635 printf("%s opened\n",filename_try);
638 ret_value = do_list(uf);
639 else if (opt_do_extract==1)
642 if (opt_extractdir && _chdir(dirname))
644 if (opt_extractdir && chdir(dirname))
647 printf("Error changing into %s, aborting\n", dirname);
651 if (filename_to_extract == NULL)
652 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
654 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);