1*a1d25298Schristos /*
2*a1d25298Schristos    minizip.c
3*a1d25298Schristos    Version 1.1, February 14h, 2010
4*a1d25298Schristos    sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
5*a1d25298Schristos 
6*a1d25298Schristos          Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
7*a1d25298Schristos 
8*a1d25298Schristos          Modifications of Unzip for Zip64
9*a1d25298Schristos          Copyright (C) 2007-2008 Even Rouault
10*a1d25298Schristos 
11*a1d25298Schristos          Modifications for Zip64 support on both zip and unzip
12*a1d25298Schristos          Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
13*a1d25298Schristos */
14*a1d25298Schristos 
15*a1d25298Schristos 
16*a1d25298Schristos #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
17*a1d25298Schristos         #ifndef __USE_FILE_OFFSET64
18*a1d25298Schristos                 #define __USE_FILE_OFFSET64
19*a1d25298Schristos         #endif
20*a1d25298Schristos         #ifndef __USE_LARGEFILE64
21*a1d25298Schristos                 #define __USE_LARGEFILE64
22*a1d25298Schristos         #endif
23*a1d25298Schristos         #ifndef _LARGEFILE64_SOURCE
24*a1d25298Schristos                 #define _LARGEFILE64_SOURCE
25*a1d25298Schristos         #endif
26*a1d25298Schristos         #ifndef _FILE_OFFSET_BIT
27*a1d25298Schristos                 #define _FILE_OFFSET_BIT 64
28*a1d25298Schristos         #endif
29*a1d25298Schristos #endif
30*a1d25298Schristos 
31*a1d25298Schristos #ifdef __APPLE__
32*a1d25298Schristos // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
33*a1d25298Schristos #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
34*a1d25298Schristos #define FTELLO_FUNC(stream) ftello(stream)
35*a1d25298Schristos #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
36*a1d25298Schristos #else
37*a1d25298Schristos #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
38*a1d25298Schristos #define FTELLO_FUNC(stream) ftello64(stream)
39*a1d25298Schristos #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
40*a1d25298Schristos #endif
41*a1d25298Schristos 
42*a1d25298Schristos 
43*a1d25298Schristos 
44*a1d25298Schristos #include <stdio.h>
45*a1d25298Schristos #include <stdlib.h>
46*a1d25298Schristos #include <string.h>
47*a1d25298Schristos #include <time.h>
48*a1d25298Schristos #include <errno.h>
49*a1d25298Schristos #include <fcntl.h>
50*a1d25298Schristos 
51*a1d25298Schristos #ifdef _WIN32
52*a1d25298Schristos # include <direct.h>
53*a1d25298Schristos # include <io.h>
54*a1d25298Schristos #else
55*a1d25298Schristos # include <unistd.h>
56*a1d25298Schristos # include <utime.h>
57*a1d25298Schristos # include <sys/types.h>
58*a1d25298Schristos # include <sys/stat.h>
59*a1d25298Schristos #endif
60*a1d25298Schristos 
61*a1d25298Schristos #include "zip.h"
62*a1d25298Schristos 
63*a1d25298Schristos #ifdef _WIN32
64*a1d25298Schristos         #define USEWIN32IOAPI
65*a1d25298Schristos         #include "iowin32.h"
66*a1d25298Schristos #endif
67*a1d25298Schristos 
68*a1d25298Schristos 
69*a1d25298Schristos 
70*a1d25298Schristos #define WRITEBUFFERSIZE (16384)
71*a1d25298Schristos #define MAXFILENAME (256)
72*a1d25298Schristos 
73*a1d25298Schristos #ifdef _WIN32
filetime(f,tmzip,dt)74*a1d25298Schristos uLong filetime(f, tmzip, dt)
75*a1d25298Schristos     char *f;                /* name of file to get info on */
76*a1d25298Schristos     tm_zip *tmzip;             /* return value: access, modific. and creation times */
77*a1d25298Schristos     uLong *dt;             /* dostime */
78*a1d25298Schristos {
79*a1d25298Schristos   int ret = 0;
80*a1d25298Schristos   {
81*a1d25298Schristos       FILETIME ftLocal;
82*a1d25298Schristos       HANDLE hFind;
83*a1d25298Schristos       WIN32_FIND_DATAA ff32;
84*a1d25298Schristos 
85*a1d25298Schristos       hFind = FindFirstFileA(f,&ff32);
86*a1d25298Schristos       if (hFind != INVALID_HANDLE_VALUE)
87*a1d25298Schristos       {
88*a1d25298Schristos         FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
89*a1d25298Schristos         FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
90*a1d25298Schristos         FindClose(hFind);
91*a1d25298Schristos         ret = 1;
92*a1d25298Schristos       }
93*a1d25298Schristos   }
94*a1d25298Schristos   return ret;
95*a1d25298Schristos }
96*a1d25298Schristos #else
97*a1d25298Schristos #ifdef unix || __APPLE__
filetime(f,tmzip,dt)98*a1d25298Schristos uLong filetime(f, tmzip, dt)
99*a1d25298Schristos     char *f;               /* name of file to get info on */
100*a1d25298Schristos     tm_zip *tmzip;         /* return value: access, modific. and creation times */
101*a1d25298Schristos     uLong *dt;             /* dostime */
102*a1d25298Schristos {
103*a1d25298Schristos   int ret=0;
104*a1d25298Schristos   struct stat s;        /* results of stat() */
105*a1d25298Schristos   struct tm* filedate;
106*a1d25298Schristos   time_t tm_t=0;
107*a1d25298Schristos 
108*a1d25298Schristos   if (strcmp(f,"-")!=0)
109*a1d25298Schristos   {
110*a1d25298Schristos     char name[MAXFILENAME+1];
111*a1d25298Schristos     int len = strlen(f);
112*a1d25298Schristos     if (len > MAXFILENAME)
113*a1d25298Schristos       len = MAXFILENAME;
114*a1d25298Schristos 
115*a1d25298Schristos     strncpy(name, f,MAXFILENAME-1);
116*a1d25298Schristos     /* strncpy doesnt append the trailing NULL, of the string is too long. */
117*a1d25298Schristos     name[ MAXFILENAME ] = '\0';
118*a1d25298Schristos 
119*a1d25298Schristos     if (name[len - 1] == '/')
120*a1d25298Schristos       name[len - 1] = '\0';
121*a1d25298Schristos     /* not all systems allow stat'ing a file with / appended */
122*a1d25298Schristos     if (stat(name,&s)==0)
123*a1d25298Schristos     {
124*a1d25298Schristos       tm_t = s.st_mtime;
125*a1d25298Schristos       ret = 1;
126*a1d25298Schristos     }
127*a1d25298Schristos   }
128*a1d25298Schristos   filedate = localtime(&tm_t);
129*a1d25298Schristos 
130*a1d25298Schristos   tmzip->tm_sec  = filedate->tm_sec;
131*a1d25298Schristos   tmzip->tm_min  = filedate->tm_min;
132*a1d25298Schristos   tmzip->tm_hour = filedate->tm_hour;
133*a1d25298Schristos   tmzip->tm_mday = filedate->tm_mday;
134*a1d25298Schristos   tmzip->tm_mon  = filedate->tm_mon ;
135*a1d25298Schristos   tmzip->tm_year = filedate->tm_year;
136*a1d25298Schristos 
137*a1d25298Schristos   return ret;
138*a1d25298Schristos }
139*a1d25298Schristos #else
filetime(f,tmzip,dt)140*a1d25298Schristos uLong filetime(f, tmzip, dt)
141*a1d25298Schristos     char *f;                /* name of file to get info on */
142*a1d25298Schristos     tm_zip *tmzip;             /* return value: access, modific. and creation times */
143*a1d25298Schristos     uLong *dt;             /* dostime */
144*a1d25298Schristos {
145*a1d25298Schristos     return 0;
146*a1d25298Schristos }
147*a1d25298Schristos #endif
148*a1d25298Schristos #endif
149*a1d25298Schristos 
150*a1d25298Schristos 
151*a1d25298Schristos 
152*a1d25298Schristos 
check_exist_file(filename)153*a1d25298Schristos int check_exist_file(filename)
154*a1d25298Schristos     const char* filename;
155*a1d25298Schristos {
156*a1d25298Schristos     FILE* ftestexist;
157*a1d25298Schristos     int ret = 1;
158*a1d25298Schristos     ftestexist = FOPEN_FUNC(filename,"rb");
159*a1d25298Schristos     if (ftestexist==NULL)
160*a1d25298Schristos         ret = 0;
161*a1d25298Schristos     else
162*a1d25298Schristos         fclose(ftestexist);
163*a1d25298Schristos     return ret;
164*a1d25298Schristos }
165*a1d25298Schristos 
do_banner()166*a1d25298Schristos void do_banner()
167*a1d25298Schristos {
168*a1d25298Schristos     printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
169*a1d25298Schristos     printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
170*a1d25298Schristos }
171*a1d25298Schristos 
do_help()172*a1d25298Schristos void do_help()
173*a1d25298Schristos {
174*a1d25298Schristos     printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
175*a1d25298Schristos            "  -o  Overwrite existing file.zip\n" \
176*a1d25298Schristos            "  -a  Append to existing file.zip\n" \
177*a1d25298Schristos            "  -0  Store only\n" \
178*a1d25298Schristos            "  -1  Compress faster\n" \
179*a1d25298Schristos            "  -9  Compress better\n\n" \
180*a1d25298Schristos            "  -j  exclude path. store only the file name.\n\n");
181*a1d25298Schristos }
182*a1d25298Schristos 
183*a1d25298Schristos /* calculate the CRC32 of a file,
184*a1d25298Schristos    because to encrypt a file, we need known the CRC32 of the file before */
getFileCrc(const char * filenameinzip,void * buf,unsigned long size_buf,unsigned long * result_crc)185*a1d25298Schristos int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
186*a1d25298Schristos {
187*a1d25298Schristos    unsigned long calculate_crc=0;
188*a1d25298Schristos    int err=ZIP_OK;
189*a1d25298Schristos    FILE * fin = FOPEN_FUNC(filenameinzip,"rb");
190*a1d25298Schristos 
191*a1d25298Schristos    unsigned long size_read = 0;
192*a1d25298Schristos    unsigned long total_read = 0;
193*a1d25298Schristos    if (fin==NULL)
194*a1d25298Schristos    {
195*a1d25298Schristos        err = ZIP_ERRNO;
196*a1d25298Schristos    }
197*a1d25298Schristos 
198*a1d25298Schristos     if (err == ZIP_OK)
199*a1d25298Schristos         do
200*a1d25298Schristos         {
201*a1d25298Schristos             err = ZIP_OK;
202*a1d25298Schristos             size_read = (int)fread(buf,1,size_buf,fin);
203*a1d25298Schristos             if (size_read < size_buf)
204*a1d25298Schristos                 if (feof(fin)==0)
205*a1d25298Schristos             {
206*a1d25298Schristos                 printf("error in reading %s\n",filenameinzip);
207*a1d25298Schristos                 err = ZIP_ERRNO;
208*a1d25298Schristos             }
209*a1d25298Schristos 
210*a1d25298Schristos             if (size_read>0)
211*a1d25298Schristos                 calculate_crc = crc32(calculate_crc,buf,size_read);
212*a1d25298Schristos             total_read += size_read;
213*a1d25298Schristos 
214*a1d25298Schristos         } while ((err == ZIP_OK) && (size_read>0));
215*a1d25298Schristos 
216*a1d25298Schristos     if (fin)
217*a1d25298Schristos         fclose(fin);
218*a1d25298Schristos 
219*a1d25298Schristos     *result_crc=calculate_crc;
220*a1d25298Schristos     printf("file %s crc %lx\n", filenameinzip, calculate_crc);
221*a1d25298Schristos     return err;
222*a1d25298Schristos }
223*a1d25298Schristos 
isLargeFile(const char * filename)224*a1d25298Schristos int isLargeFile(const char* filename)
225*a1d25298Schristos {
226*a1d25298Schristos   int largeFile = 0;
227*a1d25298Schristos   ZPOS64_T pos = 0;
228*a1d25298Schristos   FILE* pFile = FOPEN_FUNC(filename, "rb");
229*a1d25298Schristos 
230*a1d25298Schristos   if(pFile != NULL)
231*a1d25298Schristos   {
232*a1d25298Schristos     int n = FSEEKO_FUNC(pFile, 0, SEEK_END);
233*a1d25298Schristos     pos = FTELLO_FUNC(pFile);
234*a1d25298Schristos 
235*a1d25298Schristos                 printf("File : %s is %lld bytes\n", filename, pos);
236*a1d25298Schristos 
237*a1d25298Schristos     if(pos >= 0xffffffff)
238*a1d25298Schristos      largeFile = 1;
239*a1d25298Schristos 
240*a1d25298Schristos                 fclose(pFile);
241*a1d25298Schristos   }
242*a1d25298Schristos 
243*a1d25298Schristos  return largeFile;
244*a1d25298Schristos }
245*a1d25298Schristos 
main(argc,argv)246*a1d25298Schristos int main(argc,argv)
247*a1d25298Schristos     int argc;
248*a1d25298Schristos     char *argv[];
249*a1d25298Schristos {
250*a1d25298Schristos     int i;
251*a1d25298Schristos     int opt_overwrite=0;
252*a1d25298Schristos     int opt_compress_level=Z_DEFAULT_COMPRESSION;
253*a1d25298Schristos     int opt_exclude_path=0;
254*a1d25298Schristos     int zipfilenamearg = 0;
255*a1d25298Schristos     char filename_try[MAXFILENAME+16];
256*a1d25298Schristos     int zipok;
257*a1d25298Schristos     int err=0;
258*a1d25298Schristos     int size_buf=0;
259*a1d25298Schristos     void* buf=NULL;
260*a1d25298Schristos     const char* password=NULL;
261*a1d25298Schristos 
262*a1d25298Schristos 
263*a1d25298Schristos     do_banner();
264*a1d25298Schristos     if (argc==1)
265*a1d25298Schristos     {
266*a1d25298Schristos         do_help();
267*a1d25298Schristos         return 0;
268*a1d25298Schristos     }
269*a1d25298Schristos     else
270*a1d25298Schristos     {
271*a1d25298Schristos         for (i=1;i<argc;i++)
272*a1d25298Schristos         {
273*a1d25298Schristos             if ((*argv[i])=='-')
274*a1d25298Schristos             {
275*a1d25298Schristos                 const char *p=argv[i]+1;
276*a1d25298Schristos 
277*a1d25298Schristos                 while ((*p)!='\0')
278*a1d25298Schristos                 {
279*a1d25298Schristos                     char c=*(p++);;
280*a1d25298Schristos                     if ((c=='o') || (c=='O'))
281*a1d25298Schristos                         opt_overwrite = 1;
282*a1d25298Schristos                     if ((c=='a') || (c=='A'))
283*a1d25298Schristos                         opt_overwrite = 2;
284*a1d25298Schristos                     if ((c>='0') && (c<='9'))
285*a1d25298Schristos                         opt_compress_level = c-'0';
286*a1d25298Schristos                     if ((c=='j') || (c=='J'))
287*a1d25298Schristos                         opt_exclude_path = 1;
288*a1d25298Schristos 
289*a1d25298Schristos                     if (((c=='p') || (c=='P')) && (i+1<argc))
290*a1d25298Schristos                     {
291*a1d25298Schristos                         password=argv[i+1];
292*a1d25298Schristos                         i++;
293*a1d25298Schristos                     }
294*a1d25298Schristos                 }
295*a1d25298Schristos             }
296*a1d25298Schristos             else
297*a1d25298Schristos             {
298*a1d25298Schristos                 if (zipfilenamearg == 0)
299*a1d25298Schristos                 {
300*a1d25298Schristos                     zipfilenamearg = i ;
301*a1d25298Schristos                 }
302*a1d25298Schristos             }
303*a1d25298Schristos         }
304*a1d25298Schristos     }
305*a1d25298Schristos 
306*a1d25298Schristos     size_buf = WRITEBUFFERSIZE;
307*a1d25298Schristos     buf = (void*)malloc(size_buf);
308*a1d25298Schristos     if (buf==NULL)
309*a1d25298Schristos     {
310*a1d25298Schristos         printf("Error allocating memory\n");
311*a1d25298Schristos         return ZIP_INTERNALERROR;
312*a1d25298Schristos     }
313*a1d25298Schristos 
314*a1d25298Schristos     if (zipfilenamearg==0)
315*a1d25298Schristos     {
316*a1d25298Schristos         zipok=0;
317*a1d25298Schristos     }
318*a1d25298Schristos     else
319*a1d25298Schristos     {
320*a1d25298Schristos         int i,len;
321*a1d25298Schristos         int dot_found=0;
322*a1d25298Schristos 
323*a1d25298Schristos         zipok = 1 ;
324*a1d25298Schristos         strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
325*a1d25298Schristos         /* strncpy doesnt append the trailing NULL, of the string is too long. */
326*a1d25298Schristos         filename_try[ MAXFILENAME ] = '\0';
327*a1d25298Schristos 
328*a1d25298Schristos         len=(int)strlen(filename_try);
329*a1d25298Schristos         for (i=0;i<len;i++)
330*a1d25298Schristos             if (filename_try[i]=='.')
331*a1d25298Schristos                 dot_found=1;
332*a1d25298Schristos 
333*a1d25298Schristos         if (dot_found==0)
334*a1d25298Schristos             strcat(filename_try,".zip");
335*a1d25298Schristos 
336*a1d25298Schristos         if (opt_overwrite==2)
337*a1d25298Schristos         {
338*a1d25298Schristos             /* if the file don't exist, we not append file */
339*a1d25298Schristos             if (check_exist_file(filename_try)==0)
340*a1d25298Schristos                 opt_overwrite=1;
341*a1d25298Schristos         }
342*a1d25298Schristos         else
343*a1d25298Schristos         if (opt_overwrite==0)
344*a1d25298Schristos             if (check_exist_file(filename_try)!=0)
345*a1d25298Schristos             {
346*a1d25298Schristos                 char rep=0;
347*a1d25298Schristos                 do
348*a1d25298Schristos                 {
349*a1d25298Schristos                     char answer[128];
350*a1d25298Schristos                     int ret;
351*a1d25298Schristos                     printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
352*a1d25298Schristos                     ret = scanf("%1s",answer);
353*a1d25298Schristos                     if (ret != 1)
354*a1d25298Schristos                     {
355*a1d25298Schristos                        exit(EXIT_FAILURE);
356*a1d25298Schristos                     }
357*a1d25298Schristos                     rep = answer[0] ;
358*a1d25298Schristos                     if ((rep>='a') && (rep<='z'))
359*a1d25298Schristos                         rep -= 0x20;
360*a1d25298Schristos                 }
361*a1d25298Schristos                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
362*a1d25298Schristos                 if (rep=='N')
363*a1d25298Schristos                     zipok = 0;
364*a1d25298Schristos                 if (rep=='A')
365*a1d25298Schristos                     opt_overwrite = 2;
366*a1d25298Schristos             }
367*a1d25298Schristos     }
368*a1d25298Schristos 
369*a1d25298Schristos     if (zipok==1)
370*a1d25298Schristos     {
371*a1d25298Schristos         zipFile zf;
372*a1d25298Schristos         int errclose;
373*a1d25298Schristos #        ifdef USEWIN32IOAPI
374*a1d25298Schristos         zlib_filefunc64_def ffunc;
375*a1d25298Schristos         fill_win32_filefunc64A(&ffunc);
376*a1d25298Schristos         zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
377*a1d25298Schristos #        else
378*a1d25298Schristos         zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
379*a1d25298Schristos #        endif
380*a1d25298Schristos 
381*a1d25298Schristos         if (zf == NULL)
382*a1d25298Schristos         {
383*a1d25298Schristos             printf("error opening %s\n",filename_try);
384*a1d25298Schristos             err= ZIP_ERRNO;
385*a1d25298Schristos         }
386*a1d25298Schristos         else
387*a1d25298Schristos             printf("creating %s\n",filename_try);
388*a1d25298Schristos 
389*a1d25298Schristos         for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
390*a1d25298Schristos         {
391*a1d25298Schristos             if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
392*a1d25298Schristos                   ((argv[i][1]=='o') || (argv[i][1]=='O') ||
393*a1d25298Schristos                    (argv[i][1]=='a') || (argv[i][1]=='A') ||
394*a1d25298Schristos                    (argv[i][1]=='p') || (argv[i][1]=='P') ||
395*a1d25298Schristos                    ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
396*a1d25298Schristos                   (strlen(argv[i]) == 2)))
397*a1d25298Schristos             {
398*a1d25298Schristos                 FILE * fin;
399*a1d25298Schristos                 int size_read;
400*a1d25298Schristos                 const char* filenameinzip = argv[i];
401*a1d25298Schristos                 const char *savefilenameinzip;
402*a1d25298Schristos                 zip_fileinfo zi;
403*a1d25298Schristos                 unsigned long crcFile=0;
404*a1d25298Schristos                 int zip64 = 0;
405*a1d25298Schristos 
406*a1d25298Schristos                 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
407*a1d25298Schristos                 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
408*a1d25298Schristos                 zi.dosDate = 0;
409*a1d25298Schristos                 zi.internal_fa = 0;
410*a1d25298Schristos                 zi.external_fa = 0;
411*a1d25298Schristos                 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
412*a1d25298Schristos 
413*a1d25298Schristos /*
414*a1d25298Schristos                 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
415*a1d25298Schristos                                  NULL,0,NULL,0,NULL / * comment * /,
416*a1d25298Schristos                                  (opt_compress_level != 0) ? Z_DEFLATED : 0,
417*a1d25298Schristos                                  opt_compress_level);
418*a1d25298Schristos */
419*a1d25298Schristos                 if ((password != NULL) && (err==ZIP_OK))
420*a1d25298Schristos                     err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
421*a1d25298Schristos 
422*a1d25298Schristos                 zip64 = isLargeFile(filenameinzip);
423*a1d25298Schristos 
424*a1d25298Schristos                                                          /* The path name saved, should not include a leading slash. */
425*a1d25298Schristos                /*if it did, windows/xp and dynazip couldn't read the zip file. */
426*a1d25298Schristos                  savefilenameinzip = filenameinzip;
427*a1d25298Schristos                  while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
428*a1d25298Schristos                  {
429*a1d25298Schristos                      savefilenameinzip++;
430*a1d25298Schristos                  }
431*a1d25298Schristos 
432*a1d25298Schristos                  /*should the zip file contain any path at all?*/
433*a1d25298Schristos                  if( opt_exclude_path )
434*a1d25298Schristos                  {
435*a1d25298Schristos                      const char *tmpptr;
436*a1d25298Schristos                      const char *lastslash = 0;
437*a1d25298Schristos                      for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
438*a1d25298Schristos                      {
439*a1d25298Schristos                          if( *tmpptr == '\\' || *tmpptr == '/')
440*a1d25298Schristos                          {
441*a1d25298Schristos                              lastslash = tmpptr;
442*a1d25298Schristos                          }
443*a1d25298Schristos                      }
444*a1d25298Schristos                      if( lastslash != NULL )
445*a1d25298Schristos                      {
446*a1d25298Schristos                          savefilenameinzip = lastslash+1; // base filename follows last slash.
447*a1d25298Schristos                      }
448*a1d25298Schristos                  }
449*a1d25298Schristos 
450*a1d25298Schristos                  /**/
451*a1d25298Schristos                 err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
452*a1d25298Schristos                                  NULL,0,NULL,0,NULL /* comment*/,
453*a1d25298Schristos                                  (opt_compress_level != 0) ? Z_DEFLATED : 0,
454*a1d25298Schristos                                  opt_compress_level,0,
455*a1d25298Schristos                                  /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
456*a1d25298Schristos                                  -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
457*a1d25298Schristos                                  password,crcFile, zip64);
458*a1d25298Schristos 
459*a1d25298Schristos                 if (err != ZIP_OK)
460*a1d25298Schristos                     printf("error in opening %s in zipfile\n",filenameinzip);
461*a1d25298Schristos                 else
462*a1d25298Schristos                 {
463*a1d25298Schristos                     fin = FOPEN_FUNC(filenameinzip,"rb");
464*a1d25298Schristos                     if (fin==NULL)
465*a1d25298Schristos                     {
466*a1d25298Schristos                         err=ZIP_ERRNO;
467*a1d25298Schristos                         printf("error in opening %s for reading\n",filenameinzip);
468*a1d25298Schristos                     }
469*a1d25298Schristos                 }
470*a1d25298Schristos 
471*a1d25298Schristos                 if (err == ZIP_OK)
472*a1d25298Schristos                     do
473*a1d25298Schristos                     {
474*a1d25298Schristos                         err = ZIP_OK;
475*a1d25298Schristos                         size_read = (int)fread(buf,1,size_buf,fin);
476*a1d25298Schristos                         if (size_read < size_buf)
477*a1d25298Schristos                             if (feof(fin)==0)
478*a1d25298Schristos                         {
479*a1d25298Schristos                             printf("error in reading %s\n",filenameinzip);
480*a1d25298Schristos                             err = ZIP_ERRNO;
481*a1d25298Schristos                         }
482*a1d25298Schristos 
483*a1d25298Schristos                         if (size_read>0)
484*a1d25298Schristos                         {
485*a1d25298Schristos                             err = zipWriteInFileInZip (zf,buf,size_read);
486*a1d25298Schristos                             if (err<0)
487*a1d25298Schristos                             {
488*a1d25298Schristos                                 printf("error in writing %s in the zipfile\n",
489*a1d25298Schristos                                                  filenameinzip);
490*a1d25298Schristos                             }
491*a1d25298Schristos 
492*a1d25298Schristos                         }
493*a1d25298Schristos                     } while ((err == ZIP_OK) && (size_read>0));
494*a1d25298Schristos 
495*a1d25298Schristos                 if (fin)
496*a1d25298Schristos                     fclose(fin);
497*a1d25298Schristos 
498*a1d25298Schristos                 if (err<0)
499*a1d25298Schristos                     err=ZIP_ERRNO;
500*a1d25298Schristos                 else
501*a1d25298Schristos                 {
502*a1d25298Schristos                     err = zipCloseFileInZip(zf);
503*a1d25298Schristos                     if (err!=ZIP_OK)
504*a1d25298Schristos                         printf("error in closing %s in the zipfile\n",
505*a1d25298Schristos                                     filenameinzip);
506*a1d25298Schristos                 }
507*a1d25298Schristos             }
508*a1d25298Schristos         }
509*a1d25298Schristos         errclose = zipClose(zf,NULL);
510*a1d25298Schristos         if (errclose != ZIP_OK)
511*a1d25298Schristos             printf("error in closing %s\n",filename_try);
512*a1d25298Schristos     }
513*a1d25298Schristos     else
514*a1d25298Schristos     {
515*a1d25298Schristos        do_help();
516*a1d25298Schristos     }
517*a1d25298Schristos 
518*a1d25298Schristos     free(buf);
519*a1d25298Schristos     return 0;
520*a1d25298Schristos }
521