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 )
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
47 #define CASESENSITIVITY (0)
48 #define WRITEBUFFERSIZE (8192)
49 #define MAXFILENAME (256)
56 mini unzip, demo of unzip package
59 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
61 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
66 /* change_file_date : change the date/time of a file
67 filename : the filename of the file where date/time must be modified
68 dosdate : the new date at the MSDos format (4 bytes)
69 tmu_date : the SAME new date at the tm_unz format */
70 void change_file_date(filename,dosdate,tmu_date)
77 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
79 hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
80 0,NULL,OPEN_EXISTING,0,NULL);
81 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
82 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
83 LocalFileTimeToFileTime(&ftLocal,&ftm);
84 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
90 newdate.tm_sec = tmu_date.tm_sec;
91 newdate.tm_min=tmu_date.tm_min;
92 newdate.tm_hour=tmu_date.tm_hour;
93 newdate.tm_mday=tmu_date.tm_mday;
94 newdate.tm_mon=tmu_date.tm_mon;
95 if (tmu_date.tm_year > 1900)
96 newdate.tm_year=tmu_date.tm_year - 1900;
98 newdate.tm_year=tmu_date.tm_year ;
101 ut.actime=ut.modtime=mktime(&newdate);
108 /* mymkdir and change_file_date are not 100 % portable
109 As I don't know well Unix, I wait feedback for the unix portion */
116 ret = _mkdir(dirname);
119 ret = mkdir (dirname,0775);
130 int len = (int)strlen(newdir);
135 buffer = (char*)malloc(len+1);
138 printf("Error allocating memory\n");
139 return UNZ_INTERNALERROR;
141 strcpy(buffer,newdir);
143 if (buffer[len-1] == '/') {
144 buffer[len-1] = '\0';
146 if (mymkdir(buffer) == 0)
157 while(*p && *p != '\\' && *p != '/')
161 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
163 printf("couldn't create directory %s\n",buffer);
177 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
178 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
183 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
184 " -e Extract without pathname (junk paths)\n" \
185 " -x Extract with pathname\n" \
188 " -d directory to extract into\n" \
189 " -o overwrite files without prompting\n" \
190 " -p extract crypted file using password\n\n");
193 void Display64BitsSize(ZPOS64_T n, int size_char)
195 /* to avoid compatibility problem , we do here the conversion */
201 number[offset]=(char)((n%10)+'0');
202 if (number[offset] != '0')
210 int size_display_string = 19-pos_string;
211 while (size_char > size_display_string)
218 printf("%s",&number[pos_string]);
225 unz_global_info64 gi;
228 err = unzGetGlobalInfo64(uf,&gi);
230 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
231 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
232 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
233 for (i=0;i<gi.number_entry;i++)
235 char filename_inzip[256];
236 unz_file_info64 file_info;
238 const char *string_method;
240 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
243 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
246 if (file_info.uncompressed_size>0)
247 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
249 /* display a '*' if the file is crypted */
250 if ((file_info.flag & 1) != 0)
253 if (file_info.compression_method==0)
254 string_method="Stored";
256 if (file_info.compression_method==Z_DEFLATED)
258 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
260 string_method="Defl:N";
262 string_method="Defl:X";
263 else if ((iLevel==2) || (iLevel==3))
264 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
267 if (file_info.compression_method==Z_BZIP2ED)
269 string_method="BZip2 ";
272 string_method="Unkn. ";
274 Display64BitsSize(file_info.uncompressed_size,7);
275 printf(" %6s%c",string_method,charCrypt);
276 Display64BitsSize(file_info.compressed_size,7);
277 printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
279 (uLong)file_info.tmu_date.tm_mon + 1,
280 (uLong)file_info.tmu_date.tm_mday,
281 (uLong)file_info.tmu_date.tm_year % 100,
282 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
283 (uLong)file_info.crc,filename_inzip);
284 if ((i+1)<gi.number_entry)
286 err = unzGoToNextFile(uf);
289 printf("error %d with zipfile in unzGoToNextFile\n",err);
299 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
301 const int* popt_extract_without_path;
303 const char* password;
305 char filename_inzip[256];
306 char* filename_withoutpath;
313 unz_file_info64 file_info;
315 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
319 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
323 size_buf = WRITEBUFFERSIZE;
324 buf = (void*)malloc(size_buf);
327 printf("Error allocating memory\n");
328 return UNZ_INTERNALERROR;
331 p = filename_withoutpath = filename_inzip;
334 if (((*p)=='/') || ((*p)=='\\'))
335 filename_withoutpath = p+1;
339 if ((*filename_withoutpath)=='\0')
341 if ((*popt_extract_without_path)==0)
343 printf("creating directory: %s\n",filename_inzip);
344 mymkdir(filename_inzip);
349 const char* write_filename;
352 if ((*popt_extract_without_path)==0)
353 write_filename = filename_inzip;
355 write_filename = filename_withoutpath;
357 err = unzOpenCurrentFilePassword(uf,password);
360 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
363 if (((*popt_overwrite)==0) && (err==UNZ_OK))
367 ftestexist = fopen64(write_filename,"rb");
368 if (ftestexist!=NULL)
376 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
377 ret = scanf("%1s",answer);
383 if ((rep>='a') && (rep<='z'))
386 while ((rep!='Y') && (rep!='N') && (rep!='A'));
396 if ((skip==0) && (err==UNZ_OK))
398 fout=fopen64(write_filename,"wb");
400 /* some zipfile don't contain directory alone before file */
401 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
402 (filename_withoutpath!=(char*)filename_inzip))
404 char c=*(filename_withoutpath-1);
405 *(filename_withoutpath-1)='\0';
406 makedir(write_filename);
407 *(filename_withoutpath-1)=c;
408 fout=fopen64(write_filename,"wb");
413 printf("error opening %s\n",write_filename);
419 printf(" extracting: %s\n",write_filename);
423 err = unzReadCurrentFile(uf,buf,size_buf);
426 printf("error %d with zipfile in unzReadCurrentFile\n",err);
430 if (fwrite(buf,err,1,fout)!=1)
432 printf("error in writing extracted file\n");
442 change_file_date(write_filename,file_info.dosDate,
448 err = unzCloseCurrentFile (uf);
451 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
455 unzCloseCurrentFile(uf); /* don't lose the error */
463 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
465 int opt_extract_without_path;
467 const char* password;
470 unz_global_info64 gi;
474 err = unzGetGlobalInfo64(uf,&gi);
476 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
478 for (i=0;i<gi.number_entry;i++)
480 if (do_extract_currentfile(uf,&opt_extract_without_path,
485 if ((i+1)<gi.number_entry)
487 err = unzGoToNextFile(uf);
490 printf("error %d with zipfile in unzGoToNextFile\n",err);
499 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
501 const char* filename;
502 int opt_extract_without_path;
504 const char* password;
507 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
509 printf("file %s not found in the zipfile\n",filename);
513 if (do_extract_currentfile(uf,&opt_extract_without_path,
526 const char *zipfilename=NULL;
527 const char *filename_to_extract=NULL;
528 const char *password=NULL;
529 char filename_try[MAXFILENAME+16] = "";
533 int opt_do_extract=1;
534 int opt_do_extract_withoutpath=0;
536 int opt_extractdir=0;
537 const char *dirname=NULL;
552 const char *p=argv[i]+1;
557 if ((c=='l') || (c=='L'))
559 if ((c=='v') || (c=='V'))
561 if ((c=='x') || (c=='X'))
563 if ((c=='e') || (c=='E'))
564 opt_do_extract = opt_do_extract_withoutpath = 1;
565 if ((c=='o') || (c=='O'))
567 if ((c=='d') || (c=='D'))
573 if (((c=='p') || (c=='P')) && (i+1<argc))
582 if (zipfilename == NULL)
583 zipfilename = argv[i];
584 else if ((filename_to_extract==NULL) && (!opt_extractdir))
585 filename_to_extract = argv[i] ;
590 if (zipfilename!=NULL)
593 # ifdef USEWIN32IOAPI
594 zlib_filefunc64_def ffunc;
597 strncpy(filename_try, zipfilename,MAXFILENAME-1);
598 /* strncpy doesnt append the trailing NULL, of the string is too long. */
599 filename_try[ MAXFILENAME ] = '\0';
601 # ifdef USEWIN32IOAPI
602 fill_win32_filefunc64A(&ffunc);
603 uf = unzOpen2_64(zipfilename,&ffunc);
605 uf = unzOpen64(zipfilename);
609 strcat(filename_try,".zip");
610 # ifdef USEWIN32IOAPI
611 uf = unzOpen2_64(filename_try,&ffunc);
613 uf = unzOpen64(filename_try);
620 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
623 printf("%s opened\n",filename_try);
626 ret_value = do_list(uf);
627 else if (opt_do_extract==1)
630 if (opt_extractdir && _chdir(dirname))
632 if (opt_extractdir && chdir(dirname))
635 printf("Error changing into %s, aborting\n", dirname);
639 if (filename_to_extract == NULL)
640 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
642 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);