1 /* $NetBSD: miniunz.c,v 1.1.1.1 2006/01/14 20:10:58 christos Exp $ */
5 Version 1.01e, February 12th, 2005
7 Copyright (C) 1998-2005 Gilles Vollant
28 #define CASESENSITIVITY (0)
29 #define WRITEBUFFERSIZE (8192)
30 #define MAXFILENAME (256)
37 mini unzip, demo of unzip package
40 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
42 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
47 /* change_file_date : change the date/time of a file
48 filename : the filename of the file where date/time must be modified
49 dosdate : the new date at the MSDos format (4 bytes)
50 tmu_date : the SAME new date at the tm_unz format */
51 void change_file_date(filename
,dosdate
,tmu_date
)
58 FILETIME ftm
,ftLocal
,ftCreate
,ftLastAcc
,ftLastWrite
;
60 hFile
= CreateFile(filename
,GENERIC_READ
| GENERIC_WRITE
,
61 0,NULL
,OPEN_EXISTING
,0,NULL
);
62 GetFileTime(hFile
,&ftCreate
,&ftLastAcc
,&ftLastWrite
);
63 DosDateTimeToFileTime((WORD
)(dosdate
>>16),(WORD
)dosdate
,&ftLocal
);
64 LocalFileTimeToFileTime(&ftLocal
,&ftm
);
65 SetFileTime(hFile
,&ftm
,&ftLastAcc
,&ftm
);
71 newdate
.tm_sec
= tmu_date
.tm_sec
;
72 newdate
.tm_min
=tmu_date
.tm_min
;
73 newdate
.tm_hour
=tmu_date
.tm_hour
;
74 newdate
.tm_mday
=tmu_date
.tm_mday
;
75 newdate
.tm_mon
=tmu_date
.tm_mon
;
76 if (tmu_date
.tm_year
> 1900)
77 newdate
.tm_year
=tmu_date
.tm_year
- 1900;
79 newdate
.tm_year
=tmu_date
.tm_year
;
82 ut
.actime
=ut
.modtime
=mktime(&newdate
);
89 /* mymkdir and change_file_date are not 100 % portable
90 As I don't know well Unix, I wait feedback for the unix portion */
100 ret
= mkdir (dirname
,0775);
111 int len
= (int)strlen(newdir
);
116 buffer
= (char*)malloc(len
+1);
117 strcpy(buffer
,newdir
);
119 if (buffer
[len
-1] == '/') {
120 buffer
[len
-1] = '\0';
122 if (mymkdir(buffer
) == 0)
133 while(*p
&& *p
!= '\\' && *p
!= '/')
137 if ((mymkdir(buffer
) == -1) && (errno
== ENOENT
))
139 printf("couldn't create directory %s\n",buffer
);
153 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
154 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
159 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
160 " -e Extract without pathname (junk paths)\n" \
161 " -x Extract with pathname\n" \
164 " -d directory to extract into\n" \
165 " -o overwrite files without prompting\n" \
166 " -p extract crypted file using password\n\n");
177 err
= unzGetGlobalInfo (uf
,&gi
);
179 printf("error %d with zipfile in unzGetGlobalInfo \n",err
);
180 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
181 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
182 for (i
=0;i
<gi
.number_entry
;i
++)
184 char filename_inzip
[256];
185 unz_file_info file_info
;
187 const char *string_method
;
189 err
= unzGetCurrentFileInfo(uf
,&file_info
,filename_inzip
,sizeof(filename_inzip
),NULL
,0,NULL
,0);
192 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err
);
195 if (file_info
.uncompressed_size
>0)
196 ratio
= (file_info
.compressed_size
*100)/file_info
.uncompressed_size
;
198 /* display a '*' if the file is crypted */
199 if ((file_info
.flag
& 1) != 0)
202 if (file_info
.compression_method
==0)
203 string_method
="Stored";
205 if (file_info
.compression_method
==Z_DEFLATED
)
207 uInt iLevel
=(uInt
)((file_info
.flag
& 0x6)/2);
209 string_method
="Defl:N";
211 string_method
="Defl:X";
212 else if ((iLevel
==2) || (iLevel
==3))
213 string_method
="Defl:F"; /* 2:fast , 3 : extra fast*/
216 string_method
="Unkn. ";
218 printf("%7lu %6s%c%7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
219 file_info
.uncompressed_size
,string_method
,
221 file_info
.compressed_size
,
223 (uLong
)file_info
.tmu_date
.tm_mon
+ 1,
224 (uLong
)file_info
.tmu_date
.tm_mday
,
225 (uLong
)file_info
.tmu_date
.tm_year
% 100,
226 (uLong
)file_info
.tmu_date
.tm_hour
,(uLong
)file_info
.tmu_date
.tm_min
,
227 (uLong
)file_info
.crc
,filename_inzip
);
228 if ((i
+1)<gi
.number_entry
)
230 err
= unzGoToNextFile(uf
);
233 printf("error %d with zipfile in unzGoToNextFile\n",err
);
243 int do_extract_currentfile(uf
,popt_extract_without_path
,popt_overwrite
,password
)
245 const int* popt_extract_without_path
;
247 const char* password
;
249 char filename_inzip
[256];
250 char* filename_withoutpath
;
257 unz_file_info file_info
;
259 err
= unzGetCurrentFileInfo(uf
,&file_info
,filename_inzip
,sizeof(filename_inzip
),NULL
,0,NULL
,0);
263 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err
);
267 size_buf
= WRITEBUFFERSIZE
;
268 buf
= (void*)malloc(size_buf
);
271 printf("Error allocating memory\n");
272 return UNZ_INTERNALERROR
;
275 p
= filename_withoutpath
= filename_inzip
;
278 if (((*p
)=='/') || ((*p
)=='\\'))
279 filename_withoutpath
= p
+1;
283 if ((*filename_withoutpath
)=='\0')
285 if ((*popt_extract_without_path
)==0)
287 printf("creating directory: %s\n",filename_inzip
);
288 mymkdir(filename_inzip
);
293 const char* write_filename
;
296 if ((*popt_extract_without_path
)==0)
297 write_filename
= filename_inzip
;
299 write_filename
= filename_withoutpath
;
301 err
= unzOpenCurrentFilePassword(uf
,password
);
304 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err
);
307 if (((*popt_overwrite
)==0) && (err
==UNZ_OK
))
311 ftestexist
= fopen(write_filename
,"rb");
312 if (ftestexist
!=NULL
)
320 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename
);
321 ret
= scanf("%1s",answer
);
327 if ((rep
>='a') && (rep
<='z'))
330 while ((rep
!='Y') && (rep
!='N') && (rep
!='A'));
340 if ((skip
==0) && (err
==UNZ_OK
))
342 fout
=fopen(write_filename
,"wb");
344 /* some zipfile don't contain directory alone before file */
345 if ((fout
==NULL
) && ((*popt_extract_without_path
)==0) &&
346 (filename_withoutpath
!=(char*)filename_inzip
))
348 char c
=*(filename_withoutpath
-1);
349 *(filename_withoutpath
-1)='\0';
350 makedir(write_filename
);
351 *(filename_withoutpath
-1)=c
;
352 fout
=fopen(write_filename
,"wb");
357 printf("error opening %s\n",write_filename
);
363 printf(" extracting: %s\n",write_filename
);
367 err
= unzReadCurrentFile(uf
,buf
,size_buf
);
370 printf("error %d with zipfile in unzReadCurrentFile\n",err
);
374 if (fwrite(buf
,err
,1,fout
)!=1)
376 printf("error in writing extracted file\n");
386 change_file_date(write_filename
,file_info
.dosDate
,
392 err
= unzCloseCurrentFile (uf
);
395 printf("error %d with zipfile in unzCloseCurrentFile\n",err
);
399 unzCloseCurrentFile(uf
); /* don't lose the error */
407 int do_extract(uf
,opt_extract_without_path
,opt_overwrite
,password
)
409 int opt_extract_without_path
;
411 const char* password
;
418 err
= unzGetGlobalInfo (uf
,&gi
);
420 printf("error %d with zipfile in unzGetGlobalInfo \n",err
);
422 for (i
=0;i
<gi
.number_entry
;i
++)
424 if (do_extract_currentfile(uf
,&opt_extract_without_path
,
429 if ((i
+1)<gi
.number_entry
)
431 err
= unzGoToNextFile(uf
);
434 printf("error %d with zipfile in unzGoToNextFile\n",err
);
443 int do_extract_onefile(uf
,filename
,opt_extract_without_path
,opt_overwrite
,password
)
445 const char* filename
;
446 int opt_extract_without_path
;
448 const char* password
;
451 if (unzLocateFile(uf
,filename
,CASESENSITIVITY
)!=UNZ_OK
)
453 printf("file %s not found in the zipfile\n",filename
);
457 if (do_extract_currentfile(uf
,&opt_extract_without_path
,
470 const char *zipfilename
=NULL
;
471 const char *filename_to_extract
=NULL
;
472 const char *password
=NULL
;
473 char filename_try
[MAXFILENAME
+16] = "";
476 int opt_do_extract
=1;
477 int opt_do_extract_withoutpath
=0;
479 int opt_extractdir
=0;
480 const char *dirname
=NULL
;
495 const char *p
=argv
[i
]+1;
500 if ((c
=='l') || (c
=='L'))
502 if ((c
=='v') || (c
=='V'))
504 if ((c
=='x') || (c
=='X'))
506 if ((c
=='e') || (c
=='E'))
507 opt_do_extract
= opt_do_extract_withoutpath
= 1;
508 if ((c
=='o') || (c
=='O'))
510 if ((c
=='d') || (c
=='D'))
516 if (((c
=='p') || (c
=='P')) && (i
+1<argc
))
525 if (zipfilename
== NULL
)
526 zipfilename
= argv
[i
];
527 else if ((filename_to_extract
==NULL
) && (!opt_extractdir
))
528 filename_to_extract
= argv
[i
] ;
533 if (zipfilename
!=NULL
)
536 # ifdef USEWIN32IOAPI
537 zlib_filefunc_def ffunc
;
540 strncpy(filename_try
, zipfilename
,MAXFILENAME
-1);
541 /* strncpy doesnt append the trailing NULL, of the string is too long. */
542 filename_try
[ MAXFILENAME
] = '\0';
544 # ifdef USEWIN32IOAPI
545 fill_win32_filefunc(&ffunc
);
546 uf
= unzOpen2(zipfilename
,&ffunc
);
548 uf
= unzOpen(zipfilename
);
552 strcat(filename_try
,".zip");
553 # ifdef USEWIN32IOAPI
554 uf
= unzOpen2(filename_try
,&ffunc
);
556 uf
= unzOpen(filename_try
);
563 printf("Cannot open %s or %s.zip\n",zipfilename
,zipfilename
);
566 printf("%s opened\n",filename_try
);
570 else if (opt_do_extract
==1)
572 if (opt_extractdir
&& chdir(dirname
))
574 printf("Error changing into %s, aborting\n", dirname
);
578 if (filename_to_extract
== NULL
)
579 return do_extract(uf
,opt_do_extract_withoutpath
,opt_overwrite
,password
);
581 return do_extract_onefile(uf
,filename_to_extract
,
582 opt_do_extract_withoutpath
,opt_overwrite
,password
);
584 unzCloseCurrentFile(uf
);