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