xref: /minix3/common/dist/zlib/contrib/minizip/minizip.c (revision 44bedb31d842b4b0444105519bcf929a69fe2dc1)
1*44bedb31SLionel Sambuc /*	$NetBSD: minizip.c,v 1.1.1.1 2006/01/14 20:10:58 christos Exp $	*/
2*44bedb31SLionel Sambuc 
3*44bedb31SLionel Sambuc /*
4*44bedb31SLionel Sambuc    minizip.c
5*44bedb31SLionel Sambuc    Version 1.01e, February 12th, 2005
6*44bedb31SLionel Sambuc 
7*44bedb31SLionel Sambuc    Copyright (C) 1998-2005 Gilles Vollant
8*44bedb31SLionel Sambuc */
9*44bedb31SLionel Sambuc 
10*44bedb31SLionel Sambuc #include <stdio.h>
11*44bedb31SLionel Sambuc #include <stdlib.h>
12*44bedb31SLionel Sambuc #include <string.h>
13*44bedb31SLionel Sambuc #include <time.h>
14*44bedb31SLionel Sambuc #include <errno.h>
15*44bedb31SLionel Sambuc #include <fcntl.h>
16*44bedb31SLionel Sambuc 
17*44bedb31SLionel Sambuc #ifdef unix
18*44bedb31SLionel Sambuc # include <unistd.h>
19*44bedb31SLionel Sambuc # include <utime.h>
20*44bedb31SLionel Sambuc # include <sys/types.h>
21*44bedb31SLionel Sambuc # include <sys/stat.h>
22*44bedb31SLionel Sambuc #else
23*44bedb31SLionel Sambuc # include <direct.h>
24*44bedb31SLionel Sambuc # include <io.h>
25*44bedb31SLionel Sambuc #endif
26*44bedb31SLionel Sambuc 
27*44bedb31SLionel Sambuc #include "zip.h"
28*44bedb31SLionel Sambuc 
29*44bedb31SLionel Sambuc #ifdef WIN32
30*44bedb31SLionel Sambuc #define USEWIN32IOAPI
31*44bedb31SLionel Sambuc #include "iowin32.h"
32*44bedb31SLionel Sambuc #endif
33*44bedb31SLionel Sambuc 
34*44bedb31SLionel Sambuc 
35*44bedb31SLionel Sambuc 
36*44bedb31SLionel Sambuc #define WRITEBUFFERSIZE (16384)
37*44bedb31SLionel Sambuc #define MAXFILENAME (256)
38*44bedb31SLionel Sambuc 
39*44bedb31SLionel Sambuc #ifdef WIN32
filetime(f,tmzip,dt)40*44bedb31SLionel Sambuc uLong filetime(f, tmzip, dt)
41*44bedb31SLionel Sambuc     char *f;                /* name of file to get info on */
42*44bedb31SLionel Sambuc     tm_zip *tmzip;             /* return value: access, modific. and creation times */
43*44bedb31SLionel Sambuc     uLong *dt;             /* dostime */
44*44bedb31SLionel Sambuc {
45*44bedb31SLionel Sambuc   int ret = 0;
46*44bedb31SLionel Sambuc   {
47*44bedb31SLionel Sambuc       FILETIME ftLocal;
48*44bedb31SLionel Sambuc       HANDLE hFind;
49*44bedb31SLionel Sambuc       WIN32_FIND_DATA  ff32;
50*44bedb31SLionel Sambuc 
51*44bedb31SLionel Sambuc       hFind = FindFirstFile(f,&ff32);
52*44bedb31SLionel Sambuc       if (hFind != INVALID_HANDLE_VALUE)
53*44bedb31SLionel Sambuc       {
54*44bedb31SLionel Sambuc         FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
55*44bedb31SLionel Sambuc         FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
56*44bedb31SLionel Sambuc         FindClose(hFind);
57*44bedb31SLionel Sambuc         ret = 1;
58*44bedb31SLionel Sambuc       }
59*44bedb31SLionel Sambuc   }
60*44bedb31SLionel Sambuc   return ret;
61*44bedb31SLionel Sambuc }
62*44bedb31SLionel Sambuc #else
63*44bedb31SLionel Sambuc #ifdef unix
filetime(f,tmzip,dt)64*44bedb31SLionel Sambuc uLong filetime(f, tmzip, dt)
65*44bedb31SLionel Sambuc     char *f;               /* name of file to get info on */
66*44bedb31SLionel Sambuc     tm_zip *tmzip;         /* return value: access, modific. and creation times */
67*44bedb31SLionel Sambuc     uLong *dt;             /* dostime */
68*44bedb31SLionel Sambuc {
69*44bedb31SLionel Sambuc   int ret=0;
70*44bedb31SLionel Sambuc   struct stat s;        /* results of stat() */
71*44bedb31SLionel Sambuc   struct tm* filedate;
72*44bedb31SLionel Sambuc   time_t tm_t=0;
73*44bedb31SLionel Sambuc 
74*44bedb31SLionel Sambuc   if (strcmp(f,"-")!=0)
75*44bedb31SLionel Sambuc   {
76*44bedb31SLionel Sambuc     char name[MAXFILENAME+1];
77*44bedb31SLionel Sambuc     int len = strlen(f);
78*44bedb31SLionel Sambuc     if (len > MAXFILENAME)
79*44bedb31SLionel Sambuc       len = MAXFILENAME;
80*44bedb31SLionel Sambuc 
81*44bedb31SLionel Sambuc     strncpy(name, f,MAXFILENAME-1);
82*44bedb31SLionel Sambuc     /* strncpy doesnt append the trailing NULL, of the string is too long. */
83*44bedb31SLionel Sambuc     name[ MAXFILENAME ] = '\0';
84*44bedb31SLionel Sambuc 
85*44bedb31SLionel Sambuc     if (name[len - 1] == '/')
86*44bedb31SLionel Sambuc       name[len - 1] = '\0';
87*44bedb31SLionel Sambuc     /* not all systems allow stat'ing a file with / appended */
88*44bedb31SLionel Sambuc     if (stat(name,&s)==0)
89*44bedb31SLionel Sambuc     {
90*44bedb31SLionel Sambuc       tm_t = s.st_mtime;
91*44bedb31SLionel Sambuc       ret = 1;
92*44bedb31SLionel Sambuc     }
93*44bedb31SLionel Sambuc   }
94*44bedb31SLionel Sambuc   filedate = localtime(&tm_t);
95*44bedb31SLionel Sambuc 
96*44bedb31SLionel Sambuc   tmzip->tm_sec  = filedate->tm_sec;
97*44bedb31SLionel Sambuc   tmzip->tm_min  = filedate->tm_min;
98*44bedb31SLionel Sambuc   tmzip->tm_hour = filedate->tm_hour;
99*44bedb31SLionel Sambuc   tmzip->tm_mday = filedate->tm_mday;
100*44bedb31SLionel Sambuc   tmzip->tm_mon  = filedate->tm_mon ;
101*44bedb31SLionel Sambuc   tmzip->tm_year = filedate->tm_year;
102*44bedb31SLionel Sambuc 
103*44bedb31SLionel Sambuc   return ret;
104*44bedb31SLionel Sambuc }
105*44bedb31SLionel Sambuc #else
filetime(f,tmzip,dt)106*44bedb31SLionel Sambuc uLong filetime(f, tmzip, dt)
107*44bedb31SLionel Sambuc     char *f;                /* name of file to get info on */
108*44bedb31SLionel Sambuc     tm_zip *tmzip;             /* return value: access, modific. and creation times */
109*44bedb31SLionel Sambuc     uLong *dt;             /* dostime */
110*44bedb31SLionel Sambuc {
111*44bedb31SLionel Sambuc     return 0;
112*44bedb31SLionel Sambuc }
113*44bedb31SLionel Sambuc #endif
114*44bedb31SLionel Sambuc #endif
115*44bedb31SLionel Sambuc 
116*44bedb31SLionel Sambuc 
117*44bedb31SLionel Sambuc 
118*44bedb31SLionel Sambuc 
check_exist_file(filename)119*44bedb31SLionel Sambuc int check_exist_file(filename)
120*44bedb31SLionel Sambuc     const char* filename;
121*44bedb31SLionel Sambuc {
122*44bedb31SLionel Sambuc     FILE* ftestexist;
123*44bedb31SLionel Sambuc     int ret = 1;
124*44bedb31SLionel Sambuc     ftestexist = fopen(filename,"rb");
125*44bedb31SLionel Sambuc     if (ftestexist==NULL)
126*44bedb31SLionel Sambuc         ret = 0;
127*44bedb31SLionel Sambuc     else
128*44bedb31SLionel Sambuc         fclose(ftestexist);
129*44bedb31SLionel Sambuc     return ret;
130*44bedb31SLionel Sambuc }
131*44bedb31SLionel Sambuc 
do_banner()132*44bedb31SLionel Sambuc void do_banner()
133*44bedb31SLionel Sambuc {
134*44bedb31SLionel Sambuc     printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\n");
135*44bedb31SLionel Sambuc     printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
136*44bedb31SLionel Sambuc }
137*44bedb31SLionel Sambuc 
do_help()138*44bedb31SLionel Sambuc void do_help()
139*44bedb31SLionel Sambuc {
140*44bedb31SLionel Sambuc     printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" \
141*44bedb31SLionel Sambuc            "  -o  Overwrite existing file.zip\n" \
142*44bedb31SLionel Sambuc            "  -a  Append to existing file.zip\n" \
143*44bedb31SLionel Sambuc            "  -0  Store only\n" \
144*44bedb31SLionel Sambuc            "  -1  Compress faster\n" \
145*44bedb31SLionel Sambuc            "  -9  Compress better\n\n");
146*44bedb31SLionel Sambuc }
147*44bedb31SLionel Sambuc 
148*44bedb31SLionel Sambuc /* calculate the CRC32 of a file,
149*44bedb31SLionel Sambuc    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)150*44bedb31SLionel Sambuc int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
151*44bedb31SLionel Sambuc {
152*44bedb31SLionel Sambuc    unsigned long calculate_crc=0;
153*44bedb31SLionel Sambuc    int err=ZIP_OK;
154*44bedb31SLionel Sambuc    FILE * fin = fopen(filenameinzip,"rb");
155*44bedb31SLionel Sambuc    unsigned long size_read = 0;
156*44bedb31SLionel Sambuc    unsigned long total_read = 0;
157*44bedb31SLionel Sambuc    if (fin==NULL)
158*44bedb31SLionel Sambuc    {
159*44bedb31SLionel Sambuc        err = ZIP_ERRNO;
160*44bedb31SLionel Sambuc    }
161*44bedb31SLionel Sambuc 
162*44bedb31SLionel Sambuc     if (err == ZIP_OK)
163*44bedb31SLionel Sambuc         do
164*44bedb31SLionel Sambuc         {
165*44bedb31SLionel Sambuc             err = ZIP_OK;
166*44bedb31SLionel Sambuc             size_read = (int)fread(buf,1,size_buf,fin);
167*44bedb31SLionel Sambuc             if (size_read < size_buf)
168*44bedb31SLionel Sambuc                 if (feof(fin)==0)
169*44bedb31SLionel Sambuc             {
170*44bedb31SLionel Sambuc                 printf("error in reading %s\n",filenameinzip);
171*44bedb31SLionel Sambuc                 err = ZIP_ERRNO;
172*44bedb31SLionel Sambuc             }
173*44bedb31SLionel Sambuc 
174*44bedb31SLionel Sambuc             if (size_read>0)
175*44bedb31SLionel Sambuc                 calculate_crc = crc32(calculate_crc,buf,size_read);
176*44bedb31SLionel Sambuc             total_read += size_read;
177*44bedb31SLionel Sambuc 
178*44bedb31SLionel Sambuc         } while ((err == ZIP_OK) && (size_read>0));
179*44bedb31SLionel Sambuc 
180*44bedb31SLionel Sambuc     if (fin)
181*44bedb31SLionel Sambuc         fclose(fin);
182*44bedb31SLionel Sambuc 
183*44bedb31SLionel Sambuc     *result_crc=calculate_crc;
184*44bedb31SLionel Sambuc     printf("file %s crc %x\n",filenameinzip,calculate_crc);
185*44bedb31SLionel Sambuc     return err;
186*44bedb31SLionel Sambuc }
187*44bedb31SLionel Sambuc 
main(argc,argv)188*44bedb31SLionel Sambuc int main(argc,argv)
189*44bedb31SLionel Sambuc     int argc;
190*44bedb31SLionel Sambuc     char *argv[];
191*44bedb31SLionel Sambuc {
192*44bedb31SLionel Sambuc     int i;
193*44bedb31SLionel Sambuc     int opt_overwrite=0;
194*44bedb31SLionel Sambuc     int opt_compress_level=Z_DEFAULT_COMPRESSION;
195*44bedb31SLionel Sambuc     int zipfilenamearg = 0;
196*44bedb31SLionel Sambuc     char filename_try[MAXFILENAME+16];
197*44bedb31SLionel Sambuc     int zipok;
198*44bedb31SLionel Sambuc     int err=0;
199*44bedb31SLionel Sambuc     int size_buf=0;
200*44bedb31SLionel Sambuc     void* buf=NULL;
201*44bedb31SLionel Sambuc     const char* password=NULL;
202*44bedb31SLionel Sambuc 
203*44bedb31SLionel Sambuc 
204*44bedb31SLionel Sambuc     do_banner();
205*44bedb31SLionel Sambuc     if (argc==1)
206*44bedb31SLionel Sambuc     {
207*44bedb31SLionel Sambuc         do_help();
208*44bedb31SLionel Sambuc         return 0;
209*44bedb31SLionel Sambuc     }
210*44bedb31SLionel Sambuc     else
211*44bedb31SLionel Sambuc     {
212*44bedb31SLionel Sambuc         for (i=1;i<argc;i++)
213*44bedb31SLionel Sambuc         {
214*44bedb31SLionel Sambuc             if ((*argv[i])=='-')
215*44bedb31SLionel Sambuc             {
216*44bedb31SLionel Sambuc                 const char *p=argv[i]+1;
217*44bedb31SLionel Sambuc 
218*44bedb31SLionel Sambuc                 while ((*p)!='\0')
219*44bedb31SLionel Sambuc                 {
220*44bedb31SLionel Sambuc                     char c=*(p++);;
221*44bedb31SLionel Sambuc                     if ((c=='o') || (c=='O'))
222*44bedb31SLionel Sambuc                         opt_overwrite = 1;
223*44bedb31SLionel Sambuc                     if ((c=='a') || (c=='A'))
224*44bedb31SLionel Sambuc                         opt_overwrite = 2;
225*44bedb31SLionel Sambuc                     if ((c>='0') && (c<='9'))
226*44bedb31SLionel Sambuc                         opt_compress_level = c-'0';
227*44bedb31SLionel Sambuc 
228*44bedb31SLionel Sambuc                     if (((c=='p') || (c=='P')) && (i+1<argc))
229*44bedb31SLionel Sambuc                     {
230*44bedb31SLionel Sambuc                         password=argv[i+1];
231*44bedb31SLionel Sambuc                         i++;
232*44bedb31SLionel Sambuc                     }
233*44bedb31SLionel Sambuc                 }
234*44bedb31SLionel Sambuc             }
235*44bedb31SLionel Sambuc             else
236*44bedb31SLionel Sambuc                 if (zipfilenamearg == 0)
237*44bedb31SLionel Sambuc                     zipfilenamearg = i ;
238*44bedb31SLionel Sambuc         }
239*44bedb31SLionel Sambuc     }
240*44bedb31SLionel Sambuc 
241*44bedb31SLionel Sambuc     size_buf = WRITEBUFFERSIZE;
242*44bedb31SLionel Sambuc     buf = (void*)malloc(size_buf);
243*44bedb31SLionel Sambuc     if (buf==NULL)
244*44bedb31SLionel Sambuc     {
245*44bedb31SLionel Sambuc         printf("Error allocating memory\n");
246*44bedb31SLionel Sambuc         return ZIP_INTERNALERROR;
247*44bedb31SLionel Sambuc     }
248*44bedb31SLionel Sambuc 
249*44bedb31SLionel Sambuc     if (zipfilenamearg==0)
250*44bedb31SLionel Sambuc         zipok=0;
251*44bedb31SLionel Sambuc     else
252*44bedb31SLionel Sambuc     {
253*44bedb31SLionel Sambuc         int i,len;
254*44bedb31SLionel Sambuc         int dot_found=0;
255*44bedb31SLionel Sambuc 
256*44bedb31SLionel Sambuc         zipok = 1 ;
257*44bedb31SLionel Sambuc         strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
258*44bedb31SLionel Sambuc         /* strncpy doesnt append the trailing NULL, of the string is too long. */
259*44bedb31SLionel Sambuc         filename_try[ MAXFILENAME ] = '\0';
260*44bedb31SLionel Sambuc 
261*44bedb31SLionel Sambuc         len=(int)strlen(filename_try);
262*44bedb31SLionel Sambuc         for (i=0;i<len;i++)
263*44bedb31SLionel Sambuc             if (filename_try[i]=='.')
264*44bedb31SLionel Sambuc                 dot_found=1;
265*44bedb31SLionel Sambuc 
266*44bedb31SLionel Sambuc         if (dot_found==0)
267*44bedb31SLionel Sambuc             strcat(filename_try,".zip");
268*44bedb31SLionel Sambuc 
269*44bedb31SLionel Sambuc         if (opt_overwrite==2)
270*44bedb31SLionel Sambuc         {
271*44bedb31SLionel Sambuc             /* if the file don't exist, we not append file */
272*44bedb31SLionel Sambuc             if (check_exist_file(filename_try)==0)
273*44bedb31SLionel Sambuc                 opt_overwrite=1;
274*44bedb31SLionel Sambuc         }
275*44bedb31SLionel Sambuc         else
276*44bedb31SLionel Sambuc         if (opt_overwrite==0)
277*44bedb31SLionel Sambuc             if (check_exist_file(filename_try)!=0)
278*44bedb31SLionel Sambuc             {
279*44bedb31SLionel Sambuc                 char rep=0;
280*44bedb31SLionel Sambuc                 do
281*44bedb31SLionel Sambuc                 {
282*44bedb31SLionel Sambuc                     char answer[128];
283*44bedb31SLionel Sambuc                     int ret;
284*44bedb31SLionel Sambuc                     printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
285*44bedb31SLionel Sambuc                     ret = scanf("%1s",answer);
286*44bedb31SLionel Sambuc                     if (ret != 1)
287*44bedb31SLionel Sambuc                     {
288*44bedb31SLionel Sambuc                        exit(EXIT_FAILURE);
289*44bedb31SLionel Sambuc                     }
290*44bedb31SLionel Sambuc                     rep = answer[0] ;
291*44bedb31SLionel Sambuc                     if ((rep>='a') && (rep<='z'))
292*44bedb31SLionel Sambuc                         rep -= 0x20;
293*44bedb31SLionel Sambuc                 }
294*44bedb31SLionel Sambuc                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
295*44bedb31SLionel Sambuc                 if (rep=='N')
296*44bedb31SLionel Sambuc                     zipok = 0;
297*44bedb31SLionel Sambuc                 if (rep=='A')
298*44bedb31SLionel Sambuc                     opt_overwrite = 2;
299*44bedb31SLionel Sambuc             }
300*44bedb31SLionel Sambuc     }
301*44bedb31SLionel Sambuc 
302*44bedb31SLionel Sambuc     if (zipok==1)
303*44bedb31SLionel Sambuc     {
304*44bedb31SLionel Sambuc         zipFile zf;
305*44bedb31SLionel Sambuc         int errclose;
306*44bedb31SLionel Sambuc #        ifdef USEWIN32IOAPI
307*44bedb31SLionel Sambuc         zlib_filefunc_def ffunc;
308*44bedb31SLionel Sambuc         fill_win32_filefunc(&ffunc);
309*44bedb31SLionel Sambuc         zf = zipOpen2(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
310*44bedb31SLionel Sambuc #        else
311*44bedb31SLionel Sambuc         zf = zipOpen(filename_try,(opt_overwrite==2) ? 2 : 0);
312*44bedb31SLionel Sambuc #        endif
313*44bedb31SLionel Sambuc 
314*44bedb31SLionel Sambuc         if (zf == NULL)
315*44bedb31SLionel Sambuc         {
316*44bedb31SLionel Sambuc             printf("error opening %s\n",filename_try);
317*44bedb31SLionel Sambuc             err= ZIP_ERRNO;
318*44bedb31SLionel Sambuc         }
319*44bedb31SLionel Sambuc         else
320*44bedb31SLionel Sambuc             printf("creating %s\n",filename_try);
321*44bedb31SLionel Sambuc 
322*44bedb31SLionel Sambuc         for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
323*44bedb31SLionel Sambuc         {
324*44bedb31SLionel Sambuc             if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
325*44bedb31SLionel Sambuc                   ((argv[i][1]=='o') || (argv[i][1]=='O') ||
326*44bedb31SLionel Sambuc                    (argv[i][1]=='a') || (argv[i][1]=='A') ||
327*44bedb31SLionel Sambuc                    (argv[i][1]=='p') || (argv[i][1]=='P') ||
328*44bedb31SLionel Sambuc                    ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
329*44bedb31SLionel Sambuc                   (strlen(argv[i]) == 2)))
330*44bedb31SLionel Sambuc             {
331*44bedb31SLionel Sambuc                 FILE * fin;
332*44bedb31SLionel Sambuc                 int size_read;
333*44bedb31SLionel Sambuc                 const char* filenameinzip = argv[i];
334*44bedb31SLionel Sambuc                 zip_fileinfo zi;
335*44bedb31SLionel Sambuc                 unsigned long crcFile=0;
336*44bedb31SLionel Sambuc 
337*44bedb31SLionel Sambuc                 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
338*44bedb31SLionel Sambuc                 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
339*44bedb31SLionel Sambuc                 zi.dosDate = 0;
340*44bedb31SLionel Sambuc                 zi.internal_fa = 0;
341*44bedb31SLionel Sambuc                 zi.external_fa = 0;
342*44bedb31SLionel Sambuc                 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
343*44bedb31SLionel Sambuc 
344*44bedb31SLionel Sambuc /*
345*44bedb31SLionel Sambuc                 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
346*44bedb31SLionel Sambuc                                  NULL,0,NULL,0,NULL / * comment * /,
347*44bedb31SLionel Sambuc                                  (opt_compress_level != 0) ? Z_DEFLATED : 0,
348*44bedb31SLionel Sambuc                                  opt_compress_level);
349*44bedb31SLionel Sambuc */
350*44bedb31SLionel Sambuc                 if ((password != NULL) && (err==ZIP_OK))
351*44bedb31SLionel Sambuc                     err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
352*44bedb31SLionel Sambuc 
353*44bedb31SLionel Sambuc                 err = zipOpenNewFileInZip3(zf,filenameinzip,&zi,
354*44bedb31SLionel Sambuc                                  NULL,0,NULL,0,NULL /* comment*/,
355*44bedb31SLionel Sambuc                                  (opt_compress_level != 0) ? Z_DEFLATED : 0,
356*44bedb31SLionel Sambuc                                  opt_compress_level,0,
357*44bedb31SLionel Sambuc                                  /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
358*44bedb31SLionel Sambuc                                  -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
359*44bedb31SLionel Sambuc                                  password,crcFile);
360*44bedb31SLionel Sambuc 
361*44bedb31SLionel Sambuc                 if (err != ZIP_OK)
362*44bedb31SLionel Sambuc                     printf("error in opening %s in zipfile\n",filenameinzip);
363*44bedb31SLionel Sambuc                 else
364*44bedb31SLionel Sambuc                 {
365*44bedb31SLionel Sambuc                     fin = fopen(filenameinzip,"rb");
366*44bedb31SLionel Sambuc                     if (fin==NULL)
367*44bedb31SLionel Sambuc                     {
368*44bedb31SLionel Sambuc                         err=ZIP_ERRNO;
369*44bedb31SLionel Sambuc                         printf("error in opening %s for reading\n",filenameinzip);
370*44bedb31SLionel Sambuc                     }
371*44bedb31SLionel Sambuc                 }
372*44bedb31SLionel Sambuc 
373*44bedb31SLionel Sambuc                 if (err == ZIP_OK)
374*44bedb31SLionel Sambuc                     do
375*44bedb31SLionel Sambuc                     {
376*44bedb31SLionel Sambuc                         err = ZIP_OK;
377*44bedb31SLionel Sambuc                         size_read = (int)fread(buf,1,size_buf,fin);
378*44bedb31SLionel Sambuc                         if (size_read < size_buf)
379*44bedb31SLionel Sambuc                             if (feof(fin)==0)
380*44bedb31SLionel Sambuc                         {
381*44bedb31SLionel Sambuc                             printf("error in reading %s\n",filenameinzip);
382*44bedb31SLionel Sambuc                             err = ZIP_ERRNO;
383*44bedb31SLionel Sambuc                         }
384*44bedb31SLionel Sambuc 
385*44bedb31SLionel Sambuc                         if (size_read>0)
386*44bedb31SLionel Sambuc                         {
387*44bedb31SLionel Sambuc                             err = zipWriteInFileInZip (zf,buf,size_read);
388*44bedb31SLionel Sambuc                             if (err<0)
389*44bedb31SLionel Sambuc                             {
390*44bedb31SLionel Sambuc                                 printf("error in writing %s in the zipfile\n",
391*44bedb31SLionel Sambuc                                                  filenameinzip);
392*44bedb31SLionel Sambuc                             }
393*44bedb31SLionel Sambuc 
394*44bedb31SLionel Sambuc                         }
395*44bedb31SLionel Sambuc                     } while ((err == ZIP_OK) && (size_read>0));
396*44bedb31SLionel Sambuc 
397*44bedb31SLionel Sambuc                 if (fin)
398*44bedb31SLionel Sambuc                     fclose(fin);
399*44bedb31SLionel Sambuc 
400*44bedb31SLionel Sambuc                 if (err<0)
401*44bedb31SLionel Sambuc                     err=ZIP_ERRNO;
402*44bedb31SLionel Sambuc                 else
403*44bedb31SLionel Sambuc                 {
404*44bedb31SLionel Sambuc                     err = zipCloseFileInZip(zf);
405*44bedb31SLionel Sambuc                     if (err!=ZIP_OK)
406*44bedb31SLionel Sambuc                         printf("error in closing %s in the zipfile\n",
407*44bedb31SLionel Sambuc                                     filenameinzip);
408*44bedb31SLionel Sambuc                 }
409*44bedb31SLionel Sambuc             }
410*44bedb31SLionel Sambuc         }
411*44bedb31SLionel Sambuc         errclose = zipClose(zf,NULL);
412*44bedb31SLionel Sambuc         if (errclose != ZIP_OK)
413*44bedb31SLionel Sambuc             printf("error in closing %s\n",filename_try);
414*44bedb31SLionel Sambuc     }
415*44bedb31SLionel Sambuc     else
416*44bedb31SLionel Sambuc     {
417*44bedb31SLionel Sambuc        do_help();
418*44bedb31SLionel Sambuc     }
419*44bedb31SLionel Sambuc 
420*44bedb31SLionel Sambuc     free(buf);
421*44bedb31SLionel Sambuc     return 0;
422*44bedb31SLionel Sambuc }
423