Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / zlib / contrib / minizip / minizip.c
blob7a4fa5a643ebf9ca484cc0654eb10071d16ea885
1 /*
2 minizip.c
3 Version 1.1, February 14h, 2010
4 sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
6 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
8 Modifications of Unzip for Zip64
9 Copyright (C) 2007-2008 Even Rouault
11 Modifications for Zip64 support on both zip and unzip
12 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
16 #ifndef _WIN32
17 #ifndef __USE_FILE_OFFSET64
18 #define __USE_FILE_OFFSET64
19 #endif
20 #ifndef __USE_LARGEFILE64
21 #define __USE_LARGEFILE64
22 #endif
23 #ifndef _LARGEFILE64_SOURCE
24 #define _LARGEFILE64_SOURCE
25 #endif
26 #ifndef _FILE_OFFSET_BIT
27 #define _FILE_OFFSET_BIT 64
28 #endif
29 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <errno.h>
36 #include <fcntl.h>
38 #ifdef unix
39 # include <unistd.h>
40 # include <utime.h>
41 # include <sys/types.h>
42 # include <sys/stat.h>
43 #else
44 # include <direct.h>
45 # include <io.h>
46 #endif
48 #include "zip.h"
50 #ifdef _WIN32
51 #define USEWIN32IOAPI
52 #include "iowin32.h"
53 #endif
57 #define WRITEBUFFERSIZE (16384)
58 #define MAXFILENAME (256)
60 #ifdef _WIN32
61 uLong filetime(f, tmzip, dt)
62 char *f; /* name of file to get info on */
63 tm_zip *tmzip; /* return value: access, modific. and creation times */
64 uLong *dt; /* dostime */
66 int ret = 0;
68 FILETIME ftLocal;
69 HANDLE hFind;
70 WIN32_FIND_DATAA ff32;
72 hFind = FindFirstFileA(f,&ff32);
73 if (hFind != INVALID_HANDLE_VALUE)
75 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
76 FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
77 FindClose(hFind);
78 ret = 1;
81 return ret;
83 #else
84 #ifdef unix
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 */
90 int ret=0;
91 struct stat s; /* results of stat() */
92 struct tm* filedate;
93 time_t tm_t=0;
95 if (strcmp(f,"-")!=0)
97 char name[MAXFILENAME+1];
98 int len = strlen(f);
99 if (len > MAXFILENAME)
100 len = MAXFILENAME;
102 strncpy(name, f,MAXFILENAME-1);
103 /* strncpy doesnt append the trailing NULL, of the string is too long. */
104 name[ MAXFILENAME ] = '\0';
106 if (name[len - 1] == '/')
107 name[len - 1] = '\0';
108 /* not all systems allow stat'ing a file with / appended */
109 if (stat(name,&s)==0)
111 tm_t = s.st_mtime;
112 ret = 1;
115 filedate = localtime(&tm_t);
117 tmzip->tm_sec = filedate->tm_sec;
118 tmzip->tm_min = filedate->tm_min;
119 tmzip->tm_hour = filedate->tm_hour;
120 tmzip->tm_mday = filedate->tm_mday;
121 tmzip->tm_mon = filedate->tm_mon ;
122 tmzip->tm_year = filedate->tm_year;
124 return ret;
126 #else
127 uLong filetime(f, tmzip, dt)
128 char *f; /* name of file to get info on */
129 tm_zip *tmzip; /* return value: access, modific. and creation times */
130 uLong *dt; /* dostime */
132 return 0;
134 #endif
135 #endif
140 int check_exist_file(filename)
141 const char* filename;
143 FILE* ftestexist;
144 int ret = 1;
145 ftestexist = fopen64(filename,"rb");
146 if (ftestexist==NULL)
147 ret = 0;
148 else
149 fclose(ftestexist);
150 return ret;
153 void do_banner()
155 printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
156 printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
159 void do_help()
161 printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
162 " -o Overwrite existing file.zip\n" \
163 " -a Append to existing file.zip\n" \
164 " -0 Store only\n" \
165 " -1 Compress faster\n" \
166 " -9 Compress better\n\n" \
167 " -j exclude path. store only the file name.\n\n");
170 /* calculate the CRC32 of a file,
171 because to encrypt a file, we need known the CRC32 of the file before */
172 int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
174 unsigned long calculate_crc=0;
175 int err=ZIP_OK;
176 FILE * fin = fopen64(filenameinzip,"rb");
177 unsigned long size_read = 0;
178 unsigned long total_read = 0;
179 if (fin==NULL)
181 err = ZIP_ERRNO;
184 if (err == ZIP_OK)
187 err = ZIP_OK;
188 size_read = (int)fread(buf,1,size_buf,fin);
189 if (size_read < size_buf)
190 if (feof(fin)==0)
192 printf("error in reading %s\n",filenameinzip);
193 err = ZIP_ERRNO;
196 if (size_read>0)
197 calculate_crc = crc32(calculate_crc,buf,size_read);
198 total_read += size_read;
200 } while ((err == ZIP_OK) && (size_read>0));
202 if (fin)
203 fclose(fin);
205 *result_crc=calculate_crc;
206 printf("file %s crc %lx\n", filenameinzip, calculate_crc);
207 return err;
210 int isLargeFile(const char* filename)
212 int largeFile = 0;
213 ZPOS64_T pos = 0;
214 FILE* pFile = fopen64(filename, "rb");
216 if(pFile != NULL)
218 int n = fseeko64(pFile, 0, SEEK_END);
220 pos = ftello64(pFile);
222 printf("File : %s is %lld bytes\n", filename, pos);
224 if(pos >= 0xffffffff)
225 largeFile = 1;
227 fclose(pFile);
230 return largeFile;
233 int main(argc,argv)
234 int argc;
235 char *argv[];
237 int i;
238 int opt_overwrite=0;
239 int opt_compress_level=Z_DEFAULT_COMPRESSION;
240 int opt_exclude_path=0;
241 int zipfilenamearg = 0;
242 char filename_try[MAXFILENAME+16];
243 int zipok;
244 int err=0;
245 int size_buf=0;
246 void* buf=NULL;
247 const char* password=NULL;
250 do_banner();
251 if (argc==1)
253 do_help();
254 return 0;
256 else
258 for (i=1;i<argc;i++)
260 if ((*argv[i])=='-')
262 const char *p=argv[i]+1;
264 while ((*p)!='\0')
266 char c=*(p++);;
267 if ((c=='o') || (c=='O'))
268 opt_overwrite = 1;
269 if ((c=='a') || (c=='A'))
270 opt_overwrite = 2;
271 if ((c>='0') && (c<='9'))
272 opt_compress_level = c-'0';
273 if ((c=='j') || (c=='J'))
274 opt_exclude_path = 1;
276 if (((c=='p') || (c=='P')) && (i+1<argc))
278 password=argv[i+1];
279 i++;
283 else
285 if (zipfilenamearg == 0)
287 zipfilenamearg = i ;
293 size_buf = WRITEBUFFERSIZE;
294 buf = (void*)malloc(size_buf);
295 if (buf==NULL)
297 printf("Error allocating memory\n");
298 return ZIP_INTERNALERROR;
301 if (zipfilenamearg==0)
303 zipok=0;
305 else
307 int i,len;
308 int dot_found=0;
310 zipok = 1 ;
311 strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
312 /* strncpy doesnt append the trailing NULL, of the string is too long. */
313 filename_try[ MAXFILENAME ] = '\0';
315 len=(int)strlen(filename_try);
316 for (i=0;i<len;i++)
317 if (filename_try[i]=='.')
318 dot_found=1;
320 if (dot_found==0)
321 strcat(filename_try,".zip");
323 if (opt_overwrite==2)
325 /* if the file don't exist, we not append file */
326 if (check_exist_file(filename_try)==0)
327 opt_overwrite=1;
329 else
330 if (opt_overwrite==0)
331 if (check_exist_file(filename_try)!=0)
333 char rep=0;
336 char answer[128];
337 int ret;
338 printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
339 ret = scanf("%1s",answer);
340 if (ret != 1)
342 exit(EXIT_FAILURE);
344 rep = answer[0] ;
345 if ((rep>='a') && (rep<='z'))
346 rep -= 0x20;
348 while ((rep!='Y') && (rep!='N') && (rep!='A'));
349 if (rep=='N')
350 zipok = 0;
351 if (rep=='A')
352 opt_overwrite = 2;
356 if (zipok==1)
358 zipFile zf;
359 int errclose;
360 # ifdef USEWIN32IOAPI
361 zlib_filefunc64_def ffunc;
362 fill_win32_filefunc64A(&ffunc);
363 zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
364 # else
365 zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
366 # endif
368 if (zf == NULL)
370 printf("error opening %s\n",filename_try);
371 err= ZIP_ERRNO;
373 else
374 printf("creating %s\n",filename_try);
376 for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
378 if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
379 ((argv[i][1]=='o') || (argv[i][1]=='O') ||
380 (argv[i][1]=='a') || (argv[i][1]=='A') ||
381 (argv[i][1]=='p') || (argv[i][1]=='P') ||
382 ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
383 (strlen(argv[i]) == 2)))
385 FILE * fin;
386 int size_read;
387 const char* filenameinzip = argv[i];
388 const char *savefilenameinzip;
389 zip_fileinfo zi;
390 unsigned long crcFile=0;
391 int zip64 = 0;
393 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
394 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
395 zi.dosDate = 0;
396 zi.internal_fa = 0;
397 zi.external_fa = 0;
398 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
401 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
402 NULL,0,NULL,0,NULL / * comment * /,
403 (opt_compress_level != 0) ? Z_DEFLATED : 0,
404 opt_compress_level);
406 if ((password != NULL) && (err==ZIP_OK))
407 err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
409 zip64 = isLargeFile(filenameinzip);
411 /* The path name saved, should not include a leading slash. */
412 /*if it did, windows/xp and dynazip couldn't read the zip file. */
413 savefilenameinzip = filenameinzip;
414 while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
416 savefilenameinzip++;
419 /*should the zip file contain any path at all?*/
420 if( opt_exclude_path )
422 const char *tmpptr;
423 const char *lastslash = 0;
424 for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
426 if( *tmpptr == '\\' || *tmpptr == '/')
428 lastslash = tmpptr;
431 if( lastslash != NULL )
433 savefilenameinzip = lastslash+1; // base filename follows last slash.
437 /**/
438 err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
439 NULL,0,NULL,0,NULL /* comment*/,
440 (opt_compress_level != 0) ? Z_DEFLATED : 0,
441 opt_compress_level,0,
442 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
443 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
444 password,crcFile, zip64);
446 if (err != ZIP_OK)
447 printf("error in opening %s in zipfile\n",filenameinzip);
448 else
450 fin = fopen64(filenameinzip,"rb");
451 if (fin==NULL)
453 err=ZIP_ERRNO;
454 printf("error in opening %s for reading\n",filenameinzip);
458 if (err == ZIP_OK)
461 err = ZIP_OK;
462 size_read = (int)fread(buf,1,size_buf,fin);
463 if (size_read < size_buf)
464 if (feof(fin)==0)
466 printf("error in reading %s\n",filenameinzip);
467 err = ZIP_ERRNO;
470 if (size_read>0)
472 err = zipWriteInFileInZip (zf,buf,size_read);
473 if (err<0)
475 printf("error in writing %s in the zipfile\n",
476 filenameinzip);
480 } while ((err == ZIP_OK) && (size_read>0));
482 if (fin)
483 fclose(fin);
485 if (err<0)
486 err=ZIP_ERRNO;
487 else
489 err = zipCloseFileInZip(zf);
490 if (err!=ZIP_OK)
491 printf("error in closing %s in the zipfile\n",
492 filenameinzip);
496 errclose = zipClose(zf,NULL);
497 if (errclose != ZIP_OK)
498 printf("error in closing %s\n",filename_try);
500 else
502 do_help();
505 free(buf);
506 return 0;