VirtualBox

source: vbox/trunk/src/libs/zlib-1.2.1/contrib/minizip/miniunz.c@ 27361

最後變更 在這個檔案從27361是 6392,由 vboxsync 提交於 17 年 前

export libpng and zlib so Windows and OS/2 builds cleanly.

  • 屬性 svn:eol-style 設為 native
檔案大小: 14.3 KB
 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <time.h>
5#include <errno.h>
6#include <fcntl.h>
7
8#ifdef unix
9# include <unistd.h>
10# include <utime.h>
11#else
12# include <direct.h>
13# include <io.h>
14#endif
15
16#include "unzip.h"
17
18#define CASESENSITIVITY (0)
19#define WRITEBUFFERSIZE (8192)
20#define MAXFILENAME (256)
21
22#ifdef WIN32
23#define USEWIN32IOAPI
24#include "iowin32.h"
25#endif
26/*
27 mini unzip, demo of unzip package
28
29 usage :
30 Usage : miniunz [-exvlo] file.zip [file_to_extract]
31
32 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
33 if it exists
34*/
35
36
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 */
41void change_file_date(filename,dosdate,tmu_date)
42 const char *filename;
43 uLong dosdate;
44 tm_unz tmu_date;
45{
46#ifdef WIN32
47 HANDLE hFile;
48 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
49
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;
71
72 ut.actime=ut.modtime=mktime(&newdate);
73 utime(filename,&ut);
74#endif
75#endif
76}
77
78
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 */
81
82int mymkdir(dirname)
83 const char* dirname;
84{
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;
94}
95
96int makedir (newdir)
97 char *newdir;
98{
99 char *buffer ;
100 char *p;
101 int len = (int)strlen(newdir);
102
103 if (len <= 0)
104 return 0;
105
106 buffer = (char*)malloc(len+1);
107 strcpy(buffer,newdir);
108
109 if (buffer[len-1] == '/') {
110 buffer[len-1] = '\0';
111 }
112 if (mymkdir(buffer) == 0)
113 {
114 free(buffer);
115 return 1;
116 }
117
118 p = buffer+1;
119 while (1)
120 {
121 char hold;
122
123 while(*p && *p != '\\' && *p != '/')
124 p++;
125 hold = *p;
126 *p = 0;
127 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
128 {
129 printf("couldn't create directory %s\n",buffer);
130 free(buffer);
131 return 0;
132 }
133 if (hold == 0)
134 break;
135 *p++ = hold;
136 }
137 free(buffer);
138 return 1;
139}
140
141void do_banner()
142{
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");
145}
146
147void do_help()
148{
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");
156}
157
158
159int do_list(uf)
160 unzFile uf;
161{
162 uLong i;
163 unz_global_info gi;
164 int err;
165
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++)
172 {
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)
180 {
181 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
182 break;
183 }
184 if (file_info.uncompressed_size>0)
185 ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
186
187 /* display a '*' if the file is crypted */
188 if ((file_info.flag & 1) != 0)
189 charCrypt='*';
190
191 if (file_info.compression_method==0)
192 string_method="Stored";
193 else
194 if (file_info.compression_method==Z_DEFLATED)
195 {
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*/
203 }
204 else
205 string_method="Unkn. ";
206
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)
218 {
219 err = unzGoToNextFile(uf);
220 if (err!=UNZ_OK)
221 {
222 printf("error %d with zipfile in unzGoToNextFile\n",err);
223 break;
224 }
225 }
226 }
227
228 return 0;
229}
230
231
232int 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;
237{
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;
245
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);
249
250 if (err!=UNZ_OK)
251 {
252 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
253 return err;
254 }
255
256 size_buf = WRITEBUFFERSIZE;
257 buf = (void*)malloc(size_buf);
258 if (buf==NULL)
259 {
260 printf("Error allocating memory\n");
261 return UNZ_INTERNALERROR;
262 }
263
264 p = filename_withoutpath = filename_inzip;
265 while ((*p) != '\0')
266 {
267 if (((*p)=='/') || ((*p)=='\\'))
268 filename_withoutpath = p+1;
269 p++;
270 }
271
272 if ((*filename_withoutpath)=='\0')
273 {
274 if ((*popt_extract_without_path)==0)
275 {
276 printf("creating directory: %s\n",filename_inzip);
277 mymkdir(filename_inzip);
278 }
279 }
280 else
281 {
282 const char* write_filename;
283 int skip=0;
284
285 if ((*popt_extract_without_path)==0)
286 write_filename = filename_inzip;
287 else
288 write_filename = filename_withoutpath;
289
290 err = unzOpenCurrentFilePassword(uf,password);
291 if (err!=UNZ_OK)
292 {
293 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
294 }
295
296 if (((*popt_overwrite)==0) && (err==UNZ_OK))
297 {
298 char rep=0;
299 FILE* ftestexist;
300 ftestexist = fopen(write_filename,"rb");
301 if (ftestexist!=NULL)
302 {
303 fclose(ftestexist);
304 do
305 {
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;
312 }
313 while ((rep!='Y') && (rep!='N') && (rep!='A'));
314 }
315
316 if (rep == 'N')
317 skip = 1;
318
319 if (rep == 'A')
320 *popt_overwrite=1;
321 }
322
323 if ((skip==0) && (err==UNZ_OK))
324 {
325 fout=fopen(write_filename,"wb");
326
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))
330 {
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");
336 }
337
338 if (fout==NULL)
339 {
340 printf("error opening %s\n",write_filename);
341 }
342 }
343
344 if (fout!=NULL)
345 {
346 printf(" extracting: %s\n",write_filename);
347
348 do
349 {
350 err = unzReadCurrentFile(uf,buf,size_buf);
351 if (err<0)
352 {
353 printf("error %d with zipfile in unzReadCurrentFile\n",err);
354 break;
355 }
356 if (err>0)
357 if (fwrite(buf,err,1,fout)!=1)
358 {
359 printf("error in writing extracted file\n");
360 err=UNZ_ERRNO;
361 break;
362 }
363 }
364 while (err>0);
365 if (fout)
366 fclose(fout);
367
368 if (err==0)
369 change_file_date(write_filename,file_info.dosDate,
370 file_info.tmu_date);
371 }
372
373 if (err==UNZ_OK)
374 {
375 err = unzCloseCurrentFile (uf);
376 if (err!=UNZ_OK)
377 {
378 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
379 }
380 }
381 else
382 unzCloseCurrentFile(uf); /* don't lose the error */
383 }
384
385 free(buf);
386 return err;
387}
388
389
390int 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;
395{
396 uLong i;
397 unz_global_info gi;
398 int err;
399 FILE* fout=NULL;
400
401 err = unzGetGlobalInfo (uf,&gi);
402 if (err!=UNZ_OK)
403 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
404
405 for (i=0;i<gi.number_entry;i++)
406 {
407 if (do_extract_currentfile(uf,&opt_extract_without_path,
408 &opt_overwrite,
409 password) != UNZ_OK)
410 break;
411
412 if ((i+1)<gi.number_entry)
413 {
414 err = unzGoToNextFile(uf);
415 if (err!=UNZ_OK)
416 {
417 printf("error %d with zipfile in unzGoToNextFile\n",err);
418 break;
419 }
420 }
421 }
422
423 return 0;
424}
425
426int 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;
432{
433 int err = UNZ_OK;
434 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
435 {
436 printf("file %s not found in the zipfile\n",filename);
437 return 2;
438 }
439
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;
446}
447
448
449int main(argc,argv)
450 int argc;
451 char *argv[];
452{
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;
463
464 do_banner();
465 if (argc==1)
466 {
467 do_help();
468 return 0;
469 }
470 else
471 {
472 for (i=1;i<argc;i++)
473 {
474 if ((*argv[i])=='-')
475 {
476 const char *p=argv[i]+1;
477
478 while ((*p)!='\0')
479 {
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))
492 {
493 password=argv[i+1];
494 i++;
495 }
496 }
497 }
498 else
499 {
500 if (zipfilename == NULL)
501 zipfilename = argv[i];
502 else if (filename_to_extract==NULL)
503 filename_to_extract = argv[i] ;
504 }
505 }
506 }
507
508 if (zipfilename!=NULL)
509 {
510
511# ifdef USEWIN32IOAPI
512 zlib_filefunc_def ffunc;
513# endif
514
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';
518
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)
526 {
527 strcat(filename_try,".zip");
528# ifdef USEWIN32IOAPI
529 uf = unzOpen2(filename_try,&ffunc);
530# else
531 uf = unzOpen(filename_try);
532# endif
533 }
534 }
535
536 if (uf==NULL)
537 {
538 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
539 return 1;
540 }
541 printf("%s opened\n",filename_try);
542
543 if (opt_do_list==1)
544 return do_list(uf);
545 else if (opt_do_extract==1)
546 {
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);
552 }
553 unzCloseCurrentFile(uf);
554
555 return 0;
556}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette