miniunz.c revision 1.1.1.3 1 1.1 christos /*
2 1.1 christos miniunz.c
3 1.1.1.2 christos Version 1.1, February 14h, 2010
4 1.1.1.2 christos sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
5 1.1.1.2 christos
6 1.1.1.2 christos Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
7 1.1.1.2 christos
8 1.1.1.2 christos Modifications of Unzip for Zip64
9 1.1.1.2 christos Copyright (C) 2007-2008 Even Rouault
10 1.1 christos
11 1.1.1.2 christos Modifications for Zip64 support on both zip and unzip
12 1.1.1.2 christos Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
13 1.1 christos */
14 1.1 christos
15 1.1.1.2 christos #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
16 1.1.1.2 christos #ifndef __USE_FILE_OFFSET64
17 1.1.1.2 christos #define __USE_FILE_OFFSET64
18 1.1.1.2 christos #endif
19 1.1.1.2 christos #ifndef __USE_LARGEFILE64
20 1.1.1.2 christos #define __USE_LARGEFILE64
21 1.1.1.2 christos #endif
22 1.1.1.2 christos #ifndef _LARGEFILE64_SOURCE
23 1.1.1.2 christos #define _LARGEFILE64_SOURCE
24 1.1.1.2 christos #endif
25 1.1.1.2 christos #ifndef _FILE_OFFSET_BIT
26 1.1.1.2 christos #define _FILE_OFFSET_BIT 64
27 1.1.1.2 christos #endif
28 1.1.1.2 christos #endif
29 1.1.1.2 christos
30 1.1.1.2 christos #ifdef __APPLE__
31 1.1.1.2 christos // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
32 1.1.1.2 christos #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
33 1.1.1.2 christos #define FTELLO_FUNC(stream) ftello(stream)
34 1.1.1.2 christos #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
35 1.1.1.2 christos #else
36 1.1.1.2 christos #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
37 1.1.1.2 christos #define FTELLO_FUNC(stream) ftello64(stream)
38 1.1.1.2 christos #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
39 1.1.1.2 christos #endif
40 1.1.1.2 christos
41 1.1 christos
42 1.1 christos #include <stdio.h>
43 1.1 christos #include <stdlib.h>
44 1.1 christos #include <string.h>
45 1.1 christos #include <time.h>
46 1.1 christos #include <errno.h>
47 1.1 christos #include <fcntl.h>
48 1.1.1.3 christos #include <sys/stat.h>
49 1.1 christos
50 1.1.1.2 christos #ifdef _WIN32
51 1.1 christos # include <direct.h>
52 1.1 christos # include <io.h>
53 1.1.1.2 christos #else
54 1.1.1.2 christos # include <unistd.h>
55 1.1.1.2 christos # include <utime.h>
56 1.1 christos #endif
57 1.1 christos
58 1.1.1.2 christos
59 1.1 christos #include "unzip.h"
60 1.1 christos
61 1.1 christos #define CASESENSITIVITY (0)
62 1.1 christos #define WRITEBUFFERSIZE (8192)
63 1.1 christos #define MAXFILENAME (256)
64 1.1 christos
65 1.1.1.2 christos #ifdef _WIN32
66 1.1 christos #define USEWIN32IOAPI
67 1.1 christos #include "iowin32.h"
68 1.1 christos #endif
69 1.1 christos /*
70 1.1 christos mini unzip, demo of unzip package
71 1.1 christos
72 1.1 christos usage :
73 1.1 christos Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
74 1.1 christos
75 1.1 christos list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
76 1.1 christos if it exists
77 1.1 christos */
78 1.1 christos
79 1.1 christos
80 1.1 christos /* change_file_date : change the date/time of a file
81 1.1 christos filename : the filename of the file where date/time must be modified
82 1.1 christos dosdate : the new date at the MSDos format (4 bytes)
83 1.1 christos tmu_date : the SAME new date at the tm_unz format */
84 1.1.1.3 christos static void change_file_date(filename,dosdate,tmu_date)
85 1.1 christos const char *filename;
86 1.1 christos uLong dosdate;
87 1.1 christos tm_unz tmu_date;
88 1.1 christos {
89 1.1.1.2 christos #ifdef _WIN32
90 1.1 christos HANDLE hFile;
91 1.1 christos FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
92 1.1 christos
93 1.1.1.2 christos hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
94 1.1 christos 0,NULL,OPEN_EXISTING,0,NULL);
95 1.1 christos GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
96 1.1 christos DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
97 1.1 christos LocalFileTimeToFileTime(&ftLocal,&ftm);
98 1.1 christos SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
99 1.1 christos CloseHandle(hFile);
100 1.1 christos #else
101 1.1.1.3 christos #if defined(unix) || defined(__APPLE__)
102 1.1.1.3 christos (void)dosdate;
103 1.1 christos struct utimbuf ut;
104 1.1 christos struct tm newdate;
105 1.1 christos newdate.tm_sec = tmu_date.tm_sec;
106 1.1 christos newdate.tm_min=tmu_date.tm_min;
107 1.1 christos newdate.tm_hour=tmu_date.tm_hour;
108 1.1 christos newdate.tm_mday=tmu_date.tm_mday;
109 1.1 christos newdate.tm_mon=tmu_date.tm_mon;
110 1.1 christos if (tmu_date.tm_year > 1900)
111 1.1 christos newdate.tm_year=tmu_date.tm_year - 1900;
112 1.1 christos else
113 1.1 christos newdate.tm_year=tmu_date.tm_year ;
114 1.1 christos newdate.tm_isdst=-1;
115 1.1 christos
116 1.1 christos ut.actime=ut.modtime=mktime(&newdate);
117 1.1 christos utime(filename,&ut);
118 1.1 christos #endif
119 1.1 christos #endif
120 1.1 christos }
121 1.1 christos
122 1.1 christos
123 1.1 christos /* mymkdir and change_file_date are not 100 % portable
124 1.1 christos As I don't know well Unix, I wait feedback for the unix portion */
125 1.1 christos
126 1.1.1.3 christos static int mymkdir(dirname)
127 1.1 christos const char* dirname;
128 1.1 christos {
129 1.1 christos int ret=0;
130 1.1.1.2 christos #ifdef _WIN32
131 1.1.1.2 christos ret = _mkdir(dirname);
132 1.1.1.2 christos #elif unix
133 1.1.1.2 christos ret = mkdir (dirname,0775);
134 1.1.1.2 christos #elif __APPLE__
135 1.1 christos ret = mkdir (dirname,0775);
136 1.1 christos #endif
137 1.1 christos return ret;
138 1.1 christos }
139 1.1 christos
140 1.1.1.3 christos static int makedir (newdir)
141 1.1.1.3 christos const char *newdir;
142 1.1 christos {
143 1.1 christos char *buffer ;
144 1.1 christos char *p;
145 1.1.1.3 christos size_t len = strlen(newdir);
146 1.1 christos
147 1.1.1.3 christos if (len == 0)
148 1.1 christos return 0;
149 1.1 christos
150 1.1 christos buffer = (char*)malloc(len+1);
151 1.1.1.2 christos if (buffer==NULL)
152 1.1.1.2 christos {
153 1.1.1.2 christos printf("Error allocating memory\n");
154 1.1.1.2 christos return UNZ_INTERNALERROR;
155 1.1.1.2 christos }
156 1.1 christos strcpy(buffer,newdir);
157 1.1 christos
158 1.1 christos if (buffer[len-1] == '/') {
159 1.1 christos buffer[len-1] = '\0';
160 1.1 christos }
161 1.1 christos if (mymkdir(buffer) == 0)
162 1.1 christos {
163 1.1 christos free(buffer);
164 1.1 christos return 1;
165 1.1 christos }
166 1.1 christos
167 1.1 christos p = buffer+1;
168 1.1 christos while (1)
169 1.1 christos {
170 1.1 christos char hold;
171 1.1 christos
172 1.1 christos while(*p && *p != '\\' && *p != '/')
173 1.1 christos p++;
174 1.1 christos hold = *p;
175 1.1 christos *p = 0;
176 1.1 christos if ((mymkdir(buffer) == -1) && (errno == ENOENT))
177 1.1 christos {
178 1.1 christos printf("couldn't create directory %s\n",buffer);
179 1.1 christos free(buffer);
180 1.1 christos return 0;
181 1.1 christos }
182 1.1 christos if (hold == 0)
183 1.1 christos break;
184 1.1 christos *p++ = hold;
185 1.1 christos }
186 1.1 christos free(buffer);
187 1.1 christos return 1;
188 1.1 christos }
189 1.1 christos
190 1.1.1.3 christos static void do_banner()
191 1.1 christos {
192 1.1 christos printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
193 1.1 christos printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
194 1.1 christos }
195 1.1 christos
196 1.1.1.3 christos static void do_help()
197 1.1 christos {
198 1.1 christos printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
199 1.1 christos " -e Extract without pathname (junk paths)\n" \
200 1.1 christos " -x Extract with pathname\n" \
201 1.1 christos " -v list files\n" \
202 1.1 christos " -l list files\n" \
203 1.1 christos " -d directory to extract into\n" \
204 1.1 christos " -o overwrite files without prompting\n" \
205 1.1 christos " -p extract crypted file using password\n\n");
206 1.1 christos }
207 1.1 christos
208 1.1.1.3 christos static void Display64BitsSize(ZPOS64_T n, int size_char)
209 1.1.1.2 christos {
210 1.1.1.2 christos /* to avoid compatibility problem , we do here the conversion */
211 1.1.1.2 christos char number[21];
212 1.1.1.2 christos int offset=19;
213 1.1.1.2 christos int pos_string = 19;
214 1.1.1.2 christos number[20]=0;
215 1.1.1.2 christos for (;;) {
216 1.1.1.2 christos number[offset]=(char)((n%10)+'0');
217 1.1.1.2 christos if (number[offset] != '0')
218 1.1.1.2 christos pos_string=offset;
219 1.1.1.2 christos n/=10;
220 1.1.1.2 christos if (offset==0)
221 1.1.1.2 christos break;
222 1.1.1.2 christos offset--;
223 1.1.1.2 christos }
224 1.1.1.2 christos {
225 1.1.1.2 christos int size_display_string = 19-pos_string;
226 1.1.1.2 christos while (size_char > size_display_string)
227 1.1.1.2 christos {
228 1.1.1.2 christos size_char--;
229 1.1.1.2 christos printf(" ");
230 1.1.1.2 christos }
231 1.1.1.2 christos }
232 1.1.1.2 christos
233 1.1.1.2 christos printf("%s",&number[pos_string]);
234 1.1.1.2 christos }
235 1.1 christos
236 1.1.1.3 christos static int do_list(uf)
237 1.1 christos unzFile uf;
238 1.1 christos {
239 1.1 christos uLong i;
240 1.1.1.2 christos unz_global_info64 gi;
241 1.1 christos int err;
242 1.1 christos
243 1.1.1.2 christos err = unzGetGlobalInfo64(uf,&gi);
244 1.1 christos if (err!=UNZ_OK)
245 1.1 christos printf("error %d with zipfile in unzGetGlobalInfo \n",err);
246 1.1.1.2 christos printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
247 1.1.1.2 christos printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
248 1.1 christos for (i=0;i<gi.number_entry;i++)
249 1.1 christos {
250 1.1 christos char filename_inzip[256];
251 1.1.1.2 christos unz_file_info64 file_info;
252 1.1 christos uLong ratio=0;
253 1.1 christos const char *string_method;
254 1.1 christos char charCrypt=' ';
255 1.1.1.2 christos err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
256 1.1 christos if (err!=UNZ_OK)
257 1.1 christos {
258 1.1 christos printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
259 1.1 christos break;
260 1.1 christos }
261 1.1 christos if (file_info.uncompressed_size>0)
262 1.1.1.2 christos ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
263 1.1 christos
264 1.1 christos /* display a '*' if the file is crypted */
265 1.1 christos if ((file_info.flag & 1) != 0)
266 1.1 christos charCrypt='*';
267 1.1 christos
268 1.1 christos if (file_info.compression_method==0)
269 1.1 christos string_method="Stored";
270 1.1 christos else
271 1.1 christos if (file_info.compression_method==Z_DEFLATED)
272 1.1 christos {
273 1.1 christos uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
274 1.1 christos if (iLevel==0)
275 1.1 christos string_method="Defl:N";
276 1.1 christos else if (iLevel==1)
277 1.1 christos string_method="Defl:X";
278 1.1 christos else if ((iLevel==2) || (iLevel==3))
279 1.1 christos string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
280 1.1 christos }
281 1.1 christos else
282 1.1.1.2 christos if (file_info.compression_method==Z_BZIP2ED)
283 1.1.1.2 christos {
284 1.1.1.2 christos string_method="BZip2 ";
285 1.1.1.2 christos }
286 1.1.1.2 christos else
287 1.1 christos string_method="Unkn. ";
288 1.1 christos
289 1.1.1.2 christos Display64BitsSize(file_info.uncompressed_size,7);
290 1.1.1.2 christos printf(" %6s%c",string_method,charCrypt);
291 1.1.1.2 christos Display64BitsSize(file_info.compressed_size,7);
292 1.1.1.2 christos printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
293 1.1 christos ratio,
294 1.1 christos (uLong)file_info.tmu_date.tm_mon + 1,
295 1.1 christos (uLong)file_info.tmu_date.tm_mday,
296 1.1 christos (uLong)file_info.tmu_date.tm_year % 100,
297 1.1 christos (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
298 1.1 christos (uLong)file_info.crc,filename_inzip);
299 1.1 christos if ((i+1)<gi.number_entry)
300 1.1 christos {
301 1.1 christos err = unzGoToNextFile(uf);
302 1.1 christos if (err!=UNZ_OK)
303 1.1 christos {
304 1.1 christos printf("error %d with zipfile in unzGoToNextFile\n",err);
305 1.1 christos break;
306 1.1 christos }
307 1.1 christos }
308 1.1 christos }
309 1.1 christos
310 1.1 christos return 0;
311 1.1 christos }
312 1.1 christos
313 1.1 christos
314 1.1.1.3 christos static int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
315 1.1 christos unzFile uf;
316 1.1 christos const int* popt_extract_without_path;
317 1.1 christos int* popt_overwrite;
318 1.1 christos const char* password;
319 1.1 christos {
320 1.1 christos char filename_inzip[256];
321 1.1 christos char* filename_withoutpath;
322 1.1 christos char* p;
323 1.1 christos int err=UNZ_OK;
324 1.1 christos FILE *fout=NULL;
325 1.1 christos void* buf;
326 1.1 christos uInt size_buf;
327 1.1 christos
328 1.1.1.2 christos unz_file_info64 file_info;
329 1.1.1.2 christos err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
330 1.1 christos
331 1.1 christos if (err!=UNZ_OK)
332 1.1 christos {
333 1.1 christos printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
334 1.1 christos return err;
335 1.1 christos }
336 1.1 christos
337 1.1 christos size_buf = WRITEBUFFERSIZE;
338 1.1 christos buf = (void*)malloc(size_buf);
339 1.1 christos if (buf==NULL)
340 1.1 christos {
341 1.1 christos printf("Error allocating memory\n");
342 1.1 christos return UNZ_INTERNALERROR;
343 1.1 christos }
344 1.1 christos
345 1.1 christos p = filename_withoutpath = filename_inzip;
346 1.1 christos while ((*p) != '\0')
347 1.1 christos {
348 1.1 christos if (((*p)=='/') || ((*p)=='\\'))
349 1.1 christos filename_withoutpath = p+1;
350 1.1 christos p++;
351 1.1 christos }
352 1.1 christos
353 1.1 christos if ((*filename_withoutpath)=='\0')
354 1.1 christos {
355 1.1 christos if ((*popt_extract_without_path)==0)
356 1.1 christos {
357 1.1 christos printf("creating directory: %s\n",filename_inzip);
358 1.1 christos mymkdir(filename_inzip);
359 1.1 christos }
360 1.1 christos }
361 1.1 christos else
362 1.1 christos {
363 1.1 christos const char* write_filename;
364 1.1 christos int skip=0;
365 1.1 christos
366 1.1 christos if ((*popt_extract_without_path)==0)
367 1.1 christos write_filename = filename_inzip;
368 1.1 christos else
369 1.1 christos write_filename = filename_withoutpath;
370 1.1 christos
371 1.1 christos err = unzOpenCurrentFilePassword(uf,password);
372 1.1 christos if (err!=UNZ_OK)
373 1.1 christos {
374 1.1 christos printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
375 1.1 christos }
376 1.1 christos
377 1.1 christos if (((*popt_overwrite)==0) && (err==UNZ_OK))
378 1.1 christos {
379 1.1 christos char rep=0;
380 1.1 christos FILE* ftestexist;
381 1.1.1.2 christos ftestexist = FOPEN_FUNC(write_filename,"rb");
382 1.1 christos if (ftestexist!=NULL)
383 1.1 christos {
384 1.1 christos fclose(ftestexist);
385 1.1 christos do
386 1.1 christos {
387 1.1 christos char answer[128];
388 1.1 christos int ret;
389 1.1 christos
390 1.1 christos printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
391 1.1 christos ret = scanf("%1s",answer);
392 1.1.1.2 christos if (ret != 1)
393 1.1 christos {
394 1.1 christos exit(EXIT_FAILURE);
395 1.1 christos }
396 1.1 christos rep = answer[0] ;
397 1.1 christos if ((rep>='a') && (rep<='z'))
398 1.1 christos rep -= 0x20;
399 1.1 christos }
400 1.1 christos while ((rep!='Y') && (rep!='N') && (rep!='A'));
401 1.1 christos }
402 1.1 christos
403 1.1 christos if (rep == 'N')
404 1.1 christos skip = 1;
405 1.1 christos
406 1.1 christos if (rep == 'A')
407 1.1 christos *popt_overwrite=1;
408 1.1 christos }
409 1.1 christos
410 1.1 christos if ((skip==0) && (err==UNZ_OK))
411 1.1 christos {
412 1.1.1.2 christos fout=FOPEN_FUNC(write_filename,"wb");
413 1.1 christos /* some zipfile don't contain directory alone before file */
414 1.1 christos if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
415 1.1 christos (filename_withoutpath!=(char*)filename_inzip))
416 1.1 christos {
417 1.1 christos char c=*(filename_withoutpath-1);
418 1.1 christos *(filename_withoutpath-1)='\0';
419 1.1 christos makedir(write_filename);
420 1.1 christos *(filename_withoutpath-1)=c;
421 1.1.1.2 christos fout=FOPEN_FUNC(write_filename,"wb");
422 1.1 christos }
423 1.1 christos
424 1.1 christos if (fout==NULL)
425 1.1 christos {
426 1.1 christos printf("error opening %s\n",write_filename);
427 1.1 christos }
428 1.1 christos }
429 1.1 christos
430 1.1 christos if (fout!=NULL)
431 1.1 christos {
432 1.1 christos printf(" extracting: %s\n",write_filename);
433 1.1 christos
434 1.1 christos do
435 1.1 christos {
436 1.1 christos err = unzReadCurrentFile(uf,buf,size_buf);
437 1.1 christos if (err<0)
438 1.1 christos {
439 1.1 christos printf("error %d with zipfile in unzReadCurrentFile\n",err);
440 1.1 christos break;
441 1.1 christos }
442 1.1 christos if (err>0)
443 1.1.1.3 christos if (fwrite(buf,(unsigned)err,1,fout)!=1)
444 1.1 christos {
445 1.1 christos printf("error in writing extracted file\n");
446 1.1 christos err=UNZ_ERRNO;
447 1.1 christos break;
448 1.1 christos }
449 1.1 christos }
450 1.1 christos while (err>0);
451 1.1 christos if (fout)
452 1.1 christos fclose(fout);
453 1.1 christos
454 1.1 christos if (err==0)
455 1.1 christos change_file_date(write_filename,file_info.dosDate,
456 1.1 christos file_info.tmu_date);
457 1.1 christos }
458 1.1 christos
459 1.1 christos if (err==UNZ_OK)
460 1.1 christos {
461 1.1 christos err = unzCloseCurrentFile (uf);
462 1.1 christos if (err!=UNZ_OK)
463 1.1 christos {
464 1.1 christos printf("error %d with zipfile in unzCloseCurrentFile\n",err);
465 1.1 christos }
466 1.1 christos }
467 1.1 christos else
468 1.1 christos unzCloseCurrentFile(uf); /* don't lose the error */
469 1.1 christos }
470 1.1 christos
471 1.1 christos free(buf);
472 1.1 christos return err;
473 1.1 christos }
474 1.1 christos
475 1.1 christos
476 1.1.1.3 christos static int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
477 1.1 christos unzFile uf;
478 1.1 christos int opt_extract_without_path;
479 1.1 christos int opt_overwrite;
480 1.1 christos const char* password;
481 1.1 christos {
482 1.1 christos uLong i;
483 1.1.1.2 christos unz_global_info64 gi;
484 1.1 christos int err;
485 1.1 christos
486 1.1.1.2 christos err = unzGetGlobalInfo64(uf,&gi);
487 1.1 christos if (err!=UNZ_OK)
488 1.1 christos printf("error %d with zipfile in unzGetGlobalInfo \n",err);
489 1.1 christos
490 1.1 christos for (i=0;i<gi.number_entry;i++)
491 1.1 christos {
492 1.1 christos if (do_extract_currentfile(uf,&opt_extract_without_path,
493 1.1 christos &opt_overwrite,
494 1.1 christos password) != UNZ_OK)
495 1.1 christos break;
496 1.1 christos
497 1.1 christos if ((i+1)<gi.number_entry)
498 1.1 christos {
499 1.1 christos err = unzGoToNextFile(uf);
500 1.1 christos if (err!=UNZ_OK)
501 1.1 christos {
502 1.1 christos printf("error %d with zipfile in unzGoToNextFile\n",err);
503 1.1 christos break;
504 1.1 christos }
505 1.1 christos }
506 1.1 christos }
507 1.1 christos
508 1.1 christos return 0;
509 1.1 christos }
510 1.1 christos
511 1.1.1.3 christos static int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
512 1.1 christos unzFile uf;
513 1.1 christos const char* filename;
514 1.1 christos int opt_extract_without_path;
515 1.1 christos int opt_overwrite;
516 1.1 christos const char* password;
517 1.1 christos {
518 1.1 christos if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
519 1.1 christos {
520 1.1 christos printf("file %s not found in the zipfile\n",filename);
521 1.1 christos return 2;
522 1.1 christos }
523 1.1 christos
524 1.1 christos if (do_extract_currentfile(uf,&opt_extract_without_path,
525 1.1 christos &opt_overwrite,
526 1.1 christos password) == UNZ_OK)
527 1.1 christos return 0;
528 1.1 christos else
529 1.1 christos return 1;
530 1.1 christos }
531 1.1 christos
532 1.1 christos
533 1.1 christos int main(argc,argv)
534 1.1 christos int argc;
535 1.1 christos char *argv[];
536 1.1 christos {
537 1.1 christos const char *zipfilename=NULL;
538 1.1 christos const char *filename_to_extract=NULL;
539 1.1 christos const char *password=NULL;
540 1.1 christos char filename_try[MAXFILENAME+16] = "";
541 1.1 christos int i;
542 1.1.1.2 christos int ret_value=0;
543 1.1 christos int opt_do_list=0;
544 1.1 christos int opt_do_extract=1;
545 1.1 christos int opt_do_extract_withoutpath=0;
546 1.1 christos int opt_overwrite=0;
547 1.1 christos int opt_extractdir=0;
548 1.1 christos const char *dirname=NULL;
549 1.1 christos unzFile uf=NULL;
550 1.1 christos
551 1.1 christos do_banner();
552 1.1 christos if (argc==1)
553 1.1 christos {
554 1.1 christos do_help();
555 1.1 christos return 0;
556 1.1 christos }
557 1.1 christos else
558 1.1 christos {
559 1.1 christos for (i=1;i<argc;i++)
560 1.1 christos {
561 1.1 christos if ((*argv[i])=='-')
562 1.1 christos {
563 1.1 christos const char *p=argv[i]+1;
564 1.1 christos
565 1.1 christos while ((*p)!='\0')
566 1.1 christos {
567 1.1.1.3 christos char c=*(p++);
568 1.1 christos if ((c=='l') || (c=='L'))
569 1.1 christos opt_do_list = 1;
570 1.1 christos if ((c=='v') || (c=='V'))
571 1.1 christos opt_do_list = 1;
572 1.1 christos if ((c=='x') || (c=='X'))
573 1.1 christos opt_do_extract = 1;
574 1.1 christos if ((c=='e') || (c=='E'))
575 1.1 christos opt_do_extract = opt_do_extract_withoutpath = 1;
576 1.1 christos if ((c=='o') || (c=='O'))
577 1.1 christos opt_overwrite=1;
578 1.1 christos if ((c=='d') || (c=='D'))
579 1.1 christos {
580 1.1 christos opt_extractdir=1;
581 1.1 christos dirname=argv[i+1];
582 1.1 christos }
583 1.1 christos
584 1.1 christos if (((c=='p') || (c=='P')) && (i+1<argc))
585 1.1 christos {
586 1.1 christos password=argv[i+1];
587 1.1 christos i++;
588 1.1 christos }
589 1.1 christos }
590 1.1 christos }
591 1.1 christos else
592 1.1 christos {
593 1.1 christos if (zipfilename == NULL)
594 1.1 christos zipfilename = argv[i];
595 1.1 christos else if ((filename_to_extract==NULL) && (!opt_extractdir))
596 1.1 christos filename_to_extract = argv[i] ;
597 1.1 christos }
598 1.1 christos }
599 1.1 christos }
600 1.1 christos
601 1.1 christos if (zipfilename!=NULL)
602 1.1 christos {
603 1.1 christos
604 1.1 christos # ifdef USEWIN32IOAPI
605 1.1.1.2 christos zlib_filefunc64_def ffunc;
606 1.1 christos # endif
607 1.1 christos
608 1.1 christos strncpy(filename_try, zipfilename,MAXFILENAME-1);
609 1.1 christos /* strncpy doesnt append the trailing NULL, of the string is too long. */
610 1.1 christos filename_try[ MAXFILENAME ] = '\0';
611 1.1 christos
612 1.1 christos # ifdef USEWIN32IOAPI
613 1.1.1.2 christos fill_win32_filefunc64A(&ffunc);
614 1.1.1.2 christos uf = unzOpen2_64(zipfilename,&ffunc);
615 1.1 christos # else
616 1.1.1.2 christos uf = unzOpen64(zipfilename);
617 1.1 christos # endif
618 1.1 christos if (uf==NULL)
619 1.1 christos {
620 1.1 christos strcat(filename_try,".zip");
621 1.1 christos # ifdef USEWIN32IOAPI
622 1.1.1.2 christos uf = unzOpen2_64(filename_try,&ffunc);
623 1.1 christos # else
624 1.1.1.2 christos uf = unzOpen64(filename_try);
625 1.1 christos # endif
626 1.1 christos }
627 1.1 christos }
628 1.1 christos
629 1.1 christos if (uf==NULL)
630 1.1 christos {
631 1.1 christos printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
632 1.1 christos return 1;
633 1.1 christos }
634 1.1 christos printf("%s opened\n",filename_try);
635 1.1 christos
636 1.1 christos if (opt_do_list==1)
637 1.1.1.2 christos ret_value = do_list(uf);
638 1.1 christos else if (opt_do_extract==1)
639 1.1 christos {
640 1.1.1.2 christos #ifdef _WIN32
641 1.1.1.2 christos if (opt_extractdir && _chdir(dirname))
642 1.1.1.2 christos #else
643 1.1.1.2 christos if (opt_extractdir && chdir(dirname))
644 1.1.1.2 christos #endif
645 1.1 christos {
646 1.1 christos printf("Error changing into %s, aborting\n", dirname);
647 1.1 christos exit(-1);
648 1.1 christos }
649 1.1 christos
650 1.1 christos if (filename_to_extract == NULL)
651 1.1.1.2 christos ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
652 1.1 christos else
653 1.1.1.2 christos ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
654 1.1 christos }
655 1.1 christos
656 1.1.1.2 christos unzClose(uf);
657 1.1.1.2 christos
658 1.1.1.2 christos return ret_value;
659 1.1 christos }
660