• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/zlib/contrib/minizip/minizip.c

00001 /*
00002    minizip.c
00003    Version 1.1, February 14h, 2010
00004    sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
00005 
00006          Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
00007 
00008          Modifications of Unzip for Zip64
00009          Copyright (C) 2007-2008 Even Rouault
00010 
00011          Modifications for Zip64 support on both zip and unzip
00012          Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
00013 */
00014 
00015 
00016 #ifndef _WIN32
00017         #ifndef __USE_FILE_OFFSET64
00018                 #define __USE_FILE_OFFSET64
00019         #endif
00020         #ifndef __USE_LARGEFILE64
00021                 #define __USE_LARGEFILE64
00022         #endif
00023         #ifndef _LARGEFILE64_SOURCE
00024                 #define _LARGEFILE64_SOURCE
00025         #endif
00026         #ifndef _FILE_OFFSET_BIT
00027                 #define _FILE_OFFSET_BIT 64
00028         #endif
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <time.h>
00035 #include <errno.h>
00036 #include <fcntl.h>
00037 
00038 #ifdef unix
00039 # include <unistd.h>
00040 # include <utime.h>
00041 # include <sys/types.h>
00042 # include <sys/stat.h>
00043 #else
00044 # include <direct.h>
00045 # include <io.h>
00046 #endif
00047 
00048 #include "zip.h"
00049 
00050 #ifdef _WIN32
00051         #define USEWIN32IOAPI
00052         #include "iowin32.h"
00053 #endif
00054 
00055 
00056 
00057 #define WRITEBUFFERSIZE (16384)
00058 #define MAXFILENAME (256)
00059 
00060 #ifdef _WIN32
00061 uLong filetime(f, tmzip, dt)
00062     char *f;                /* name of file to get info on */
00063     tm_zip *tmzip;             /* return value: access, modific. and creation times */
00064     uLong *dt;             /* dostime */
00065 {
00066   int ret = 0;
00067   {
00068       FILETIME ftLocal;
00069       HANDLE hFind;
00070       WIN32_FIND_DATAA ff32;
00071 
00072       hFind = FindFirstFileA(f,&ff32);
00073       if (hFind != INVALID_HANDLE_VALUE)
00074       {
00075         FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
00076         FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
00077         FindClose(hFind);
00078         ret = 1;
00079       }
00080   }
00081   return ret;
00082 }
00083 #else
00084 #ifdef unix
00085 uLong filetime(f, tmzip, dt)
00086     char *f;               /* name of file to get info on */
00087     tm_zip *tmzip;         /* return value: access, modific. and creation times */
00088     uLong *dt;             /* dostime */
00089 {
00090   int ret=0;
00091   struct stat s;        /* results of stat() */
00092   struct tm* filedate;
00093   time_t tm_t=0;
00094 
00095   if (strcmp(f,"-")!=0)
00096   {
00097     char name[MAXFILENAME+1];
00098     int len = strlen(f);
00099     if (len > MAXFILENAME)
00100       len = MAXFILENAME;
00101 
00102     strncpy(name, f,MAXFILENAME-1);
00103     /* strncpy doesnt append the trailing NULL, of the string is too long. */
00104     name[ MAXFILENAME ] = '\0';
00105 
00106     if (name[len - 1] == '/')
00107       name[len - 1] = '\0';
00108     /* not all systems allow stat'ing a file with / appended */
00109     if (stat(name,&s)==0)
00110     {
00111       tm_t = s.st_mtime;
00112       ret = 1;
00113     }
00114   }
00115   filedate = localtime(&tm_t);
00116 
00117   tmzip->tm_sec  = filedate->tm_sec;
00118   tmzip->tm_min  = filedate->tm_min;
00119   tmzip->tm_hour = filedate->tm_hour;
00120   tmzip->tm_mday = filedate->tm_mday;
00121   tmzip->tm_mon  = filedate->tm_mon ;
00122   tmzip->tm_year = filedate->tm_year;
00123 
00124   return ret;
00125 }
00126 #else
00127 uLong filetime(f, tmzip, dt)
00128     char *f;                /* name of file to get info on */
00129     tm_zip *tmzip;             /* return value: access, modific. and creation times */
00130     uLong *dt;             /* dostime */
00131 {
00132     return 0;
00133 }
00134 #endif
00135 #endif
00136 
00137 
00138 
00139 
00140 int check_exist_file(filename)
00141     const char* filename;
00142 {
00143     FILE* ftestexist;
00144     int ret = 1;
00145     ftestexist = fopen64(filename,"rb");
00146     if (ftestexist==NULL)
00147         ret = 0;
00148     else
00149         fclose(ftestexist);
00150     return ret;
00151 }
00152 
00153 void do_banner()
00154 {
00155     printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
00156     printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
00157 }
00158 
00159 void do_help()
00160 {
00161     printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
00162            "  -o  Overwrite existing file.zip\n" \
00163            "  -a  Append to existing file.zip\n" \
00164            "  -0  Store only\n" \
00165            "  -1  Compress faster\n" \
00166            "  -9  Compress better\n\n" \
00167            "  -j  exclude path. store only the file name.\n\n");
00168 }
00169 
00170 /* calculate the CRC32 of a file,
00171    because to encrypt a file, we need known the CRC32 of the file before */
00172 int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
00173 {
00174    unsigned long calculate_crc=0;
00175    int err=ZIP_OK;
00176    FILE * fin = fopen64(filenameinzip,"rb");
00177    unsigned long size_read = 0;
00178    unsigned long total_read = 0;
00179    if (fin==NULL)
00180    {
00181        err = ZIP_ERRNO;
00182    }
00183 
00184     if (err == ZIP_OK)
00185         do
00186         {
00187             err = ZIP_OK;
00188             size_read = (int)fread(buf,1,size_buf,fin);
00189             if (size_read < size_buf)
00190                 if (feof(fin)==0)
00191             {
00192                 printf("error in reading %s\n",filenameinzip);
00193                 err = ZIP_ERRNO;
00194             }
00195 
00196             if (size_read>0)
00197                 calculate_crc = crc32(calculate_crc,buf,size_read);
00198             total_read += size_read;
00199 
00200         } while ((err == ZIP_OK) && (size_read>0));
00201 
00202     if (fin)
00203         fclose(fin);
00204 
00205     *result_crc=calculate_crc;
00206     printf("file %s crc %lx\n", filenameinzip, calculate_crc);
00207     return err;
00208 }
00209 
00210 int isLargeFile(const char* filename)
00211 {
00212   int largeFile = 0;
00213   ZPOS64_T pos = 0;
00214   FILE* pFile = fopen64(filename, "rb");
00215 
00216   if(pFile != NULL)
00217   {
00218     int n = fseeko64(pFile, 0, SEEK_END);
00219 
00220     pos = ftello64(pFile);
00221 
00222                 printf("File : %s is %lld bytes\n", filename, pos);
00223 
00224     if(pos >= 0xffffffff)
00225      largeFile = 1;
00226 
00227                 fclose(pFile);
00228   }
00229 
00230  return largeFile;
00231 }
00232 
00233 int main(argc,argv)
00234     int argc;
00235     char *argv[];
00236 {
00237     int i;
00238     int opt_overwrite=0;
00239     int opt_compress_level=Z_DEFAULT_COMPRESSION;
00240     int opt_exclude_path=0;
00241     int zipfilenamearg = 0;
00242     char filename_try[MAXFILENAME+16];
00243     int zipok;
00244     int err=0;
00245     int size_buf=0;
00246     void* buf=NULL;
00247     const char* password=NULL;
00248 
00249 
00250     do_banner();
00251     if (argc==1)
00252     {
00253         do_help();
00254         return 0;
00255     }
00256     else
00257     {
00258         for (i=1;i<argc;i++)
00259         {
00260             if ((*argv[i])=='-')
00261             {
00262                 const char *p=argv[i]+1;
00263 
00264                 while ((*p)!='\0')
00265                 {
00266                     char c=*(p++);;
00267                     if ((c=='o') || (c=='O'))
00268                         opt_overwrite = 1;
00269                     if ((c=='a') || (c=='A'))
00270                         opt_overwrite = 2;
00271                     if ((c>='0') && (c<='9'))
00272                         opt_compress_level = c-'0';
00273                     if ((c=='j') || (c=='J'))
00274                         opt_exclude_path = 1;
00275 
00276                     if (((c=='p') || (c=='P')) && (i+1<argc))
00277                     {
00278                         password=argv[i+1];
00279                         i++;
00280                     }
00281                 }
00282             }
00283             else
00284             {
00285                 if (zipfilenamearg == 0)
00286                 {
00287                     zipfilenamearg = i ;
00288                 }
00289             }
00290         }
00291     }
00292 
00293     size_buf = WRITEBUFFERSIZE;
00294     buf = (void*)malloc(size_buf);
00295     if (buf==NULL)
00296     {
00297         printf("Error allocating memory\n");
00298         return ZIP_INTERNALERROR;
00299     }
00300 
00301     if (zipfilenamearg==0)
00302     {
00303         zipok=0;
00304     }
00305     else
00306     {
00307         int i,len;
00308         int dot_found=0;
00309 
00310         zipok = 1 ;
00311         strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
00312         /* strncpy doesnt append the trailing NULL, of the string is too long. */
00313         filename_try[ MAXFILENAME ] = '\0';
00314 
00315         len=(int)strlen(filename_try);
00316         for (i=0;i<len;i++)
00317             if (filename_try[i]=='.')
00318                 dot_found=1;
00319 
00320         if (dot_found==0)
00321             strcat(filename_try,".zip");
00322 
00323         if (opt_overwrite==2)
00324         {
00325             /* if the file don't exist, we not append file */
00326             if (check_exist_file(filename_try)==0)
00327                 opt_overwrite=1;
00328         }
00329         else
00330         if (opt_overwrite==0)
00331             if (check_exist_file(filename_try)!=0)
00332             {
00333                 char rep=0;
00334                 do
00335                 {
00336                     char answer[128];
00337                     int ret;
00338                     printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
00339                     ret = scanf("%1s",answer);
00340                     if (ret != 1)
00341                     {
00342                        exit(EXIT_FAILURE);
00343                     }
00344                     rep = answer[0] ;
00345                     if ((rep>='a') && (rep<='z'))
00346                         rep -= 0x20;
00347                 }
00348                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
00349                 if (rep=='N')
00350                     zipok = 0;
00351                 if (rep=='A')
00352                     opt_overwrite = 2;
00353             }
00354     }
00355 
00356     if (zipok==1)
00357     {
00358         zipFile zf;
00359         int errclose;
00360 #        ifdef USEWIN32IOAPI
00361         zlib_filefunc64_def ffunc;
00362         fill_win32_filefunc64A(&ffunc);
00363         zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
00364 #        else
00365         zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
00366 #        endif
00367 
00368         if (zf == NULL)
00369         {
00370             printf("error opening %s\n",filename_try);
00371             err= ZIP_ERRNO;
00372         }
00373         else
00374             printf("creating %s\n",filename_try);
00375 
00376         for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
00377         {
00378             if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
00379                   ((argv[i][1]=='o') || (argv[i][1]=='O') ||
00380                    (argv[i][1]=='a') || (argv[i][1]=='A') ||
00381                    (argv[i][1]=='p') || (argv[i][1]=='P') ||
00382                    ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
00383                   (strlen(argv[i]) == 2)))
00384             {
00385                 FILE * fin;
00386                 int size_read;
00387                 const char* filenameinzip = argv[i];
00388                 const char *savefilenameinzip;
00389                 zip_fileinfo zi;
00390                 unsigned long crcFile=0;
00391                 int zip64 = 0;
00392 
00393                 zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
00394                 zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
00395                 zi.dosDate = 0;
00396                 zi.internal_fa = 0;
00397                 zi.external_fa = 0;
00398                 filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
00399 
00400 /*
00401                 err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
00402                                  NULL,0,NULL,0,NULL / * comment * /,
00403                                  (opt_compress_level != 0) ? Z_DEFLATED : 0,
00404                                  opt_compress_level);
00405 */
00406                 if ((password != NULL) && (err==ZIP_OK))
00407                     err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
00408 
00409                 zip64 = isLargeFile(filenameinzip);
00410 
00411                                                          /* The path name saved, should not include a leading slash. */
00412                /*if it did, windows/xp and dynazip couldn't read the zip file. */
00413                  savefilenameinzip = filenameinzip;
00414                  while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
00415                  {
00416                      savefilenameinzip++;
00417                  }
00418 
00419                  /*should the zip file contain any path at all?*/
00420                  if( opt_exclude_path )
00421                  {
00422                      const char *tmpptr;
00423                      const char *lastslash = 0;
00424                      for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
00425                      {
00426                          if( *tmpptr == '\\' || *tmpptr == '/')
00427                          {
00428                              lastslash = tmpptr;
00429                          }
00430                      }
00431                      if( lastslash != NULL )
00432                      {
00433                          savefilenameinzip = lastslash+1; // base filename follows last slash.
00434                      }
00435                  }
00436 
00437                  
00438                 err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
00439                                  NULL,0,NULL,0,NULL /* comment*/,
00440                                  (opt_compress_level != 0) ? Z_DEFLATED : 0,
00441                                  opt_compress_level,0,
00442                                  /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
00443                                  -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
00444                                  password,crcFile, zip64);
00445 
00446                 if (err != ZIP_OK)
00447                     printf("error in opening %s in zipfile\n",filenameinzip);
00448                 else
00449                 {
00450                     fin = fopen64(filenameinzip,"rb");
00451                     if (fin==NULL)
00452                     {
00453                         err=ZIP_ERRNO;
00454                         printf("error in opening %s for reading\n",filenameinzip);
00455                     }
00456                 }
00457 
00458                 if (err == ZIP_OK)
00459                     do
00460                     {
00461                         err = ZIP_OK;
00462                         size_read = (int)fread(buf,1,size_buf,fin);
00463                         if (size_read < size_buf)
00464                             if (feof(fin)==0)
00465                         {
00466                             printf("error in reading %s\n",filenameinzip);
00467                             err = ZIP_ERRNO;
00468                         }
00469 
00470                         if (size_read>0)
00471                         {
00472                             err = zipWriteInFileInZip (zf,buf,size_read);
00473                             if (err<0)
00474                             {
00475                                 printf("error in writing %s in the zipfile\n",
00476                                                  filenameinzip);
00477                             }
00478 
00479                         }
00480                     } while ((err == ZIP_OK) && (size_read>0));
00481 
00482                 if (fin)
00483                     fclose(fin);
00484 
00485                 if (err<0)
00486                     err=ZIP_ERRNO;
00487                 else
00488                 {
00489                     err = zipCloseFileInZip(zf);
00490                     if (err!=ZIP_OK)
00491                         printf("error in closing %s in the zipfile\n",
00492                                     filenameinzip);
00493                 }
00494             }
00495         }
00496         errclose = zipClose(zf,NULL);
00497         if (errclose != ZIP_OK)
00498             printf("error in closing %s\n",filename_try);
00499     }
00500     else
00501     {
00502        do_help();
00503     }
00504 
00505     free(buf);
00506     return 0;
00507 }

Generated on Wed Oct 20 2010 11:12:17 for APBS by  doxygen 1.7.2