Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / zlib / contrib / minizip / miniunz.c
blobc8cf81e3be37f6b457d06fa0dd53640a611e6348
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include <errno.h>
6 #include <fcntl.h>
8 #ifdef unix
9 # include <unistd.h>
10 # include <utime.h>
11 #else
12 # include <direct.h>
13 # include <io.h>
14 #endif
16 #include "unzip.h"
18 #define CASESENSITIVITY (0)
19 #define WRITEBUFFERSIZE (8192)
20 #define MAXFILENAME (256)
22 #ifdef WIN32
23 #define USEWIN32IOAPI
24 #include "iowin32.h"
25 #endif
27 mini unzip, demo of unzip package
29 usage :
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
33 if it exists
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)
42 const char *filename;
43 uLong dosdate;
44 tm_unz tmu_date;
46 #ifdef WIN32
47 HANDLE hFile;
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);
56 CloseHandle(hFile);
57 #else
58 #ifdef unix
59 struct utimbuf ut;
60 struct tm newdate;
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;
68 else
69 newdate.tm_year=tmu_date.tm_year ;
70 newdate.tm_isdst=-1;
72 ut.actime=ut.modtime=mktime(&newdate);
73 utime(filename,&ut);
74 #endif
75 #endif
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 */
82 int mymkdir(dirname)
83 const char* dirname;
85 int ret=0;
86 #ifdef WIN32
87 ret = mkdir(dirname);
88 #else
89 #ifdef unix
90 ret = mkdir (dirname,0775);
91 #endif
92 #endif
93 return ret;
96 int makedir (newdir)
97 char *newdir;
99 char *buffer ;
100 char *p;
101 int len = (int)strlen(newdir);
103 if (len <= 0)
104 return 0;
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)
114 free(buffer);
115 return 1;
118 p = buffer+1;
119 while (1)
121 char hold;
123 while(*p && *p != '\\' && *p != '/')
124 p++;
125 hold = *p;
126 *p = 0;
127 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
129 printf("couldn't create directory %s\n",buffer);
130 free(buffer);
131 return 0;
133 if (hold == 0)
134 break;
135 *p++ = hold;
137 free(buffer);
138 return 1;
141 void do_banner()
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");
147 void do_help()
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" \
152 " -v list files\n" \
153 " -l list files\n" \
154 " -o overwrite files without prompting\n" \
155 " -p extract crypted file using password\n\n");
159 int do_list(uf)
160 unzFile uf;
162 uLong i;
163 unz_global_info gi;
164 int err;
166 err = unzGetGlobalInfo (uf,&gi);
167 if (err!=UNZ_OK)
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;
175 uLong ratio=0;
176 const char *string_method;
177 char charCrypt=' ';
178 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
179 if (err!=UNZ_OK)
181 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
182 break;
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)
189 charCrypt='*';
191 if (file_info.compression_method==0)
192 string_method="Stored";
193 else
194 if (file_info.compression_method==Z_DEFLATED)
196 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
197 if (iLevel==0)
198 string_method="Defl:N";
199 else if (iLevel==1)
200 string_method="Defl:X";
201 else if ((iLevel==2) || (iLevel==3))
202 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
204 else
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,
209 charCrypt,
210 file_info.compressed_size,
211 ratio,
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);
220 if (err!=UNZ_OK)
222 printf("error %d with zipfile in unzGoToNextFile\n",err);
223 break;
228 return 0;
232 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
233 unzFile uf;
234 const int* popt_extract_without_path;
235 int* popt_overwrite;
236 const char* password;
238 char filename_inzip[256];
239 char* filename_withoutpath;
240 char* p;
241 int err=UNZ_OK;
242 FILE *fout=NULL;
243 void* buf;
244 uInt size_buf;
246 unz_file_info file_info;
247 uLong ratio=0;
248 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
250 if (err!=UNZ_OK)
252 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
253 return err;
256 size_buf = WRITEBUFFERSIZE;
257 buf = (void*)malloc(size_buf);
258 if (buf==NULL)
260 printf("Error allocating memory\n");
261 return UNZ_INTERNALERROR;
264 p = filename_withoutpath = filename_inzip;
265 while ((*p) != '\0')
267 if (((*p)=='/') || ((*p)=='\\'))
268 filename_withoutpath = p+1;
269 p++;
272 if ((*filename_withoutpath)=='\0')
274 if ((*popt_extract_without_path)==0)
276 printf("creating directory: %s\n",filename_inzip);
277 mymkdir(filename_inzip);
280 else
282 const char* write_filename;
283 int skip=0;
285 if ((*popt_extract_without_path)==0)
286 write_filename = filename_inzip;
287 else
288 write_filename = filename_withoutpath;
290 err = unzOpenCurrentFilePassword(uf,password);
291 if (err!=UNZ_OK)
293 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
296 if (((*popt_overwrite)==0) && (err==UNZ_OK))
298 char rep=0;
299 FILE* ftestexist;
300 ftestexist = fopen(write_filename,"rb");
301 if (ftestexist!=NULL)
303 fclose(ftestexist);
306 char answer[128];
307 printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
308 scanf("%1s",answer);
309 rep = answer[0] ;
310 if ((rep>='a') && (rep<='z'))
311 rep -= 0x20;
313 while ((rep!='Y') && (rep!='N') && (rep!='A'));
316 if (rep == 'N')
317 skip = 1;
319 if (rep == 'A')
320 *popt_overwrite=1;
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");
338 if (fout==NULL)
340 printf("error opening %s\n",write_filename);
344 if (fout!=NULL)
346 printf(" extracting: %s\n",write_filename);
350 err = unzReadCurrentFile(uf,buf,size_buf);
351 if (err<0)
353 printf("error %d with zipfile in unzReadCurrentFile\n",err);
354 break;
356 if (err>0)
357 if (fwrite(buf,err,1,fout)!=1)
359 printf("error in writing extracted file\n");
360 err=UNZ_ERRNO;
361 break;
364 while (err>0);
365 if (fout)
366 fclose(fout);
368 if (err==0)
369 change_file_date(write_filename,file_info.dosDate,
370 file_info.tmu_date);
373 if (err==UNZ_OK)
375 err = unzCloseCurrentFile (uf);
376 if (err!=UNZ_OK)
378 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
381 else
382 unzCloseCurrentFile(uf); /* don't lose the error */
385 free(buf);
386 return err;
390 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
391 unzFile uf;
392 int opt_extract_without_path;
393 int opt_overwrite;
394 const char* password;
396 uLong i;
397 unz_global_info gi;
398 int err;
399 FILE* fout=NULL;
401 err = unzGetGlobalInfo (uf,&gi);
402 if (err!=UNZ_OK)
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,
408 &opt_overwrite,
409 password) != UNZ_OK)
410 break;
412 if ((i+1)<gi.number_entry)
414 err = unzGoToNextFile(uf);
415 if (err!=UNZ_OK)
417 printf("error %d with zipfile in unzGoToNextFile\n",err);
418 break;
423 return 0;
426 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
427 unzFile uf;
428 const char* filename;
429 int opt_extract_without_path;
430 int opt_overwrite;
431 const char* password;
433 int err = UNZ_OK;
434 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
436 printf("file %s not found in the zipfile\n",filename);
437 return 2;
440 if (do_extract_currentfile(uf,&opt_extract_without_path,
441 &opt_overwrite,
442 password) == UNZ_OK)
443 return 0;
444 else
445 return 1;
449 int main(argc,argv)
450 int argc;
451 char *argv[];
453 const char *zipfilename=NULL;
454 const char *filename_to_extract=NULL;
455 const char *password=NULL;
456 char filename_try[MAXFILENAME+16] = "";
457 int i;
458 int opt_do_list=0;
459 int opt_do_extract=1;
460 int opt_do_extract_withoutpath=0;
461 int opt_overwrite=0;
462 unzFile uf=NULL;
464 do_banner();
465 if (argc==1)
467 do_help();
468 return 0;
470 else
472 for (i=1;i<argc;i++)
474 if ((*argv[i])=='-')
476 const char *p=argv[i]+1;
478 while ((*p)!='\0')
480 char c=*(p++);;
481 if ((c=='l') || (c=='L'))
482 opt_do_list = 1;
483 if ((c=='v') || (c=='V'))
484 opt_do_list = 1;
485 if ((c=='x') || (c=='X'))
486 opt_do_extract = 1;
487 if ((c=='e') || (c=='E'))
488 opt_do_extract = opt_do_extract_withoutpath = 1;
489 if ((c=='o') || (c=='O'))
490 opt_overwrite=1;
491 if (((c=='p') || (c=='P')) && (i+1<argc))
493 password=argv[i+1];
494 i++;
498 else
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;
513 # endif
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);
522 # else
523 uf = unzOpen(zipfilename);
524 # endif
525 if (uf==NULL)
527 strcat(filename_try,".zip");
528 # ifdef USEWIN32IOAPI
529 uf = unzOpen2(filename_try,&ffunc);
530 # else
531 uf = unzOpen(filename_try);
532 # endif
536 if (uf==NULL)
538 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
539 return 1;
541 printf("%s opened\n",filename_try);
543 if (opt_do_list==1)
544 return do_list(uf);
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);
549 else
550 return do_extract_onefile(uf,filename_to_extract,
551 opt_do_extract_withoutpath,opt_overwrite,password);
553 unzCloseCurrentFile(uf);
555 return 0;