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
40 # include <sys/stat.h>
48 #define CASESENSITIVITY (0)
49 #define WRITEBUFFERSIZE (8192)
50 #define MAXFILENAME (256)
57 mini unzip, demo of unzip package
60 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
62 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
67 /* change_file_date : change the date/time of a file
68 filename : the filename of the file where date/time must be modified
69 dosdate : the new date at the MSDos format (4 bytes)
70 tmu_date : the SAME new date at the tm_unz format */
71 void change_file_date(filename,dosdate,tmu_date)
78 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
80 hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
81 0,NULL,OPEN_EXISTING,0,NULL);
82 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
83 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
84 LocalFileTimeToFileTime(&ftLocal,&ftm);
85 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
91 newdate.tm_sec = tmu_date.tm_sec;
92 newdate.tm_min=tmu_date.tm_min;
93 newdate.tm_hour=tmu_date.tm_hour;
94 newdate.tm_mday=tmu_date.tm_mday;
95 newdate.tm_mon=tmu_date.tm_mon;
96 if (tmu_date.tm_year > 1900)
97 newdate.tm_year=tmu_date.tm_year - 1900;
99 newdate.tm_year=tmu_date.tm_year ;
102 ut.actime=ut.modtime=mktime(&newdate);
109 /* mymkdir and change_file_date are not 100 % portable
110 As I don't know well Unix, I wait feedback for the unix portion */
117 ret = _mkdir(dirname);
120 ret = mkdir (dirname,0775);
131 int len = (int)strlen(newdir);
136 buffer = (char*)malloc(len+1);
139 printf("Error allocating memory\n");
140 return UNZ_INTERNALERROR;
142 strcpy(buffer,newdir);
144 if (buffer[len-1] == '/') {
145 buffer[len-1] = '\0';
147 if (mymkdir(buffer) == 0)
158 while(*p && *p != '\\' && *p != '/')
162 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
164 printf("couldn't create directory %s\n",buffer);
178 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
179 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
184 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
185 " -e Extract without pathname (junk paths)\n" \
186 " -x Extract with pathname\n" \
189 " -d directory to extract into\n" \
190 " -o overwrite files without prompting\n" \
191 " -p extract crypted file using password\n\n");
194 void Display64BitsSize(ZPOS64_T n, int size_char)
196 /* to avoid compatibility problem , we do here the conversion */
202 number[offset]=(char)((n%10)+'0');
203 if (number[offset] != '0')
211 int size_display_string = 19-pos_string;
212 while (size_char > size_display_string)
219 printf("%s",&number[pos_string]);
226 unz_global_info64 gi;
229 err = unzGetGlobalInfo64(uf,&gi);
231 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
232 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
233 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
234 for (i=0;i<gi.number_entry;i++)
236 char filename_inzip[256];
237 unz_file_info64 file_info;
239 const char *string_method;
241 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
244 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
247 if (file_info.uncompressed_size>0)
248 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
250 /* display a '*' if the file is crypted */
251 if ((file_info.flag & 1) != 0)
254 if (file_info.compression_method==0)
255 string_method="Stored";
257 if (file_info.compression_method==Z_DEFLATED)
259 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
261 string_method="Defl:N";
263 string_method="Defl:X";
264 else if ((iLevel==2) || (iLevel==3))
265 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
268 if (file_info.compression_method==Z_BZIP2ED)
270 string_method="BZip2 ";
273 string_method="Unkn. ";
275 Display64BitsSize(file_info.uncompressed_size,7);
276 printf(" %6s%c",string_method,charCrypt);
277 Display64BitsSize(file_info.compressed_size,7);
278 printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
280 (uLong)file_info.tmu_date.tm_mon + 1,
281 (uLong)file_info.tmu_date.tm_mday,
282 (uLong)file_info.tmu_date.tm_year % 100,
283 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
284 (uLong)file_info.crc,filename_inzip);
285 if ((i+1)<gi.number_entry)
287 err = unzGoToNextFile(uf);
290 printf("error %d with zipfile in unzGoToNextFile\n",err);
300 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
302 const int* popt_extract_without_path;
304 const char* password;
306 char filename_inzip[256];
307 char* filename_withoutpath;
314 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;
473 err = unzGetGlobalInfo64(uf,&gi);
475 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
477 for (i=0;i<gi.number_entry;i++)
479 if (do_extract_currentfile(uf,&opt_extract_without_path,
484 if ((i+1)<gi.number_entry)
486 err = unzGoToNextFile(uf);
489 printf("error %d with zipfile in unzGoToNextFile\n",err);
498 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
500 const char* filename;
501 int opt_extract_without_path;
503 const char* password;
505 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
507 printf("file %s not found in the zipfile\n",filename);
511 if (do_extract_currentfile(uf,&opt_extract_without_path,
524 const char *zipfilename=NULL;
525 const char *filename_to_extract=NULL;
526 const char *password=NULL;
527 char filename_try[MAXFILENAME+16] = "";
531 int opt_do_extract=1;
532 int opt_do_extract_withoutpath=0;
534 int opt_extractdir=0;
535 const char *dirname=NULL;
550 const char *p=argv[i]+1;
555 if ((c=='l') || (c=='L'))
557 if ((c=='v') || (c=='V'))
559 if ((c=='x') || (c=='X'))
561 if ((c=='e') || (c=='E'))
562 opt_do_extract = opt_do_extract_withoutpath = 1;
563 if ((c=='o') || (c=='O'))
565 if ((c=='d') || (c=='D'))
571 if (((c=='p') || (c=='P')) && (i+1<argc))
580 if (zipfilename == NULL)
581 zipfilename = argv[i];
582 else if ((filename_to_extract==NULL) && (!opt_extractdir))
583 filename_to_extract = argv[i] ;
588 if (zipfilename!=NULL)
591 # ifdef USEWIN32IOAPI
592 zlib_filefunc64_def ffunc;
595 strncpy(filename_try, zipfilename,MAXFILENAME-1);
596 /* strncpy doesnt append the trailing NULL, of the string is too long. */
597 filename_try[ MAXFILENAME ] = '\0';
599 # ifdef USEWIN32IOAPI
600 fill_win32_filefunc64A(&ffunc);
601 uf = unzOpen2_64(zipfilename,&ffunc);
603 uf = unzOpen64(zipfilename);
607 strcat(filename_try,".zip");
608 # ifdef USEWIN32IOAPI
609 uf = unzOpen2_64(filename_try,&ffunc);
611 uf = unzOpen64(filename_try);
618 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
621 printf("%s opened\n",filename_try);
624 ret_value = do_list(uf);
625 else if (opt_do_extract==1)
628 if (opt_extractdir && _chdir(dirname))
630 if (opt_extractdir && chdir(dirname))
633 printf("Error changing into %s, aborting\n", dirname);
637 if (filename_to_extract == NULL)
638 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
640 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);