diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:22:37 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:22:37 -0700 |
commit | 4b5a43a219d51066c01ff2ab86af18b967f2d0dd (patch) | |
tree | 4dcaf0cd18751d04cf638a9a6ec521990d4f2e90 /contrib/minizip/miniunz.c | |
parent | 086e982175da84b3db958191031380794315f95f (diff) | |
download | zlib-1.2.0.5.tar.gz zlib-1.2.0.5.tar.bz2 zlib-1.2.0.5.zip |
zlib 1.2.0.5v1.2.0.5
Diffstat (limited to 'contrib/minizip/miniunz.c')
-rw-r--r-- | contrib/minizip/miniunz.c | 1112 |
1 files changed, 556 insertions, 556 deletions
diff --git a/contrib/minizip/miniunz.c b/contrib/minizip/miniunz.c index ea23e40..57601e1 100644 --- a/contrib/minizip/miniunz.c +++ b/contrib/minizip/miniunz.c | |||
@@ -1,556 +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 */ |
41 | void change_file_date(filename,dosdate,tmu_date) | 41 | void 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 | ||
82 | int mymkdir(dirname) | 82 | int 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 | ||
96 | int makedir (newdir) | 96 | int 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 | ||
141 | void do_banner() | 141 | void do_banner() |
142 | { | 142 | { |
143 | printf("MiniUnz 0.22, 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 | ||
147 | void do_help() | 147 | void do_help() |
148 | { | 148 | { |
149 | printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.]\n\n" \ | 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" \ | 150 | " -e Extract without pathname (junk paths)\n" \ |
151 | " -x Extract with pathname\n" \ | 151 | " -x Extract with pathname\n" \ |
152 | " -v list files\n" \ | 152 | " -v list files\n" \ |
153 | " -l list files\n" \ | 153 | " -l list files\n" \ |
154 | " -o overwrite files without prompting\n" \ | 154 | " -o overwrite files without prompting\n" \ |
155 | " -p extract crypted file using password\n\n"); | 155 | " -p extract crypted file using password\n\n"); |
156 | } | 156 | } |
157 | 157 | ||
158 | 158 | ||
159 | int do_list(uf) | 159 | int do_list(uf) |
160 | unzFile uf; | 160 | unzFile uf; |
161 | { | 161 | { |
162 | uLong i; | 162 | uLong i; |
163 | unz_global_info gi; | 163 | unz_global_info gi; |
164 | int err; | 164 | int err; |
165 | 165 | ||
166 | err = unzGetGlobalInfo (uf,&gi); | 166 | err = unzGetGlobalInfo (uf,&gi); |
167 | if (err!=UNZ_OK) | 167 | if (err!=UNZ_OK) |
168 | printf("error %d with zipfile in unzGetGlobalInfo \n",err); | 168 | printf("error %d with zipfile in unzGetGlobalInfo \n",err); |
169 | printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); | 169 | printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); |
170 | printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); | 170 | printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); |
171 | for (i=0;i<gi.number_entry;i++) | 171 | for (i=0;i<gi.number_entry;i++) |
172 | { | 172 | { |
173 | char filename_inzip[256]; | 173 | char filename_inzip[256]; |
174 | unz_file_info file_info; | 174 | unz_file_info file_info; |
175 | uLong ratio=0; | 175 | uLong ratio=0; |
176 | const char *string_method; | 176 | const char *string_method; |
177 | char charCrypt=' '; | 177 | char charCrypt=' '; |
178 | err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); | 178 | err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); |
179 | if (err!=UNZ_OK) | 179 | if (err!=UNZ_OK) |
180 | { | 180 | { |
181 | printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); | 181 | printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); |
182 | break; | 182 | break; |
183 | } | 183 | } |
184 | if (file_info.uncompressed_size>0) | 184 | if (file_info.uncompressed_size>0) |
185 | ratio = (file_info.compressed_size*100)/file_info.uncompressed_size; | 185 | ratio = (file_info.compressed_size*100)/file_info.uncompressed_size; |
186 | 186 | ||
187 | /* display a '*' if the file is crypted */ | 187 | /* display a '*' if the file is crypted */ |
188 | if ((file_info.flag & 1) != 0) | 188 | if ((file_info.flag & 1) != 0) |
189 | charCrypt='*'; | 189 | charCrypt='*'; |
190 | 190 | ||
191 | if (file_info.compression_method==0) | 191 | if (file_info.compression_method==0) |
192 | string_method="Stored"; | 192 | string_method="Stored"; |
193 | else | 193 | else |
194 | if (file_info.compression_method==Z_DEFLATED) | 194 | if (file_info.compression_method==Z_DEFLATED) |
195 | { | 195 | { |
196 | uInt iLevel=(uInt)((file_info.flag & 0x6)/2); | 196 | uInt iLevel=(uInt)((file_info.flag & 0x6)/2); |
197 | if (iLevel==0) | 197 | if (iLevel==0) |
198 | string_method="Defl:N"; | 198 | string_method="Defl:N"; |
199 | else if (iLevel==1) | 199 | else if (iLevel==1) |
200 | string_method="Defl:X"; | 200 | string_method="Defl:X"; |
201 | else if ((iLevel==2) || (iLevel==3)) | 201 | else if ((iLevel==2) || (iLevel==3)) |
202 | string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ | 202 | string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ |
203 | } | 203 | } |
204 | else | 204 | else |
205 | string_method="Unkn. "; | 205 | string_method="Unkn. "; |
206 | 206 | ||
207 | printf("%7lu %6s%c%7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", | 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, | 208 | file_info.uncompressed_size,string_method, |
209 | charCrypt, | 209 | charCrypt, |
210 | file_info.compressed_size, | 210 | file_info.compressed_size, |
211 | ratio, | 211 | ratio, |
212 | (uLong)file_info.tmu_date.tm_mon + 1, | 212 | (uLong)file_info.tmu_date.tm_mon + 1, |
213 | (uLong)file_info.tmu_date.tm_mday, | 213 | (uLong)file_info.tmu_date.tm_mday, |
214 | (uLong)file_info.tmu_date.tm_year % 100, | 214 | (uLong)file_info.tmu_date.tm_year % 100, |
215 | (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min, | 215 | (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min, |
216 | (uLong)file_info.crc,filename_inzip); | 216 | (uLong)file_info.crc,filename_inzip); |
217 | if ((i+1)<gi.number_entry) | 217 | if ((i+1)<gi.number_entry) |
218 | { | 218 | { |
219 | err = unzGoToNextFile(uf); | 219 | err = unzGoToNextFile(uf); |
220 | if (err!=UNZ_OK) | 220 | if (err!=UNZ_OK) |
221 | { | 221 | { |
222 | printf("error %d with zipfile in unzGoToNextFile\n",err); | 222 | printf("error %d with zipfile in unzGoToNextFile\n",err); |
223 | break; | 223 | break; |
224 | } | 224 | } |
225 | } | 225 | } |
226 | } | 226 | } |
227 | 227 | ||
228 | return 0; | 228 | return 0; |
229 | } | 229 | } |
230 | 230 | ||
231 | 231 | ||
232 | int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password) | 232 | int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password) |
233 | unzFile uf; | 233 | unzFile uf; |
234 | const int* popt_extract_without_path; | 234 | const int* popt_extract_without_path; |
235 | int* popt_overwrite; | 235 | int* popt_overwrite; |
236 | const char* password; | 236 | const char* password; |
237 | { | 237 | { |
238 | char filename_inzip[256]; | 238 | char filename_inzip[256]; |
239 | char* filename_withoutpath; | 239 | char* filename_withoutpath; |
240 | char* p; | 240 | char* p; |
241 | int err=UNZ_OK; | 241 | int err=UNZ_OK; |
242 | FILE *fout=NULL; | 242 | FILE *fout=NULL; |
243 | void* buf; | 243 | void* buf; |
244 | uInt size_buf; | 244 | uInt size_buf; |
245 | 245 | ||
246 | unz_file_info file_info; | 246 | unz_file_info file_info; |
247 | uLong ratio=0; | 247 | uLong ratio=0; |
248 | err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); | 248 | err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); |
249 | 249 | ||
250 | if (err!=UNZ_OK) | 250 | if (err!=UNZ_OK) |
251 | { | 251 | { |
252 | printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); | 252 | printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); |
253 | return err; | 253 | return err; |
254 | } | 254 | } |
255 | 255 | ||
256 | size_buf = WRITEBUFFERSIZE; | 256 | size_buf = WRITEBUFFERSIZE; |
257 | buf = (void*)malloc(size_buf); | 257 | buf = (void*)malloc(size_buf); |
258 | if (buf==NULL) | 258 | if (buf==NULL) |
259 | { | 259 | { |
260 | printf("Error allocating memory\n"); | 260 | printf("Error allocating memory\n"); |
261 | return UNZ_INTERNALERROR; | 261 | return UNZ_INTERNALERROR; |
262 | } | 262 | } |
263 | 263 | ||
264 | p = filename_withoutpath = filename_inzip; | 264 | p = filename_withoutpath = filename_inzip; |
265 | while ((*p) != '\0') | 265 | while ((*p) != '\0') |
266 | { | 266 | { |
267 | if (((*p)=='/') || ((*p)=='\\')) | 267 | if (((*p)=='/') || ((*p)=='\\')) |
268 | filename_withoutpath = p+1; | 268 | filename_withoutpath = p+1; |
269 | p++; | 269 | p++; |
270 | } | 270 | } |
271 | 271 | ||
272 | if ((*filename_withoutpath)=='\0') | 272 | if ((*filename_withoutpath)=='\0') |
273 | { | 273 | { |
274 | if ((*popt_extract_without_path)==0) | 274 | if ((*popt_extract_without_path)==0) |
275 | { | 275 | { |
276 | printf("creating directory: %s\n",filename_inzip); | 276 | printf("creating directory: %s\n",filename_inzip); |
277 | mymkdir(filename_inzip); | 277 | mymkdir(filename_inzip); |
278 | } | 278 | } |
279 | } | 279 | } |
280 | else | 280 | else |
281 | { | 281 | { |
282 | const char* write_filename; | 282 | const char* write_filename; |
283 | int skip=0; | 283 | int skip=0; |
284 | 284 | ||
285 | if ((*popt_extract_without_path)==0) | 285 | if ((*popt_extract_without_path)==0) |
286 | write_filename = filename_inzip; | 286 | write_filename = filename_inzip; |
287 | else | 287 | else |
288 | write_filename = filename_withoutpath; | 288 | write_filename = filename_withoutpath; |
289 | 289 | ||
290 | err = unzOpenCurrentFilePassword(uf,password); | 290 | err = unzOpenCurrentFilePassword(uf,password); |
291 | if (err!=UNZ_OK) | 291 | if (err!=UNZ_OK) |
292 | { | 292 | { |
293 | printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); | 293 | printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); |
294 | } | 294 | } |
295 | 295 | ||
296 | if (((*popt_overwrite)==0) && (err==UNZ_OK)) | 296 | if (((*popt_overwrite)==0) && (err==UNZ_OK)) |
297 | { | 297 | { |
298 | char rep=0; | 298 | char rep=0; |
299 | FILE* ftestexist; | 299 | FILE* ftestexist; |
300 | ftestexist = fopen(write_filename,"rb"); | 300 | ftestexist = fopen(write_filename,"rb"); |
301 | if (ftestexist!=NULL) | 301 | if (ftestexist!=NULL) |
302 | { | 302 | { |
303 | fclose(ftestexist); | 303 | fclose(ftestexist); |
304 | do | 304 | do |
305 | { | 305 | { |
306 | char answer[128]; | 306 | char answer[128]; |
307 | printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); | 307 | printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); |
308 | scanf("%1s",answer); | 308 | scanf("%1s",answer); |
309 | rep = answer[0] ; | 309 | rep = answer[0] ; |
310 | if ((rep>='a') && (rep<='z')) | 310 | if ((rep>='a') && (rep<='z')) |
311 | rep -= 0x20; | 311 | rep -= 0x20; |
312 | } | 312 | } |
313 | while ((rep!='Y') && (rep!='N') && (rep!='A')); | 313 | while ((rep!='Y') && (rep!='N') && (rep!='A')); |
314 | } | 314 | } |
315 | 315 | ||
316 | if (rep == 'N') | 316 | if (rep == 'N') |
317 | skip = 1; | 317 | skip = 1; |
318 | 318 | ||
319 | if (rep == 'A') | 319 | if (rep == 'A') |
320 | *popt_overwrite=1; | 320 | *popt_overwrite=1; |
321 | } | 321 | } |
322 | 322 | ||
323 | if ((skip==0) && (err==UNZ_OK)) | 323 | if ((skip==0) && (err==UNZ_OK)) |
324 | { | 324 | { |
325 | fout=fopen(write_filename,"wb"); | 325 | fout=fopen(write_filename,"wb"); |
326 | 326 | ||
327 | /* some zipfile don't contain directory alone before file */ | 327 | /* some zipfile don't contain directory alone before file */ |
328 | if ((fout==NULL) && ((*popt_extract_without_path)==0) && | 328 | if ((fout==NULL) && ((*popt_extract_without_path)==0) && |
329 | (filename_withoutpath!=(char*)filename_inzip)) | 329 | (filename_withoutpath!=(char*)filename_inzip)) |
330 | { | 330 | { |
331 | char c=*(filename_withoutpath-1); | 331 | char c=*(filename_withoutpath-1); |
332 | *(filename_withoutpath-1)='\0'; | 332 | *(filename_withoutpath-1)='\0'; |
333 | makedir(write_filename); | 333 | makedir(write_filename); |
334 | *(filename_withoutpath-1)=c; | 334 | *(filename_withoutpath-1)=c; |
335 | fout=fopen(write_filename,"wb"); | 335 | fout=fopen(write_filename,"wb"); |
336 | } | 336 | } |
337 | 337 | ||
338 | if (fout==NULL) | 338 | if (fout==NULL) |
339 | { | 339 | { |
340 | printf("error opening %s\n",write_filename); | 340 | printf("error opening %s\n",write_filename); |
341 | } | 341 | } |
342 | } | 342 | } |
343 | 343 | ||
344 | if (fout!=NULL) | 344 | if (fout!=NULL) |
345 | { | 345 | { |
346 | printf(" extracting: %s\n",write_filename); | 346 | printf(" extracting: %s\n",write_filename); |
347 | 347 | ||
348 | do | 348 | do |
349 | { | 349 | { |
350 | err = unzReadCurrentFile(uf,buf,size_buf); | 350 | err = unzReadCurrentFile(uf,buf,size_buf); |
351 | if (err<0) | 351 | if (err<0) |
352 | { | 352 | { |
353 | printf("error %d with zipfile in unzReadCurrentFile\n",err); | 353 | printf("error %d with zipfile in unzReadCurrentFile\n",err); |
354 | break; | 354 | break; |
355 | } | 355 | } |
356 | if (err>0) | 356 | if (err>0) |
357 | if (fwrite(buf,err,1,fout)!=1) | 357 | if (fwrite(buf,err,1,fout)!=1) |
358 | { | 358 | { |
359 | printf("error in writing extracted file\n"); | 359 | printf("error in writing extracted file\n"); |
360 | err=UNZ_ERRNO; | 360 | err=UNZ_ERRNO; |
361 | break; | 361 | break; |
362 | } | 362 | } |
363 | } | 363 | } |
364 | while (err>0); | 364 | while (err>0); |
365 | if (fout) | 365 | if (fout) |
366 | fclose(fout); | 366 | fclose(fout); |
367 | 367 | ||
368 | if (err==0) | 368 | if (err==0) |
369 | change_file_date(write_filename,file_info.dosDate, | 369 | change_file_date(write_filename,file_info.dosDate, |
370 | file_info.tmu_date); | 370 | file_info.tmu_date); |
371 | } | 371 | } |
372 | 372 | ||
373 | if (err==UNZ_OK) | 373 | if (err==UNZ_OK) |
374 | { | 374 | { |
375 | err = unzCloseCurrentFile (uf); | 375 | err = unzCloseCurrentFile (uf); |
376 | if (err!=UNZ_OK) | 376 | if (err!=UNZ_OK) |
377 | { | 377 | { |
378 | printf("error %d with zipfile in unzCloseCurrentFile\n",err); | 378 | printf("error %d with zipfile in unzCloseCurrentFile\n",err); |
379 | } | 379 | } |
380 | } | 380 | } |
381 | else | 381 | else |
382 | unzCloseCurrentFile(uf); /* don't lose the error */ | 382 | unzCloseCurrentFile(uf); /* don't lose the error */ |
383 | } | 383 | } |
384 | 384 | ||
385 | free(buf); | 385 | free(buf); |
386 | return err; | 386 | return err; |
387 | } | 387 | } |
388 | 388 | ||
389 | 389 | ||
390 | int do_extract(uf,opt_extract_without_path,opt_overwrite,password) | 390 | int do_extract(uf,opt_extract_without_path,opt_overwrite,password) |
391 | unzFile uf; | 391 | unzFile uf; |
392 | int opt_extract_without_path; | 392 | int opt_extract_without_path; |
393 | int opt_overwrite; | 393 | int opt_overwrite; |
394 | const char* password; | 394 | const char* password; |
395 | { | 395 | { |
396 | uLong i; | 396 | uLong i; |
397 | unz_global_info gi; | 397 | unz_global_info gi; |
398 | int err; | 398 | int err; |
399 | FILE* fout=NULL; | 399 | FILE* fout=NULL; |
400 | 400 | ||
401 | err = unzGetGlobalInfo (uf,&gi); | 401 | err = unzGetGlobalInfo (uf,&gi); |
402 | if (err!=UNZ_OK) | 402 | if (err!=UNZ_OK) |
403 | printf("error %d with zipfile in unzGetGlobalInfo \n",err); | 403 | printf("error %d with zipfile in unzGetGlobalInfo \n",err); |
404 | 404 | ||
405 | for (i=0;i<gi.number_entry;i++) | 405 | for (i=0;i<gi.number_entry;i++) |
406 | { | 406 | { |
407 | if (do_extract_currentfile(uf,&opt_extract_without_path, | 407 | if (do_extract_currentfile(uf,&opt_extract_without_path, |
408 | &opt_overwrite, | 408 | &opt_overwrite, |
409 | password) != UNZ_OK) | 409 | password) != UNZ_OK) |
410 | break; | 410 | break; |
411 | 411 | ||
412 | if ((i+1)<gi.number_entry) | 412 | if ((i+1)<gi.number_entry) |
413 | { | 413 | { |
414 | err = unzGoToNextFile(uf); | 414 | err = unzGoToNextFile(uf); |
415 | if (err!=UNZ_OK) | 415 | if (err!=UNZ_OK) |
416 | { | 416 | { |
417 | printf("error %d with zipfile in unzGoToNextFile\n",err); | 417 | printf("error %d with zipfile in unzGoToNextFile\n",err); |
418 | break; | 418 | break; |
419 | } | 419 | } |
420 | } | 420 | } |
421 | } | 421 | } |
422 | 422 | ||
423 | return 0; | 423 | return 0; |
424 | } | 424 | } |
425 | 425 | ||
426 | int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password) | 426 | int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password) |
427 | unzFile uf; | 427 | unzFile uf; |
428 | const char* filename; | 428 | const char* filename; |
429 | int opt_extract_without_path; | 429 | int opt_extract_without_path; |
430 | int opt_overwrite; | 430 | int opt_overwrite; |
431 | const char* password; | 431 | const char* password; |
432 | { | 432 | { |
433 | int err = UNZ_OK; | 433 | int err = UNZ_OK; |
434 | if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK) | 434 | if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK) |
435 | { | 435 | { |
436 | printf("file %s not found in the zipfile\n",filename); | 436 | printf("file %s not found in the zipfile\n",filename); |
437 | return 2; | 437 | return 2; |
438 | } | 438 | } |
439 | 439 | ||
440 | if (do_extract_currentfile(uf,&opt_extract_without_path, | 440 | if (do_extract_currentfile(uf,&opt_extract_without_path, |
441 | &opt_overwrite, | 441 | &opt_overwrite, |
442 | password) == UNZ_OK) | 442 | password) == UNZ_OK) |
443 | return 0; | 443 | return 0; |
444 | else | 444 | else |
445 | return 1; | 445 | return 1; |
446 | } | 446 | } |
447 | 447 | ||
448 | 448 | ||
449 | int main(argc,argv) | 449 | int main(argc,argv) |
450 | int argc; | 450 | int argc; |
451 | char *argv[]; | 451 | char *argv[]; |
452 | { | 452 | { |
453 | const char *zipfilename=NULL; | 453 | const char *zipfilename=NULL; |
454 | const char *filename_to_extract=NULL; | 454 | const char *filename_to_extract=NULL; |
455 | const char *password=NULL; | 455 | const char *password=NULL; |
456 | char filename_try[MAXFILENAME+16] = ""; | 456 | char filename_try[MAXFILENAME+16] = ""; |
457 | int i; | 457 | int i; |
458 | int opt_do_list=0; | 458 | int opt_do_list=0; |
459 | int opt_do_extract=1; | 459 | int opt_do_extract=1; |
460 | int opt_do_extract_withoutpath=0; | 460 | int opt_do_extract_withoutpath=0; |
461 | int opt_overwrite=0; | 461 | int opt_overwrite=0; |
462 | unzFile uf=NULL; | 462 | unzFile uf=NULL; |
463 | 463 | ||
464 | do_banner(); | 464 | do_banner(); |
465 | if (argc==1) | 465 | if (argc==1) |
466 | { | 466 | { |
467 | do_help(); | 467 | do_help(); |
468 | return 0; | 468 | return 0; |
469 | } | 469 | } |
470 | else | 470 | else |
471 | { | 471 | { |
472 | for (i=1;i<argc;i++) | 472 | for (i=1;i<argc;i++) |
473 | { | 473 | { |
474 | if ((*argv[i])=='-') | 474 | if ((*argv[i])=='-') |
475 | { | 475 | { |
476 | const char *p=argv[i]+1; | 476 | const char *p=argv[i]+1; |
477 | 477 | ||
478 | while ((*p)!='\0') | 478 | while ((*p)!='\0') |
479 | { | 479 | { |
480 | char c=*(p++);; | 480 | char c=*(p++);; |
481 | if ((c=='l') || (c=='L')) | 481 | if ((c=='l') || (c=='L')) |
482 | opt_do_list = 1; | 482 | opt_do_list = 1; |
483 | if ((c=='v') || (c=='V')) | 483 | if ((c=='v') || (c=='V')) |
484 | opt_do_list = 1; | 484 | opt_do_list = 1; |
485 | if ((c=='x') || (c=='X')) | 485 | if ((c=='x') || (c=='X')) |
486 | opt_do_extract = 1; | 486 | opt_do_extract = 1; |
487 | if ((c=='e') || (c=='E')) | 487 | if ((c=='e') || (c=='E')) |
488 | opt_do_extract = opt_do_extract_withoutpath = 1; | 488 | opt_do_extract = opt_do_extract_withoutpath = 1; |
489 | if ((c=='o') || (c=='O')) | 489 | if ((c=='o') || (c=='O')) |
490 | opt_overwrite=1; | 490 | opt_overwrite=1; |
491 | if (((c=='p') || (c=='P')) && (i+1<argc)) | 491 | if (((c=='p') || (c=='P')) && (i+1<argc)) |
492 | { | 492 | { |
493 | password=argv[i+1]; | 493 | password=argv[i+1]; |
494 | i++; | 494 | i++; |
495 | } | 495 | } |
496 | } | 496 | } |
497 | } | 497 | } |
498 | else | 498 | else |
499 | { | 499 | { |
500 | if (zipfilename == NULL) | 500 | if (zipfilename == NULL) |
501 | zipfilename = argv[i]; | 501 | zipfilename = argv[i]; |
502 | else if (filename_to_extract==NULL) | 502 | else if (filename_to_extract==NULL) |
503 | filename_to_extract = argv[i] ; | 503 | filename_to_extract = argv[i] ; |
504 | } | 504 | } |
505 | } | 505 | } |
506 | } | 506 | } |
507 | 507 | ||
508 | if (zipfilename!=NULL) | 508 | if (zipfilename!=NULL) |
509 | { | 509 | { |
510 | 510 | ||
511 | #ifdef USEWIN32IOAPI | 511 | # ifdef USEWIN32IOAPI |
512 | zlib_filefunc_def ffunc; | 512 | zlib_filefunc_def ffunc; |
513 | #endif | 513 | # endif |
514 | 514 | ||
515 | strncpy(filename_try, zipfilename,MAXFILENAME-1); | 515 | strncpy(filename_try, zipfilename,MAXFILENAME-1); |
516 | /* strncpy doesnt append the trailing NULL, of the string is too long. */ | 516 | /* strncpy doesnt append the trailing NULL, of the string is too long. */ |
517 | filename_try[ MAXFILENAME ] = '\0'; | 517 | filename_try[ MAXFILENAME ] = '\0'; |
518 | 518 | ||
519 | #ifdef USEWIN32IOAPI | 519 | # ifdef USEWIN32IOAPI |
520 | fill_win32_filefunc(&ffunc); | 520 | fill_win32_filefunc(&ffunc); |
521 | uf = unzOpen2(zipfilename,&ffunc); | 521 | uf = unzOpen2(zipfilename,&ffunc); |
522 | #else | 522 | # else |
523 | uf = unzOpen(zipfilename); | 523 | uf = unzOpen(zipfilename); |
524 | #endif | 524 | # endif |
525 | if (uf==NULL) | 525 | if (uf==NULL) |
526 | { | 526 | { |
527 | strcat(filename_try,".zip"); | 527 | strcat(filename_try,".zip"); |
528 | #ifdef USEWIN32IOAPI | 528 | # ifdef USEWIN32IOAPI |
529 | uf = unzOpen2(filename_try,&ffunc); | 529 | uf = unzOpen2(filename_try,&ffunc); |
530 | #else | 530 | # else |
531 | uf = unzOpen(filename_try); | 531 | uf = unzOpen(filename_try); |
532 | #endif | 532 | # endif |
533 | } | 533 | } |
534 | } | 534 | } |
535 | 535 | ||
536 | if (uf==NULL) | 536 | if (uf==NULL) |
537 | { | 537 | { |
538 | printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); | 538 | printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); |
539 | return 1; | 539 | return 1; |
540 | } | 540 | } |
541 | printf("%s opened\n",filename_try); | 541 | printf("%s opened\n",filename_try); |
542 | 542 | ||
543 | if (opt_do_list==1) | 543 | if (opt_do_list==1) |
544 | return do_list(uf); | 544 | return do_list(uf); |
545 | else if (opt_do_extract==1) | 545 | else if (opt_do_extract==1) |
546 | { | 546 | { |
547 | if (filename_to_extract == NULL) | 547 | if (filename_to_extract == NULL) |
548 | return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password); | 548 | return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password); |
549 | else | 549 | else |
550 | return do_extract_onefile(uf,filename_to_extract, | 550 | return do_extract_onefile(uf,filename_to_extract, |
551 | opt_do_extract_withoutpath,opt_overwrite,password); | 551 | opt_do_extract_withoutpath,opt_overwrite,password); |
552 | } | 552 | } |
553 | unzCloseCurrentFile(uf); | 553 | unzCloseCurrentFile(uf); |
554 | 554 | ||
555 | return 0; | 555 | return 0; |
556 | } | 556 | } |