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 )
17 #ifndef __USE_FILE_OFFSET64
18 #define __USE_FILE_OFFSET64
20 #ifndef __USE_LARGEFILE64
21 #define __USE_LARGEFILE64
23 #ifndef _LARGEFILE64_SOURCE
24 #define _LARGEFILE64_SOURCE
26 #ifndef _FILE_OFFSET_BIT
27 #define _FILE_OFFSET_BIT 64
41 # include <sys/types.h>
42 # include <sys/stat.h>
57 #define WRITEBUFFERSIZE (16384)
58 #define MAXFILENAME (256)
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 */
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);
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 */
91 struct stat s; /* results of stat() */
97 char name[MAXFILENAME+1];
99 if (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)
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;
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 */
140 int check_exist_file(filename)
141 const char* filename;
145 ftestexist = fopen64(filename,"rb");
146 if (ftestexist==NULL)
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");
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" \
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;
176 FILE * fin = fopen64(filenameinzip,"rb");
177 unsigned long size_read = 0;
178 unsigned long total_read = 0;
188 size_read = (int)fread(buf,1,size_buf,fin);
189 if (size_read < size_buf)
192 printf("error in reading %s\n",filenameinzip);
197 calculate_crc = crc32(calculate_crc,buf,size_read);
198 total_read += size_read;
200 } while ((err == ZIP_OK) && (size_read>0));
205 *result_crc=calculate_crc;
206 printf("file %s crc %lx\n", filenameinzip, calculate_crc);
210 int isLargeFile(const char* filename)
214 FILE* pFile = fopen64(filename, "rb");
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)
239 int opt_compress_level=Z_DEFAULT_COMPRESSION;
240 int opt_exclude_path=0;
241 int zipfilenamearg = 0;
242 char filename_try[MAXFILENAME+16];
247 const char* password=NULL;
262 const char *p=argv[i]+1;
267 if ((c=='o') || (c=='O'))
269 if ((c=='a') || (c=='A'))
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))
285 if (zipfilenamearg == 0)
293 size_buf = WRITEBUFFERSIZE;
294 buf = (void*)malloc(size_buf);
297 printf("Error allocating memory\n");
298 return ZIP_INTERNALERROR;
301 if (zipfilenamearg==0)
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);
317 if (filename_try[i]=='.')
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)
330 if (opt_overwrite==0)
331 if (check_exist_file(filename_try)!=0)
338 printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
339 ret = scanf("%1s",answer);
345 if ((rep>='a') && (rep<='z'))
348 while ((rep!='Y') && (rep!='N') && (rep!='A'));
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);
365 zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
370 printf("error opening %s\n",filename_try);
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)))
387 const char* filenameinzip = argv[i];
388 const char *savefilenameinzip;
390 unsigned long crcFile=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;
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,
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] == '/' )
419 /*should the zip file contain any path at all?*/
420 if( opt_exclude_path )
423 const char *lastslash = 0;
424 for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
426 if( *tmpptr == '\\' || *tmpptr == '/')
431 if( lastslash != NULL )
433 savefilenameinzip = lastslash+1; // base filename follows last slash.
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);
447 printf("error in opening %s in zipfile\n",filenameinzip);
450 fin = fopen64(filenameinzip,"rb");
454 printf("error in opening %s for reading\n",filenameinzip);
462 size_read = (int)fread(buf,1,size_buf,fin);
463 if (size_read < size_buf)
466 printf("error in reading %s\n",filenameinzip);
472 err = zipWriteInFileInZip (zf,buf,size_read);
475 printf("error in writing %s in the zipfile\n",
480 } while ((err == ZIP_OK) && (size_read>0));
489 err = zipCloseFileInZip(zf);
491 printf("error in closing %s in the zipfile\n",
496 errclose = zipClose(zf,NULL);
497 if (errclose != ZIP_OK)
498 printf("error in closing %s\n",filename_try);