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;
314 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
318 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
322 size_buf = WRITEBUFFERSIZE;
323 buf = (void*)malloc(size_buf);
326 printf("Error allocating memory\n");
327 return UNZ_INTERNALERROR;
330 p = filename_withoutpath = filename_inzip;
333 if (((*p)=='/') || ((*p)=='\\'))
334 filename_withoutpath = p+1;
338 if ((*filename_withoutpath)=='\0')
340 if ((*popt_extract_without_path)==0)
342 printf("creating directory: %s\n",filename_inzip);
343 mymkdir(filename_inzip);
348 const char* write_filename;
351 if ((*popt_extract_without_path)==0)
352 write_filename = filename_inzip;
354 write_filename = filename_withoutpath;
356 err = unzOpenCurrentFilePassword(uf,password);
359 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
362 if (((*popt_overwrite)==0) && (err==UNZ_OK))
366 ftestexist = fopen64(write_filename,"rb");
367 if (ftestexist!=NULL)
375 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
376 ret = scanf("%1s",answer);
382 if ((rep>='a') && (rep<='z'))
385 while ((rep!='Y') && (rep!='N') && (rep!='A'));
395 if ((skip==0) && (err==UNZ_OK))
397 fout=fopen64(write_filename,"wb");
399 /* some zipfile don't contain directory alone before file */
400 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
401 (filename_withoutpath!=(char*)filename_inzip))
403 char c=*(filename_withoutpath-1);
404 *(filename_withoutpath-1)='\0';
405 makedir(write_filename);
406 *(filename_withoutpath-1)=c;
407 fout=fopen64(write_filename,"wb");
412 printf("error opening %s\n",write_filename);
418 printf(" extracting: %s\n",write_filename);
422 err = unzReadCurrentFile(uf,buf,size_buf);
425 printf("error %d with zipfile in unzReadCurrentFile\n",err);
429 if (fwrite(buf,err,1,fout)!=1)
431 printf("error in writing extracted file\n");
441 change_file_date(write_filename,file_info.dosDate,
447 err = unzCloseCurrentFile (uf);
450 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
454 unzCloseCurrentFile(uf); /* don't lose the error */
462 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
464 int opt_extract_without_path;
466 const char* password;
469 unz_global_info64 gi;
472 err = unzGetGlobalInfo64(uf,&gi);
474 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
476 for (i=0;i<gi.number_entry;i++)
478 if (do_extract_currentfile(uf,&opt_extract_without_path,
483 if ((i+1)<gi.number_entry)
485 err = unzGoToNextFile(uf);
488 printf("error %d with zipfile in unzGoToNextFile\n",err);
497 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
499 const char* filename;
500 int opt_extract_without_path;
502 const char* password;
504 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
506 printf("file %s not found in the zipfile\n",filename);
510 if (do_extract_currentfile(uf,&opt_extract_without_path,
523 const char *zipfilename=NULL;
524 const char *filename_to_extract=NULL;
525 const char *password=NULL;
526 char filename_try[MAXFILENAME+16] = "";
530 int opt_do_extract=1;
531 int opt_do_extract_withoutpath=0;
533 int opt_extractdir=0;
534 const char *dirname=NULL;
549 const char *p=argv[i]+1;
554 if ((c=='l') || (c=='L'))
556 if ((c=='v') || (c=='V'))
558 if ((c=='x') || (c=='X'))
560 if ((c=='e') || (c=='E'))
561 opt_do_extract = opt_do_extract_withoutpath = 1;
562 if ((c=='o') || (c=='O'))
564 if ((c=='d') || (c=='D'))
570 if (((c=='p') || (c=='P')) && (i+1<argc))
579 if (zipfilename == NULL)
580 zipfilename = argv[i];
581 else if ((filename_to_extract==NULL) && (!opt_extractdir))
582 filename_to_extract = argv[i] ;
587 if (zipfilename!=NULL)
590 # ifdef USEWIN32IOAPI
591 zlib_filefunc64_def ffunc;
594 strncpy(filename_try, zipfilename,MAXFILENAME-1);
595 /* strncpy doesnt append the trailing NULL, of the string is too long. */
596 filename_try[ MAXFILENAME ] = '\0';
598 # ifdef USEWIN32IOAPI
599 fill_win32_filefunc64A(&ffunc);
600 uf = unzOpen2_64(zipfilename,&ffunc);
602 uf = unzOpen64(zipfilename);
606 strcat(filename_try,".zip");
607 # ifdef USEWIN32IOAPI
608 uf = unzOpen2_64(filename_try,&ffunc);
610 uf = unzOpen64(filename_try);
617 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
620 printf("%s opened\n",filename_try);
623 ret_value = do_list(uf);
624 else if (opt_do_extract==1)
627 if (opt_extractdir && _chdir(dirname))
629 if (opt_extractdir && chdir(dirname))
632 printf("Error changing into %s, aborting\n", dirname);
636 if (filename_to_extract == NULL)
637 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
639 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);