diff options
Diffstat (limited to 'contrib/minizip/minizip.c')
-rw-r--r-- | contrib/minizip/minizip.c | 302 |
1 files changed, 302 insertions, 0 deletions
diff --git a/contrib/minizip/minizip.c b/contrib/minizip/minizip.c new file mode 100644 index 0000000..5e492d2 --- /dev/null +++ b/contrib/minizip/minizip.c | |||
@@ -0,0 +1,302 @@ | |||
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 | # include <sys/types.h> | ||
12 | # include <sys/stat.h> | ||
13 | #else | ||
14 | # include <direct.h> | ||
15 | # include <io.h> | ||
16 | #endif | ||
17 | |||
18 | #include "zip.h" | ||
19 | |||
20 | |||
21 | #define WRITEBUFFERSIZE (16384) | ||
22 | #define MAXFILENAME (256) | ||
23 | |||
24 | #ifdef WIN32 | ||
25 | uLong filetime(f, tmzip, dt) | ||
26 | char *f; /* name of file to get info on */ | ||
27 | tm_zip *tmzip; /* return value: access, modific. and creation times */ | ||
28 | uLong *dt; /* dostime */ | ||
29 | { | ||
30 | int ret = 0; | ||
31 | { | ||
32 | FILETIME ftLocal; | ||
33 | HANDLE hFind; | ||
34 | WIN32_FIND_DATA ff32; | ||
35 | |||
36 | hFind = FindFirstFile(f,&ff32); | ||
37 | if (hFind != INVALID_HANDLE_VALUE) | ||
38 | { | ||
39 | FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); | ||
40 | FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); | ||
41 | FindClose(hFind); | ||
42 | ret = 1; | ||
43 | } | ||
44 | } | ||
45 | return ret; | ||
46 | } | ||
47 | #else | ||
48 | #ifdef unix | ||
49 | uLong filetime(f, tmzip, dt) | ||
50 | char *f; /* name of file to get info on */ | ||
51 | tm_zip *tmzip; /* return value: access, modific. and creation times */ | ||
52 | uLong *dt; /* dostime */ | ||
53 | { | ||
54 | int ret=0; | ||
55 | struct stat s; /* results of stat() */ | ||
56 | struct tm* filedate; | ||
57 | time_t tm_t=0; | ||
58 | |||
59 | if (strcmp(f,"-")!=0) | ||
60 | { | ||
61 | char name[MAXFILENAME]; | ||
62 | int len = strlen(f); | ||
63 | strcpy(name, f); | ||
64 | if (name[len - 1] == '/') | ||
65 | name[len - 1] = '\0'; | ||
66 | /* not all systems allow stat'ing a file with / appended */ | ||
67 | if (stat(name,&s)==0) | ||
68 | { | ||
69 | tm_t = s.st_mtime; | ||
70 | ret = 1; | ||
71 | } | ||
72 | } | ||
73 | filedate = localtime(&tm_t); | ||
74 | |||
75 | tmzip->tm_sec = filedate->tm_sec; | ||
76 | tmzip->tm_min = filedate->tm_min; | ||
77 | tmzip->tm_hour = filedate->tm_hour; | ||
78 | tmzip->tm_mday = filedate->tm_mday; | ||
79 | tmzip->tm_mon = filedate->tm_mon ; | ||
80 | tmzip->tm_year = filedate->tm_year; | ||
81 | |||
82 | return ret; | ||
83 | } | ||
84 | #else | ||
85 | uLong filetime(f, tmzip, dt) | ||
86 | char *f; /* name of file to get info on */ | ||
87 | tm_zip *tmzip; /* return value: access, modific. and creation times */ | ||
88 | uLong *dt; /* dostime */ | ||
89 | { | ||
90 | return 0; | ||
91 | } | ||
92 | #endif | ||
93 | #endif | ||
94 | |||
95 | |||
96 | |||
97 | |||
98 | int check_exist_file(filename) | ||
99 | const char* filename; | ||
100 | { | ||
101 | FILE* ftestexist; | ||
102 | int ret = 1; | ||
103 | ftestexist = fopen(filename,"rb"); | ||
104 | if (ftestexist==NULL) | ||
105 | ret = 0; | ||
106 | else | ||
107 | fclose(ftestexist); | ||
108 | return ret; | ||
109 | } | ||
110 | |||
111 | void do_banner() | ||
112 | { | ||
113 | printf("MiniZip 0.15, demo of zLib + Zip package written by Gilles Vollant\n"); | ||
114 | printf("more info at http://wwww.winimage/zLibDll/unzip.htm\n\n"); | ||
115 | } | ||
116 | |||
117 | void do_help() | ||
118 | { | ||
119 | printf("Usage : minizip [-o] file.zip [files_to_add]\n\n") ; | ||
120 | } | ||
121 | |||
122 | int main(argc,argv) | ||
123 | int argc; | ||
124 | char *argv[]; | ||
125 | { | ||
126 | int i; | ||
127 | int opt_overwrite=0; | ||
128 | int opt_compress_level=Z_DEFAULT_COMPRESSION; | ||
129 | int zipfilenamearg = 0; | ||
130 | char filename_try[MAXFILENAME]; | ||
131 | int zipok; | ||
132 | int err=0; | ||
133 | int size_buf=0; | ||
134 | void* buf=NULL, | ||
135 | |||
136 | |||
137 | do_banner(); | ||
138 | if (argc==1) | ||
139 | { | ||
140 | do_help(); | ||
141 | exit(0); | ||
142 | return 0; | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | for (i=1;i<argc;i++) | ||
147 | { | ||
148 | if ((*argv[i])=='-') | ||
149 | { | ||
150 | const char *p=argv[i]+1; | ||
151 | |||
152 | while ((*p)!='\0') | ||
153 | { | ||
154 | char c=*(p++);; | ||
155 | if ((c=='o') || (c=='O')) | ||
156 | opt_overwrite = 1; | ||
157 | if ((c>='0') && (c<='9')) | ||
158 | opt_compress_level = c-'0'; | ||
159 | } | ||
160 | } | ||
161 | else | ||
162 | if (zipfilenamearg == 0) | ||
163 | zipfilenamearg = i ; | ||
164 | } | ||
165 | } | ||
166 | |||
167 | size_buf = WRITEBUFFERSIZE; | ||
168 | buf = (void*)malloc(size_buf); | ||
169 | if (buf==NULL) | ||
170 | { | ||
171 | printf("Error allocating memory\n"); | ||
172 | return ZIP_INTERNALERROR; | ||
173 | } | ||
174 | |||
175 | if (zipfilenamearg==0) | ||
176 | zipok=0; | ||
177 | else | ||
178 | { | ||
179 | int i,len; | ||
180 | int dot_found=0; | ||
181 | |||
182 | zipok = 1 ; | ||
183 | strcpy(filename_try,argv[zipfilenamearg]); | ||
184 | len=strlen(filename_try); | ||
185 | for (i=0;i<len;i++) | ||
186 | if (filename_try[i]=='.') | ||
187 | dot_found=1; | ||
188 | |||
189 | if (dot_found==0) | ||
190 | strcat(filename_try,".zip"); | ||
191 | |||
192 | if (opt_overwrite==0) | ||
193 | if (check_exist_file(filename_try)!=0) | ||
194 | { | ||
195 | char rep; | ||
196 | do | ||
197 | { | ||
198 | char answer[128]; | ||
199 | printf("The file %s exist. Overwrite ? [y]es, [n]o : ",filename_try); | ||
200 | scanf("%1s",answer); | ||
201 | rep = answer[0] ; | ||
202 | if ((rep>='a') && (rep<='z')) | ||
203 | rep -= 0x20; | ||
204 | } | ||
205 | while ((rep!='Y') && (rep!='N')); | ||
206 | if (rep=='N') | ||
207 | zipok = 0; | ||
208 | } | ||
209 | } | ||
210 | |||
211 | if (zipok==1) | ||
212 | { | ||
213 | zipFile zf; | ||
214 | int errclose; | ||
215 | zf = zipOpen(filename_try,0); | ||
216 | if (zf == NULL) | ||
217 | { | ||
218 | printf("error opening %s\n",filename_try); | ||
219 | err= ZIP_ERRNO; | ||
220 | } | ||
221 | else | ||
222 | printf("creating %s\n",filename_try); | ||
223 | |||
224 | for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) | ||
225 | { | ||
226 | if (((*(argv[i]))!='-') && ((*(argv[i]))!='/')) | ||
227 | { | ||
228 | FILE * fin; | ||
229 | int size_read; | ||
230 | const char* filenameinzip = argv[i]; | ||
231 | zip_fileinfo zi; | ||
232 | |||
233 | zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = | ||
234 | zi.tmz_date.tm_mday = zi.tmz_date.tm_min = zi.tmz_date.tm_year = 0; | ||
235 | zi.dosDate = 0; | ||
236 | zi.internal_fa = 0; | ||
237 | zi.external_fa = 0; | ||
238 | filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); | ||
239 | |||
240 | |||
241 | err = zipOpenNewFileInZip(zf,filenameinzip,&zi, | ||
242 | NULL,0,NULL,0,NULL /* comment*/, | ||
243 | (opt_compress_level != 0) ? Z_DEFLATED : 0, | ||
244 | opt_compress_level); | ||
245 | |||
246 | if (err != ZIP_OK) | ||
247 | printf("error in opening %s in zipfile\n",filenameinzip); | ||
248 | else | ||
249 | { | ||
250 | fin = fopen(filenameinzip,"rb"); | ||
251 | if (fin==NULL) | ||
252 | { | ||
253 | err=ZIP_ERRNO; | ||
254 | printf("error in opening %s for reading\n",filenameinzip); | ||
255 | } | ||
256 | } | ||
257 | |||
258 | if (err == ZIP_OK) | ||
259 | do | ||
260 | { | ||
261 | err = ZIP_OK; | ||
262 | size_read = fread(buf,1,size_buf,fin); | ||
263 | if (size_read < size_buf) | ||
264 | if (feof(fin)==0) | ||
265 | { | ||
266 | printf("error in reading %s\n",filenameinzip); | ||
267 | err = ZIP_ERRNO; | ||
268 | } | ||
269 | |||
270 | if (size_read>0) | ||
271 | { | ||
272 | err = zipWriteInFileInZip (zf,buf,size_read); | ||
273 | if (err<0) | ||
274 | { | ||
275 | printf("error in writing %s in the zipfile\n", | ||
276 | filenameinzip); | ||
277 | } | ||
278 | |||
279 | } | ||
280 | } while ((err == ZIP_OK) && (size_read>0)); | ||
281 | |||
282 | fclose(fin); | ||
283 | if (err<0) | ||
284 | err=ZIP_ERRNO; | ||
285 | else | ||
286 | { | ||
287 | err = zipCloseFileInZip(zf); | ||
288 | if (err!=ZIP_OK) | ||
289 | printf("error in closing %s in the zipfile\n", | ||
290 | filenameinzip); | ||
291 | } | ||
292 | } | ||
293 | } | ||
294 | errclose = zipClose(zf,NULL); | ||
295 | if (errclose != ZIP_OK) | ||
296 | printf("error in closing %s\n",filename_try); | ||
297 | } | ||
298 | |||
299 | free(buf); | ||
300 | exit(0); | ||
301 | return 0; /* to avoid warning */ | ||
302 | } | ||