18 #define CASESENSITIVITY (0)
19 #define WRITEBUFFERSIZE (8192)
20 #define MAXFILENAME (256)
27 mini unzip, demo of unzip package
30 Usage : miniunz [-exvlo] file.zip [file_to_extract]
32 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
37 /* change_file_date : change the date/time of a file
38 filename : the filename of the file where date/time must be modified
39 dosdate : the new date at the MSDos format (4 bytes)
40 tmu_date : the SAME new date at the tm_unz format */
41 void change_file_date(filename,dosdate,tmu_date)
48 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
50 hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,
51 0,NULL,OPEN_EXISTING,0,NULL);
52 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
53 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
54 LocalFileTimeToFileTime(&ftLocal,&ftm);
55 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
61 newdate.tm_sec = tmu_date.tm_sec;
62 newdate.tm_min=tmu_date.tm_min;
63 newdate.tm_hour=tmu_date.tm_hour;
64 newdate.tm_mday=tmu_date.tm_mday;
65 newdate.tm_mon=tmu_date.tm_mon;
66 if (tmu_date.tm_year > 1900)
67 newdate.tm_year=tmu_date.tm_year - 1900;
69 newdate.tm_year=tmu_date.tm_year ;
72 ut.actime=ut.modtime=mktime(&newdate);
79 /* mymkdir and change_file_date are not 100 % portable
80 As I don't know well Unix, I wait feedback for the unix portion */
90 ret = mkdir (dirname,0775);
101 int len = (int)strlen(newdir);
106 buffer = (char*)malloc(len+1);
107 strcpy(buffer,newdir);
109 if (buffer[len-1] == '/') {
110 buffer[len-1] = '\0';
112 if (mymkdir(buffer) == 0)
123 while(*p && *p != '\\' && *p != '/')
127 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
129 printf("couldn't create directory %s\n",buffer);
143 printf("MiniUnz 1.00, demo of zLib + Unz package written by Gilles Vollant\n");
144 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
149 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.]\n\n" \
150 " -e Extract without pathname (junk paths)\n" \
151 " -x Extract with pathname\n" \
154 " -o overwrite files without prompting\n" \
155 " -p extract crypted file using password\n\n");
166 err = unzGetGlobalInfo (uf,&gi);
168 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
169 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
170 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
171 for (i=0;i<gi.number_entry;i++)
173 char filename_inzip[256];
174 unz_file_info file_info;
176 const char *string_method;
178 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
181 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
184 if (file_info.uncompressed_size>0)
185 ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
187 /* display a '*' if the file is crypted */
188 if ((file_info.flag & 1) != 0)
191 if (file_info.compression_method==0)
192 string_method="Stored";
194 if (file_info.compression_method==Z_DEFLATED)
196 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
198 string_method="Defl:N";
200 string_method="Defl:X";
201 else if ((iLevel==2) || (iLevel==3))
202 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
205 string_method="Unkn. ";
207 printf("%7lu %6s%c%7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
208 file_info.uncompressed_size,string_method,
210 file_info.compressed_size,
212 (uLong)file_info.tmu_date.tm_mon + 1,
213 (uLong)file_info.tmu_date.tm_mday,
214 (uLong)file_info.tmu_date.tm_year % 100,
215 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
216 (uLong)file_info.crc,filename_inzip);
217 if ((i+1)<gi.number_entry)
219 err = unzGoToNextFile(uf);
222 printf("error %d with zipfile in unzGoToNextFile\n",err);
232 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
234 const int* popt_extract_without_path;
236 const char* password;
238 char filename_inzip[256];
239 char* filename_withoutpath;
246 unz_file_info file_info;
248 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
252 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
256 size_buf = WRITEBUFFERSIZE;
257 buf = (void*)malloc(size_buf);
260 printf("Error allocating memory\n");
261 return UNZ_INTERNALERROR;
264 p = filename_withoutpath = filename_inzip;
267 if (((*p)=='/') || ((*p)=='\\'))
268 filename_withoutpath = p+1;
272 if ((*filename_withoutpath)=='\0')
274 if ((*popt_extract_without_path)==0)
276 printf("creating directory: %s\n",filename_inzip);
277 mymkdir(filename_inzip);
282 const char* write_filename;
285 if ((*popt_extract_without_path)==0)
286 write_filename = filename_inzip;
288 write_filename = filename_withoutpath;
290 err = unzOpenCurrentFilePassword(uf,password);
293 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
296 if (((*popt_overwrite)==0) && (err==UNZ_OK))
300 ftestexist = fopen(write_filename,"rb");
301 if (ftestexist!=NULL)
307 printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
310 if ((rep>='a') && (rep<='z'))
313 while ((rep!='Y') && (rep!='N') && (rep!='A'));
323 if ((skip==0) && (err==UNZ_OK))
325 fout=fopen(write_filename,"wb");
327 /* some zipfile don't contain directory alone before file */
328 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
329 (filename_withoutpath!=(char*)filename_inzip))
331 char c=*(filename_withoutpath-1);
332 *(filename_withoutpath-1)='\0';
333 makedir(write_filename);
334 *(filename_withoutpath-1)=c;
335 fout=fopen(write_filename,"wb");
340 printf("error opening %s\n",write_filename);
346 printf(" extracting: %s\n",write_filename);
350 err = unzReadCurrentFile(uf,buf,size_buf);
353 printf("error %d with zipfile in unzReadCurrentFile\n",err);
357 if (fwrite(buf,err,1,fout)!=1)
359 printf("error in writing extracted file\n");
369 change_file_date(write_filename,file_info.dosDate,
375 err = unzCloseCurrentFile (uf);
378 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
382 unzCloseCurrentFile(uf); /* don't lose the error */
390 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
392 int opt_extract_without_path;
394 const char* password;
401 err = unzGetGlobalInfo (uf,&gi);
403 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
405 for (i=0;i<gi.number_entry;i++)
407 if (do_extract_currentfile(uf,&opt_extract_without_path,
412 if ((i+1)<gi.number_entry)
414 err = unzGoToNextFile(uf);
417 printf("error %d with zipfile in unzGoToNextFile\n",err);
426 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
428 const char* filename;
429 int opt_extract_without_path;
431 const char* password;
434 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
436 printf("file %s not found in the zipfile\n",filename);
440 if (do_extract_currentfile(uf,&opt_extract_without_path,
453 const char *zipfilename=NULL;
454 const char *filename_to_extract=NULL;
455 const char *password=NULL;
456 char filename_try[MAXFILENAME+16] = "";
459 int opt_do_extract=1;
460 int opt_do_extract_withoutpath=0;
476 const char *p=argv[i]+1;
481 if ((c=='l') || (c=='L'))
483 if ((c=='v') || (c=='V'))
485 if ((c=='x') || (c=='X'))
487 if ((c=='e') || (c=='E'))
488 opt_do_extract = opt_do_extract_withoutpath = 1;
489 if ((c=='o') || (c=='O'))
491 if (((c=='p') || (c=='P')) && (i+1<argc))
500 if (zipfilename == NULL)
501 zipfilename = argv[i];
502 else if (filename_to_extract==NULL)
503 filename_to_extract = argv[i] ;
508 if (zipfilename!=NULL)
511 # ifdef USEWIN32IOAPI
512 zlib_filefunc_def ffunc;
515 strncpy(filename_try, zipfilename,MAXFILENAME-1);
516 /* strncpy doesnt append the trailing NULL, of the string is too long. */
517 filename_try[ MAXFILENAME ] = '\0';
519 # ifdef USEWIN32IOAPI
520 fill_win32_filefunc(&ffunc);
521 uf = unzOpen2(zipfilename,&ffunc);
523 uf = unzOpen(zipfilename);
527 strcat(filename_try,".zip");
528 # ifdef USEWIN32IOAPI
529 uf = unzOpen2(filename_try,&ffunc);
531 uf = unzOpen(filename_try);
538 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
541 printf("%s opened\n",filename_try);
545 else if (opt_do_extract==1)
547 if (filename_to_extract == NULL)
548 return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password);
550 return do_extract_onefile(uf,filename_to_extract,
551 opt_do_extract_withoutpath,opt_overwrite,password);
553 unzCloseCurrentFile(uf);