summaryrefslogtreecommitdiff
path: root/contrib/minizip/miniunz.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/minizip/miniunz.c')
-rw-r--r--contrib/minizip/miniunz.c1099
1 files changed, 556 insertions, 543 deletions
diff --git a/contrib/minizip/miniunz.c b/contrib/minizip/miniunz.c
index 938d4ef..ea23e40 100644
--- a/contrib/minizip/miniunz.c
+++ b/contrib/minizip/miniunz.c
@@ -1,543 +1,556 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <string.h> 3#include <string.h>
4#include <time.h> 4#include <time.h>
5#include <errno.h> 5#include <errno.h>
6#include <fcntl.h> 6#include <fcntl.h>
7 7
8#ifdef unix 8#ifdef unix
9# include <unistd.h> 9# include <unistd.h>
10# include <utime.h> 10# include <utime.h>
11#else 11#else
12# include <direct.h> 12# include <direct.h>
13# include <io.h> 13# include <io.h>
14#endif 14#endif
15 15
16#include "unzip.h" 16#include "unzip.h"
17 17
18#define CASESENSITIVITY (0) 18#define CASESENSITIVITY (0)
19#define WRITEBUFFERSIZE (8192) 19#define WRITEBUFFERSIZE (8192)
20#define MAXFILENAME (256) 20#define MAXFILENAME (256)
21 21
22#ifdef WIN32 22#ifdef WIN32
23#define USEWIN32IOAPI 23#define USEWIN32IOAPI
24#include "iowin32.h" 24#include "iowin32.h"
25#endif 25#endif
26/* 26/*
27 mini unzip, demo of unzip package 27 mini unzip, demo of unzip package
28 28
29 usage : 29 usage :
30 Usage : miniunz [-exvlo] file.zip [file_to_extract] 30 Usage : miniunz [-exvlo] file.zip [file_to_extract]
31 31
32 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT 32 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
33 if it exists 33 if it exists
34*/ 34*/
35 35
36 36
37/* change_file_date : change the date/time of a file 37/* change_file_date : change the date/time of a file
38 filename : the filename of the file where date/time must be modified 38 filename : the filename of the file where date/time must be modified
39 dosdate : the new date at the MSDos format (4 bytes) 39 dosdate : the new date at the MSDos format (4 bytes)
40 tmu_date : the SAME new date at the tm_unz format */ 40 tmu_date : the SAME new date at the tm_unz format */
41void change_file_date(filename,dosdate,tmu_date) 41void change_file_date(filename,dosdate,tmu_date)
42 const char *filename; 42 const char *filename;
43 uLong dosdate; 43 uLong dosdate;
44 tm_unz tmu_date; 44 tm_unz tmu_date;
45{ 45{
46#ifdef WIN32 46#ifdef WIN32
47 HANDLE hFile; 47 HANDLE hFile;
48 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite; 48 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
49 49
50 hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE, 50 hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,
51 0,NULL,OPEN_EXISTING,0,NULL); 51 0,NULL,OPEN_EXISTING,0,NULL);
52 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite); 52 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
53 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal); 53 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
54 LocalFileTimeToFileTime(&ftLocal,&ftm); 54 LocalFileTimeToFileTime(&ftLocal,&ftm);
55 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm); 55 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
56 CloseHandle(hFile); 56 CloseHandle(hFile);
57#else 57#else
58#ifdef unix 58#ifdef unix
59 struct utimbuf ut; 59 struct utimbuf ut;
60 struct tm newdate; 60 struct tm newdate;
61 newdate.tm_sec = tmu_date.tm_sec; 61 newdate.tm_sec = tmu_date.tm_sec;
62 newdate.tm_min=tmu_date.tm_min; 62 newdate.tm_min=tmu_date.tm_min;
63 newdate.tm_hour=tmu_date.tm_hour; 63 newdate.tm_hour=tmu_date.tm_hour;
64 newdate.tm_mday=tmu_date.tm_mday; 64 newdate.tm_mday=tmu_date.tm_mday;
65 newdate.tm_mon=tmu_date.tm_mon; 65 newdate.tm_mon=tmu_date.tm_mon;
66 if (tmu_date.tm_year > 1900) 66 if (tmu_date.tm_year > 1900)
67 newdate.tm_year=tmu_date.tm_year - 1900; 67 newdate.tm_year=tmu_date.tm_year - 1900;
68 else 68 else
69 newdate.tm_year=tmu_date.tm_year ; 69 newdate.tm_year=tmu_date.tm_year ;
70 newdate.tm_isdst=-1; 70 newdate.tm_isdst=-1;
71 71
72 ut.actime=ut.modtime=mktime(&newdate); 72 ut.actime=ut.modtime=mktime(&newdate);
73 utime(filename,&ut); 73 utime(filename,&ut);
74#endif 74#endif
75#endif 75#endif
76} 76}
77 77
78 78
79/* mymkdir and change_file_date are not 100 % portable 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 */ 80 As I don't know well Unix, I wait feedback for the unix portion */
81 81
82int mymkdir(dirname) 82int mymkdir(dirname)
83 const char* dirname; 83 const char* dirname;
84{ 84{
85 int ret=0; 85 int ret=0;
86#ifdef WIN32 86#ifdef WIN32
87 ret = mkdir(dirname); 87 ret = mkdir(dirname);
88#else 88#else
89#ifdef unix 89#ifdef unix
90 ret = mkdir (dirname,0775); 90 ret = mkdir (dirname,0775);
91#endif 91#endif
92#endif 92#endif
93 return ret; 93 return ret;
94} 94}
95 95
96int makedir (newdir) 96int makedir (newdir)
97 char *newdir; 97 char *newdir;
98{ 98{
99 char *buffer ; 99 char *buffer ;
100 char *p; 100 char *p;
101 int len = (int)strlen(newdir); 101 int len = (int)strlen(newdir);
102 102
103 if (len <= 0) 103 if (len <= 0)
104 return 0; 104 return 0;
105 105
106 buffer = (char*)malloc(len+1); 106 buffer = (char*)malloc(len+1);
107 strcpy(buffer,newdir); 107 strcpy(buffer,newdir);
108 108
109 if (buffer[len-1] == '/') { 109 if (buffer[len-1] == '/') {
110 buffer[len-1] = '\0'; 110 buffer[len-1] = '\0';
111 } 111 }
112 if (mymkdir(buffer) == 0) 112 if (mymkdir(buffer) == 0)
113 { 113 {
114 free(buffer); 114 free(buffer);
115 return 1; 115 return 1;
116 } 116 }
117 117
118 p = buffer+1; 118 p = buffer+1;
119 while (1) 119 while (1)
120 { 120 {
121 char hold; 121 char hold;
122 122
123 while(*p && *p != '\\' && *p != '/') 123 while(*p && *p != '\\' && *p != '/')
124 p++; 124 p++;
125 hold = *p; 125 hold = *p;
126 *p = 0; 126 *p = 0;
127 if ((mymkdir(buffer) == -1) && (errno == ENOENT)) 127 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
128 { 128 {
129 printf("couldn't create directory %s\n",buffer); 129 printf("couldn't create directory %s\n",buffer);
130 free(buffer); 130 free(buffer);
131 return 0; 131 return 0;
132 } 132 }
133 if (hold == 0) 133 if (hold == 0)
134 break; 134 break;
135 *p++ = hold; 135 *p++ = hold;
136 } 136 }
137 free(buffer); 137 free(buffer);
138 return 1; 138 return 1;
139} 139}
140 140
141void do_banner() 141void do_banner()
142{ 142{
143 printf("MiniUnz 0.15, demo of zLib + Unz package written by Gilles Vollant\n"); 143 printf("MiniUnz 0.22, demo of zLib + Unz package written by Gilles Vollant\n");
144 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); 144 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
145} 145}
146 146
147void do_help() 147void do_help()
148{ 148{
149 printf("Usage : miniunz [-exvlo] file.zip [file_to_extract]\n\n") ; 149 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.]\n\n" \
150} 150 " -e Extract without pathname (junk paths)\n" \
151 151 " -x Extract with pathname\n" \
152 152 " -v list files\n" \
153int do_list(uf) 153 " -l list files\n" \
154 unzFile uf; 154 " -o overwrite files without prompting\n" \
155{ 155 " -p extract crypted file using password\n\n");
156 uLong i; 156}
157 unz_global_info gi; 157
158 int err; 158
159 159int do_list(uf)
160 err = unzGetGlobalInfo (uf,&gi); 160 unzFile uf;
161 if (err!=UNZ_OK) 161{
162 printf("error %d with zipfile in unzGetGlobalInfo \n",err); 162 uLong i;
163 printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); 163 unz_global_info gi;
164 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); 164 int err;
165 for (i=0;i<gi.number_entry;i++) 165
166 { 166 err = unzGetGlobalInfo (uf,&gi);
167 char filename_inzip[256]; 167 if (err!=UNZ_OK)
168 unz_file_info file_info; 168 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
169 uLong ratio=0; 169 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
170 const char *string_method; 170 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
171 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); 171 for (i=0;i<gi.number_entry;i++)
172 if (err!=UNZ_OK) 172 {
173 { 173 char filename_inzip[256];
174 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); 174 unz_file_info file_info;
175 break; 175 uLong ratio=0;
176 } 176 const char *string_method;
177 if (file_info.uncompressed_size>0) 177 char charCrypt=' ';
178 ratio = (file_info.compressed_size*100)/file_info.uncompressed_size; 178 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
179 179 if (err!=UNZ_OK)
180 if (file_info.compression_method==0) 180 {
181 string_method="Stored"; 181 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
182 else 182 break;
183 if (file_info.compression_method==Z_DEFLATED) 183 }
184 { 184 if (file_info.uncompressed_size>0)
185 uInt iLevel=(uInt)((file_info.flag & 0x6)/2); 185 ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
186 if (iLevel==0) 186
187 string_method="Defl:N"; 187 /* display a '*' if the file is crypted */
188 else if (iLevel==1) 188 if ((file_info.flag & 1) != 0)
189 string_method="Defl:X"; 189 charCrypt='*';
190 else if ((iLevel==2) || (iLevel==3)) 190
191 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ 191 if (file_info.compression_method==0)
192 } 192 string_method="Stored";
193 else 193 else
194 string_method="Unkn. "; 194 if (file_info.compression_method==Z_DEFLATED)
195 195 {
196 printf("%7lu %6s %7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", 196 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
197 file_info.uncompressed_size,string_method,file_info.compressed_size, 197 if (iLevel==0)
198 ratio, 198 string_method="Defl:N";
199 (uLong)file_info.tmu_date.tm_mon + 1, 199 else if (iLevel==1)
200 (uLong)file_info.tmu_date.tm_mday, 200 string_method="Defl:X";
201 (uLong)file_info.tmu_date.tm_year % 100, 201 else if ((iLevel==2) || (iLevel==3))
202 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min, 202 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
203 (uLong)file_info.crc,filename_inzip); 203 }
204 if ((i+1)<gi.number_entry) 204 else
205 { 205 string_method="Unkn. ";
206 err = unzGoToNextFile(uf); 206
207 if (err!=UNZ_OK) 207 printf("%7lu %6s%c%7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
208 { 208 file_info.uncompressed_size,string_method,
209 printf("error %d with zipfile in unzGoToNextFile\n",err); 209 charCrypt,
210 break; 210 file_info.compressed_size,
211 } 211 ratio,
212 } 212 (uLong)file_info.tmu_date.tm_mon + 1,
213 } 213 (uLong)file_info.tmu_date.tm_mday,
214 214 (uLong)file_info.tmu_date.tm_year % 100,
215 return 0; 215 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
216} 216 (uLong)file_info.crc,filename_inzip);
217 217 if ((i+1)<gi.number_entry)
218 218 {
219int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password) 219 err = unzGoToNextFile(uf);
220 unzFile uf; 220 if (err!=UNZ_OK)
221 const int* popt_extract_without_path; 221 {
222 int* popt_overwrite; 222 printf("error %d with zipfile in unzGoToNextFile\n",err);
223 const char* password; 223 break;
224{ 224 }
225 char filename_inzip[256]; 225 }
226 char* filename_withoutpath; 226 }
227 char* p; 227
228 int err=UNZ_OK; 228 return 0;
229 FILE *fout=NULL; 229}
230 void* buf; 230
231 uInt size_buf; 231
232 232int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
233 unz_file_info file_info; 233 unzFile uf;
234 uLong ratio=0; 234 const int* popt_extract_without_path;
235 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); 235 int* popt_overwrite;
236 236 const char* password;
237 if (err!=UNZ_OK) 237{
238 { 238 char filename_inzip[256];
239 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); 239 char* filename_withoutpath;
240 return err; 240 char* p;
241 } 241 int err=UNZ_OK;
242 242 FILE *fout=NULL;
243 size_buf = WRITEBUFFERSIZE; 243 void* buf;
244 buf = (void*)malloc(size_buf); 244 uInt size_buf;
245 if (buf==NULL) 245
246 { 246 unz_file_info file_info;
247 printf("Error allocating memory\n"); 247 uLong ratio=0;
248 return UNZ_INTERNALERROR; 248 err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
249 } 249
250 250 if (err!=UNZ_OK)
251 p = filename_withoutpath = filename_inzip; 251 {
252 while ((*p) != '\0') 252 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
253 { 253 return err;
254 if (((*p)=='/') || ((*p)=='\\')) 254 }
255 filename_withoutpath = p+1; 255
256 p++; 256 size_buf = WRITEBUFFERSIZE;
257 } 257 buf = (void*)malloc(size_buf);
258 258 if (buf==NULL)
259 if ((*filename_withoutpath)=='\0') 259 {
260 { 260 printf("Error allocating memory\n");
261 if ((*popt_extract_without_path)==0) 261 return UNZ_INTERNALERROR;
262 { 262 }
263 printf("creating directory: %s\n",filename_inzip); 263
264 mymkdir(filename_inzip); 264 p = filename_withoutpath = filename_inzip;
265 } 265 while ((*p) != '\0')
266 } 266 {
267 else 267 if (((*p)=='/') || ((*p)=='\\'))
268 { 268 filename_withoutpath = p+1;
269 const char* write_filename; 269 p++;
270 int skip=0; 270 }
271 271
272 if ((*popt_extract_without_path)==0) 272 if ((*filename_withoutpath)=='\0')
273 write_filename = filename_inzip; 273 {
274 else 274 if ((*popt_extract_without_path)==0)
275 write_filename = filename_withoutpath; 275 {
276 276 printf("creating directory: %s\n",filename_inzip);
277 err = unzOpenCurrentFilePassword(uf,password); 277 mymkdir(filename_inzip);
278 if (err!=UNZ_OK) 278 }
279 { 279 }
280 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); 280 else
281 } 281 {
282 282 const char* write_filename;
283 if (((*popt_overwrite)==0) && (err==UNZ_OK)) 283 int skip=0;
284 { 284
285 char rep=0; 285 if ((*popt_extract_without_path)==0)
286 FILE* ftestexist; 286 write_filename = filename_inzip;
287 ftestexist = fopen(write_filename,"rb"); 287 else
288 if (ftestexist!=NULL) 288 write_filename = filename_withoutpath;
289 { 289
290 fclose(ftestexist); 290 err = unzOpenCurrentFilePassword(uf,password);
291 do 291 if (err!=UNZ_OK)
292 { 292 {
293 char answer[128]; 293 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
294 printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); 294 }
295 scanf("%1s",answer); 295
296 rep = answer[0] ; 296 if (((*popt_overwrite)==0) && (err==UNZ_OK))
297 if ((rep>='a') && (rep<='z')) 297 {
298 rep -= 0x20; 298 char rep=0;
299 } 299 FILE* ftestexist;
300 while ((rep!='Y') && (rep!='N') && (rep!='A')); 300 ftestexist = fopen(write_filename,"rb");
301 } 301 if (ftestexist!=NULL)
302 302 {
303 if (rep == 'N') 303 fclose(ftestexist);
304 skip = 1; 304 do
305 305 {
306 if (rep == 'A') 306 char answer[128];
307 *popt_overwrite=1; 307 printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
308 } 308 scanf("%1s",answer);
309 309 rep = answer[0] ;
310 if ((skip==0) && (err==UNZ_OK)) 310 if ((rep>='a') && (rep<='z'))
311 { 311 rep -= 0x20;
312 fout=fopen(write_filename,"wb"); 312 }
313 313 while ((rep!='Y') && (rep!='N') && (rep!='A'));
314 /* some zipfile don't contain directory alone before file */ 314 }
315 if ((fout==NULL) && ((*popt_extract_without_path)==0) && 315
316 (filename_withoutpath!=(char*)filename_inzip)) 316 if (rep == 'N')
317 { 317 skip = 1;
318 char c=*(filename_withoutpath-1); 318
319 *(filename_withoutpath-1)='\0'; 319 if (rep == 'A')
320 makedir(write_filename); 320 *popt_overwrite=1;
321 *(filename_withoutpath-1)=c; 321 }
322 fout=fopen(write_filename,"wb"); 322
323 } 323 if ((skip==0) && (err==UNZ_OK))
324 324 {
325 if (fout==NULL) 325 fout=fopen(write_filename,"wb");
326 { 326
327 printf("error opening %s\n",write_filename); 327 /* some zipfile don't contain directory alone before file */
328 } 328 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
329 } 329 (filename_withoutpath!=(char*)filename_inzip))
330 330 {
331 if (fout!=NULL) 331 char c=*(filename_withoutpath-1);
332 { 332 *(filename_withoutpath-1)='\0';
333 printf(" extracting: %s\n",write_filename); 333 makedir(write_filename);
334 334 *(filename_withoutpath-1)=c;
335 do 335 fout=fopen(write_filename,"wb");
336 { 336 }
337 err = unzReadCurrentFile(uf,buf,size_buf); 337
338 if (err<0) 338 if (fout==NULL)
339 { 339 {
340 printf("error %d with zipfile in unzReadCurrentFile\n",err); 340 printf("error opening %s\n",write_filename);
341 break; 341 }
342 } 342 }
343 if (err>0) 343
344 if (fwrite(buf,err,1,fout)!=1) 344 if (fout!=NULL)
345 { 345 {
346 printf("error in writing extracted file\n"); 346 printf(" extracting: %s\n",write_filename);
347 err=UNZ_ERRNO; 347
348 break; 348 do
349 } 349 {
350 } 350 err = unzReadCurrentFile(uf,buf,size_buf);
351 while (err>0); 351 if (err<0)
352 if (fout) 352 {
353 fclose(fout); 353 printf("error %d with zipfile in unzReadCurrentFile\n",err);
354 354 break;
355 if (err==0) 355 }
356 change_file_date(write_filename,file_info.dosDate, 356 if (err>0)
357 file_info.tmu_date); 357 if (fwrite(buf,err,1,fout)!=1)
358 } 358 {
359 359 printf("error in writing extracted file\n");
360 if (err==UNZ_OK) 360 err=UNZ_ERRNO;
361 { 361 break;
362 err = unzCloseCurrentFile (uf); 362 }
363 if (err!=UNZ_OK) 363 }
364 { 364 while (err>0);
365 printf("error %d with zipfile in unzCloseCurrentFile\n",err); 365 if (fout)
366 } 366 fclose(fout);
367 } 367
368 else 368 if (err==0)
369 unzCloseCurrentFile(uf); /* don't lose the error */ 369 change_file_date(write_filename,file_info.dosDate,
370 } 370 file_info.tmu_date);
371 371 }
372 free(buf); 372
373 return err; 373 if (err==UNZ_OK)
374} 374 {
375 375 err = unzCloseCurrentFile (uf);
376 376 if (err!=UNZ_OK)
377int do_extract(uf,opt_extract_without_path,opt_overwrite,password) 377 {
378 unzFile uf; 378 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
379 int opt_extract_without_path; 379 }
380 int opt_overwrite; 380 }
381 const char* password; 381 else
382{ 382 unzCloseCurrentFile(uf); /* don't lose the error */
383 uLong i; 383 }
384 unz_global_info gi; 384
385 int err; 385 free(buf);
386 FILE* fout=NULL; 386 return err;
387 387}
388 err = unzGetGlobalInfo (uf,&gi); 388
389 if (err!=UNZ_OK) 389
390 printf("error %d with zipfile in unzGetGlobalInfo \n",err); 390int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
391 391 unzFile uf;
392 for (i=0;i<gi.number_entry;i++) 392 int opt_extract_without_path;
393 { 393 int opt_overwrite;
394 if (do_extract_currentfile(uf,&opt_extract_without_path, 394 const char* password;
395 &opt_overwrite, 395{
396 password) != UNZ_OK) 396 uLong i;
397 break; 397 unz_global_info gi;
398 398 int err;
399 if ((i+1)<gi.number_entry) 399 FILE* fout=NULL;
400 { 400
401 err = unzGoToNextFile(uf); 401 err = unzGetGlobalInfo (uf,&gi);
402 if (err!=UNZ_OK) 402 if (err!=UNZ_OK)
403 { 403 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
404 printf("error %d with zipfile in unzGoToNextFile\n",err); 404
405 break; 405 for (i=0;i<gi.number_entry;i++)
406 } 406 {
407 } 407 if (do_extract_currentfile(uf,&opt_extract_without_path,
408 } 408 &opt_overwrite,
409 409 password) != UNZ_OK)
410 return 0; 410 break;
411} 411
412 412 if ((i+1)<gi.number_entry)
413int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password) 413 {
414 unzFile uf; 414 err = unzGoToNextFile(uf);
415 const char* filename; 415 if (err!=UNZ_OK)
416 int opt_extract_without_path; 416 {
417 int opt_overwrite; 417 printf("error %d with zipfile in unzGoToNextFile\n",err);
418 const char* password; 418 break;
419{ 419 }
420 int err = UNZ_OK; 420 }
421 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK) 421 }
422 { 422
423 printf("file %s not found in the zipfile\n",filename); 423 return 0;
424 return 2; 424}
425 } 425
426 426int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
427 if (do_extract_currentfile(uf,&opt_extract_without_path, 427 unzFile uf;
428 &opt_overwrite, 428 const char* filename;
429 password) == UNZ_OK) 429 int opt_extract_without_path;
430 return 0; 430 int opt_overwrite;
431 else 431 const char* password;
432 return 1; 432{
433} 433 int err = UNZ_OK;
434 434 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
435 435 {
436int main(argc,argv) 436 printf("file %s not found in the zipfile\n",filename);
437 int argc; 437 return 2;
438 char *argv[]; 438 }
439{ 439
440 const char *zipfilename=NULL; 440 if (do_extract_currentfile(uf,&opt_extract_without_path,
441 const char *filename_to_extract=NULL; 441 &opt_overwrite,
442 const char *password=NULL; 442 password) == UNZ_OK)
443 char filename_try[MAXFILENAME+16] = ""; 443 return 0;
444 int i; 444 else
445 int opt_do_list=0; 445 return 1;
446 int opt_do_extract=1; 446}
447 int opt_do_extract_withoutpath=0; 447
448 int opt_overwrite=0; 448
449 unzFile uf=NULL; 449int main(argc,argv)
450 450 int argc;
451 do_banner(); 451 char *argv[];
452 if (argc==1) 452{
453 { 453 const char *zipfilename=NULL;
454 do_help(); 454 const char *filename_to_extract=NULL;
455 return 0; 455 const char *password=NULL;
456 } 456 char filename_try[MAXFILENAME+16] = "";
457 else 457 int i;
458 { 458 int opt_do_list=0;
459 for (i=1;i<argc;i++) 459 int opt_do_extract=1;
460 { 460 int opt_do_extract_withoutpath=0;
461 if ((*argv[i])=='-') 461 int opt_overwrite=0;
462 { 462 unzFile uf=NULL;
463 const char *p=argv[i]+1; 463
464 464 do_banner();
465 while ((*p)!='\0') 465 if (argc==1)
466 { 466 {
467 char c=*(p++);; 467 do_help();
468 if ((c=='l') || (c=='L')) 468 return 0;
469 opt_do_list = 1; 469 }
470 if ((c=='v') || (c=='V')) 470 else
471 opt_do_list = 1; 471 {
472 if ((c=='x') || (c=='X')) 472 for (i=1;i<argc;i++)
473 opt_do_extract = 1; 473 {
474 if ((c=='e') || (c=='E')) 474 if ((*argv[i])=='-')
475 opt_do_extract = opt_do_extract_withoutpath = 1; 475 {
476 if ((c=='o') || (c=='O')) 476 const char *p=argv[i]+1;
477 opt_overwrite=1; 477
478 if (((c=='p') || (c=='P')) && (i+1<argc)) 478 while ((*p)!='\0')
479 { 479 {
480 password=argv[i+1]; 480 char c=*(p++);;
481 i++; 481 if ((c=='l') || (c=='L'))
482 } 482 opt_do_list = 1;
483 } 483 if ((c=='v') || (c=='V'))
484 } 484 opt_do_list = 1;
485 else 485 if ((c=='x') || (c=='X'))
486 { 486 opt_do_extract = 1;
487 if (zipfilename == NULL) 487 if ((c=='e') || (c=='E'))
488 zipfilename = argv[i]; 488 opt_do_extract = opt_do_extract_withoutpath = 1;
489 else if (filename_to_extract==NULL) 489 if ((c=='o') || (c=='O'))
490 filename_to_extract = argv[i] ; 490 opt_overwrite=1;
491 } 491 if (((c=='p') || (c=='P')) && (i+1<argc))
492 } 492 {
493 } 493 password=argv[i+1];
494 494 i++;
495 if (zipfilename!=NULL) 495 }
496 { 496 }
497 497 }
498 #ifdef USEWIN32IOAPI 498 else
499 zlib_filefunc_def ffunc; 499 {
500 #endif 500 if (zipfilename == NULL)
501 501 zipfilename = argv[i];
502 strncpy(filename_try, zipfilename,MAXFILENAME-1); 502 else if (filename_to_extract==NULL)
503 /* strncpy doesnt append the trailing NULL, of the string is too long. */ 503 filename_to_extract = argv[i] ;
504 filename_try[ MAXFILENAME ] = '\0'; 504 }
505 505 }
506 #ifdef USEWIN32IOAPI 506 }
507 fill_win32_filefunc(&ffunc); 507
508 uf = unzOpen2(zipfilename,&ffunc); 508 if (zipfilename!=NULL)
509 #else 509 {
510 uf = unzOpen(zipfilename); 510
511 #endif 511 #ifdef USEWIN32IOAPI
512 if (uf==NULL) 512 zlib_filefunc_def ffunc;
513 { 513 #endif
514 strcat(filename_try,".zip"); 514
515 #ifdef USEWIN32IOAPI 515 strncpy(filename_try, zipfilename,MAXFILENAME-1);
516 uf = unzOpen2(filename_try,&ffunc); 516 /* strncpy doesnt append the trailing NULL, of the string is too long. */
517 #else 517 filename_try[ MAXFILENAME ] = '\0';
518 uf = unzOpen(filename_try); 518
519 #endif 519 #ifdef USEWIN32IOAPI
520 } 520 fill_win32_filefunc(&ffunc);
521 } 521 uf = unzOpen2(zipfilename,&ffunc);
522 522 #else
523 if (uf==NULL) 523 uf = unzOpen(zipfilename);
524 { 524 #endif
525 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); 525 if (uf==NULL)
526 return 1; 526 {
527 } 527 strcat(filename_try,".zip");
528 printf("%s opened\n",filename_try); 528 #ifdef USEWIN32IOAPI
529 529 uf = unzOpen2(filename_try,&ffunc);
530 if (opt_do_list==1) 530 #else
531 return do_list(uf); 531 uf = unzOpen(filename_try);
532 else if (opt_do_extract==1) 532 #endif
533 { 533 }
534 if (filename_to_extract == NULL) 534 }
535 return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password); 535
536 else 536 if (uf==NULL)
537 return do_extract_onefile(uf,filename_to_extract, 537 {
538 opt_do_extract_withoutpath,opt_overwrite,password); 538 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
539 } 539 return 1;
540 unzCloseCurrentFile(uf); 540 }
541 541 printf("%s opened\n",filename_try);
542 return 0; 542
543} 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}