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)
70 mini unzip, demo of unzip package
73 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
75 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
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 */
84 static void change_file_date(filename,dosdate,tmu_date)
91 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
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);
101 #if defined(unix) || defined(__APPLE__)
105 newdate.tm_sec = tmu_date.tm_sec;
106 newdate.tm_min=tmu_date.tm_min;
107 newdate.tm_hour=tmu_date.tm_hour;
108 newdate.tm_mday=tmu_date.tm_mday;
109 newdate.tm_mon=tmu_date.tm_mon;
110 if (tmu_date.tm_year > 1900)
111 newdate.tm_year=tmu_date.tm_year - 1900;
113 newdate.tm_year=tmu_date.tm_year ;
116 ut.actime=ut.modtime=mktime(&newdate);
123 /* mymkdir and change_file_date are not 100 % portable
124 As I don't know well Unix, I wait feedback for the unix portion */
126 static int mymkdir(dirname)
131 ret = _mkdir(dirname);
133 ret = mkdir (dirname,0775);
135 ret = mkdir (dirname,0775);
140 static int makedir (newdir)
145 size_t len = strlen(newdir);
150 buffer = (char*)malloc(len+1);
153 printf("Error allocating memory\n");
154 return UNZ_INTERNALERROR;
156 strcpy(buffer,newdir);
158 if (buffer[len-1] == '/') {
159 buffer[len-1] = '\0';
161 if (mymkdir(buffer) == 0)
172 while(*p && *p != '\\' && *p != '/')
176 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
178 printf("couldn't create directory %s\n",buffer);
190 static void do_banner()
192 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
193 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
196 static void do_help()
198 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
199 " -e Extract without pathname (junk paths)\n" \
200 " -x Extract with pathname\n" \
203 " -d directory to extract into\n" \
204 " -o overwrite files without prompting\n" \
205 " -p extract crypted file using password\n\n");
208 static void Display64BitsSize(ZPOS64_T n, int size_char)
210 /* to avoid compatibility problem , we do here the conversion */
216 number[offset]=(char)((n%10)+'0');
217 if (number[offset] != '0')
225 int size_display_string = 19-pos_string;
226 while (size_char > size_display_string)
233 printf("%s",&number[pos_string]);
236 static int do_list(uf)
240 unz_global_info64 gi;
243 err = unzGetGlobalInfo64(uf,&gi);
245 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
246 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
247 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
248 for (i=0;i<gi.number_entry;i++)
250 char filename_inzip[256];
251 unz_file_info64 file_info;
253 const char *string_method;
255 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
258 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
261 if (file_info.uncompressed_size>0)
262 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
264 /* display a '*' if the file is crypted */
265 if ((file_info.flag & 1) != 0)
268 if (file_info.compression_method==0)
269 string_method="Stored";
271 if (file_info.compression_method==Z_DEFLATED)
273 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
275 string_method="Defl:N";
277 string_method="Defl:X";
278 else if ((iLevel==2) || (iLevel==3))
279 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
282 if (file_info.compression_method==Z_BZIP2ED)
284 string_method="BZip2 ";
287 string_method="Unkn. ";
289 Display64BitsSize(file_info.uncompressed_size,7);
290 printf(" %6s%c",string_method,charCrypt);
291 Display64BitsSize(file_info.compressed_size,7);
292 printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
294 (uLong)file_info.tmu_date.tm_mon + 1,
295 (uLong)file_info.tmu_date.tm_mday,
296 (uLong)file_info.tmu_date.tm_year % 100,
297 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
298 (uLong)file_info.crc,filename_inzip);
299 if ((i+1)<gi.number_entry)
301 err = unzGoToNextFile(uf);
304 printf("error %d with zipfile in unzGoToNextFile\n",err);
314 static int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
316 const int* popt_extract_without_path;
318 const char* password;
320 char filename_inzip[256];
321 char* filename_withoutpath;
328 unz_file_info64 file_info;
329 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
333 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
337 size_buf = WRITEBUFFERSIZE;
338 buf = (void*)malloc(size_buf);
341 printf("Error allocating memory\n");
342 return UNZ_INTERNALERROR;
345 p = filename_withoutpath = filename_inzip;
348 if (((*p)=='/') || ((*p)=='\\'))
349 filename_withoutpath = p+1;
353 if ((*filename_withoutpath)=='\0')
355 if ((*popt_extract_without_path)==0)
357 printf("creating directory: %s\n",filename_inzip);
358 mymkdir(filename_inzip);
363 const char* write_filename;
366 if ((*popt_extract_without_path)==0)
367 write_filename = filename_inzip;
369 write_filename = filename_withoutpath;
371 err = unzOpenCurrentFilePassword(uf,password);
374 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
377 if (((*popt_overwrite)==0) && (err==UNZ_OK))
381 ftestexist = FOPEN_FUNC(write_filename,"rb");
382 if (ftestexist!=NULL)
390 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
391 ret = scanf("%1s",answer);
397 if ((rep>='a') && (rep<='z'))
400 while ((rep!='Y') && (rep!='N') && (rep!='A'));
410 if ((skip==0) && (err==UNZ_OK))
412 fout=FOPEN_FUNC(write_filename,"wb");
413 /* some zipfile don't contain directory alone before file */
414 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
415 (filename_withoutpath!=(char*)filename_inzip))
417 char c=*(filename_withoutpath-1);
418 *(filename_withoutpath-1)='\0';
419 makedir(write_filename);
420 *(filename_withoutpath-1)=c;
421 fout=FOPEN_FUNC(write_filename,"wb");
426 printf("error opening %s\n",write_filename);
432 printf(" extracting: %s\n",write_filename);
436 err = unzReadCurrentFile(uf,buf,size_buf);
439 printf("error %d with zipfile in unzReadCurrentFile\n",err);
443 if (fwrite(buf,(unsigned)err,1,fout)!=1)
445 printf("error in writing extracted file\n");
455 change_file_date(write_filename,file_info.dosDate,
461 err = unzCloseCurrentFile (uf);
464 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
468 unzCloseCurrentFile(uf); /* don't lose the error */
476 static int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
478 int opt_extract_without_path;
480 const char* password;
483 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 static 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;
518 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
520 printf("file %s not found in the zipfile\n",filename);
524 if (do_extract_currentfile(uf,&opt_extract_without_path,
537 const char *zipfilename=NULL;
538 const char *filename_to_extract=NULL;
539 const char *password=NULL;
540 char filename_try[MAXFILENAME+16] = "";
544 int opt_do_extract=1;
545 int opt_do_extract_withoutpath=0;
547 int opt_extractdir=0;
548 const char *dirname=NULL;
563 const char *p=argv[i]+1;
568 if ((c=='l') || (c=='L'))
570 if ((c=='v') || (c=='V'))
572 if ((c=='x') || (c=='X'))
574 if ((c=='e') || (c=='E'))
575 opt_do_extract = opt_do_extract_withoutpath = 1;
576 if ((c=='o') || (c=='O'))
578 if ((c=='d') || (c=='D'))
584 if (((c=='p') || (c=='P')) && (i+1<argc))
593 if (zipfilename == NULL)
594 zipfilename = argv[i];
595 else if ((filename_to_extract==NULL) && (!opt_extractdir))
596 filename_to_extract = argv[i] ;
601 if (zipfilename!=NULL)
604 # ifdef USEWIN32IOAPI
605 zlib_filefunc64_def ffunc;
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';
612 # ifdef USEWIN32IOAPI
613 fill_win32_filefunc64A(&ffunc);
614 uf = unzOpen2_64(zipfilename,&ffunc);
616 uf = unzOpen64(zipfilename);
620 strcat(filename_try,".zip");
621 # ifdef USEWIN32IOAPI
622 uf = unzOpen2_64(filename_try,&ffunc);
624 uf = unzOpen64(filename_try);
631 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
634 printf("%s opened\n",filename_try);
637 ret_value = do_list(uf);
638 else if (opt_do_extract==1)
641 if (opt_extractdir && _chdir(dirname))
643 if (opt_extractdir && chdir(dirname))
646 printf("Error changing into %s, aborting\n", dirname);
650 if (filename_to_extract == NULL)
651 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
653 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);