18 #define CASESENSITIVITY (0)
19 #define WRITEBUFFERSIZE (8192)
22 mini unzip, demo of unzip package
25 Usage : miniunz [-exvlo] file.zip [file_to_extract]
27 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
32 /* change_file_date : change the date/time of a file
33 filename : the filename of the file where date/time must be modified
34 dosdate : the new date at the MSDos format (4 bytes)
35 tmu_date : the SAME new date at the tm_unz format */
36 void change_file_date(filename,dosdate,tmu_date)
43 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
45 hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,
46 0,NULL,OPEN_EXISTING,0,NULL);
47 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
48 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
49 LocalFileTimeToFileTime(&ftLocal,&ftm);
50 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
56 newdate.tm_sec = tmu_date.tm_sec;
57 newdate.tm_min=tmu_date.tm_min;
58 newdate.tm_hour=tmu_date.tm_hour;
59 newdate.tm_mday=tmu_date.tm_mday;
60 newdate.tm_mon=tmu_date.tm_mon;
61 if (tmu_date.tm_year > 1900)
62 newdate.tm_year=tmu_date.tm_year - 1900;
64 newdate.tm_year=tmu_date.tm_year ;
67 ut.actime=ut.modtime=mktime(&newdate);
74 /* mymkdir and change_file_date are not 100 % portable
75 As I don't know well Unix, I wait feedback for the unix portion */
85 ret = mkdir (dirname,0775);
96 int len = strlen(newdir);
101 buffer = (char*)malloc(len+1);
102 strcpy(buffer,newdir);
104 if (buffer[len-1] == '/') {
105 buffer[len-1] = '\0';
107 if (mymkdir(buffer) == 0)
118 while(*p && *p != '\\' && *p != '/')
122 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
124 printf("couldn't create directory %s\n",buffer);
138 printf("MiniUnz 0.15, demo of zLib + Unz package written by Gilles Vollant\n");
139 printf("more info at http://wwww.winimage/zLibDll/unzip.htm\n\n");
144 printf("Usage : miniunz [-exvlo] file.zip [file_to_extract]\n\n") ;
155 err = unzGetGlobalInfo (uf,&gi);
157 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
158 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
159 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
160 for (i=0;i<gi.number_entry;i++)
162 char filename_inzip[256];
163 unz_file_info file_info;
165 const char *string_method;
166 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
169 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
172 if (file_info.uncompressed_size>0)
173 ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
175 if (file_info.compression_method==0)
176 string_method="Stored";
178 if (file_info.compression_method==Z_DEFLATED)
180 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
182 string_method="Defl:N";
184 string_method="Defl:X";
185 else if ((iLevel==2) || (iLevel==3))
186 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
189 string_method="Unkn. ";
191 printf("%7lu %6s %7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
192 file_info.uncompressed_size,string_method,file_info.compressed_size,
194 (uLong)file_info.tmu_date.tm_mon + 1,
195 (uLong)file_info.tmu_date.tm_mday,
196 (uLong)file_info.tmu_date.tm_year % 100,
197 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
198 (uLong)file_info.crc,filename_inzip);
199 if ((i+1)<gi.number_entry)
201 err = unzGoToNextFile(uf);
204 printf("error %d with zipfile in unzGoToNextFile\n",err);
214 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite)
216 const int* popt_extract_without_path;
219 char filename_inzip[256];
220 char* filename_withoutpath;
227 unz_file_info file_info;
229 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
233 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
237 size_buf = WRITEBUFFERSIZE;
238 buf = (void*)malloc(size_buf);
241 printf("Error allocating memory\n");
242 return UNZ_INTERNALERROR;
245 p = filename_withoutpath = filename_inzip;
248 if (((*p)=='/') || ((*p)=='\\'))
249 filename_withoutpath = p+1;
253 if ((*filename_withoutpath)=='\0')
255 if ((*popt_extract_without_path)==0)
257 printf("creating directory: %s\n",filename_inzip);
258 mymkdir(filename_inzip);
263 const char* write_filename;
266 if ((*popt_extract_without_path)==0)
267 write_filename = filename_inzip;
269 write_filename = filename_withoutpath;
271 err = unzOpenCurrentFile(uf);
274 printf("error %d with zipfile in unzOpenCurrentFile\n",err);
277 if (((*popt_overwrite)==0) && (err==UNZ_OK))
281 ftestexist = fopen(write_filename,"rb");
282 if (ftestexist!=NULL)
288 printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
291 if ((rep>='a') && (rep<='z'))
294 while ((rep!='Y') && (rep!='N') && (rep!='A'));
304 if ((skip==0) && (err==UNZ_OK))
306 fout=fopen(write_filename,"wb");
308 /* some zipfile don't contain directory alone before file */
309 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
310 (filename_withoutpath!=(char*)filename_inzip))
312 char c=*(filename_withoutpath-1);
313 *(filename_withoutpath-1)='\0';
314 makedir(write_filename);
315 *(filename_withoutpath-1)=c;
316 fout=fopen(write_filename,"wb");
321 printf("error opening %s\n",write_filename);
327 printf(" extracting: %s\n",write_filename);
331 err = unzReadCurrentFile(uf,buf,size_buf);
334 printf("error %d with zipfile in unzReadCurrentFile\n",err);
338 if (fwrite(buf,err,1,fout)!=1)
340 printf("error in writing extracted file\n");
348 change_file_date(write_filename,file_info.dosDate,
354 err = unzCloseCurrentFile (uf);
357 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
361 unzCloseCurrentFile(uf); /* don't lose the error */
369 int do_extract(uf,opt_extract_without_path,opt_overwrite)
371 int opt_extract_without_path;
379 err = unzGetGlobalInfo (uf,&gi);
381 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
383 for (i=0;i<gi.number_entry;i++)
385 if (do_extract_currentfile(uf,&opt_extract_without_path,
386 &opt_overwrite) != UNZ_OK)
389 if ((i+1)<gi.number_entry)
391 err = unzGoToNextFile(uf);
394 printf("error %d with zipfile in unzGoToNextFile\n",err);
403 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite)
405 const char* filename;
406 int opt_extract_without_path;
410 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
412 printf("file %s not found in the zipfile\n",filename);
416 if (do_extract_currentfile(uf,&opt_extract_without_path,
417 &opt_overwrite) == UNZ_OK)
428 const char *zipfilename=NULL;
429 const char *filename_to_extract=NULL;
432 int opt_do_extract=1;
433 int opt_do_extract_withoutpath=0;
435 char filename_try[512];
450 const char *p=argv[i]+1;
455 if ((c=='l') || (c=='L'))
457 if ((c=='v') || (c=='V'))
459 if ((c=='x') || (c=='X'))
461 if ((c=='e') || (c=='E'))
462 opt_do_extract = opt_do_extract_withoutpath = 1;
463 if ((c=='o') || (c=='O'))
469 if (zipfilename == NULL)
470 zipfilename = argv[i];
471 else if (filename_to_extract==NULL)
472 filename_to_extract = argv[i] ;
477 if (zipfilename!=NULL)
479 strcpy(filename_try,zipfilename);
480 uf = unzOpen(zipfilename);
483 strcat(filename_try,".zip");
484 uf = unzOpen(filename_try);
490 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
493 printf("%s opened\n",filename_try);
497 else if (opt_do_extract==1)
499 if (filename_to_extract == NULL)
500 return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite);
502 return do_extract_onefile(uf,filename_to_extract,
503 opt_do_extract_withoutpath,opt_overwrite);
505 unzCloseCurrentFile(uf);
507 return 0; /* to avoid warning */