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

contrib/zlib/contrib/minizip/miniunz.c

00001 /*
00002    miniunz.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 #ifndef _WIN32
00016         #ifndef __USE_FILE_OFFSET64
00017                 #define __USE_FILE_OFFSET64
00018         #endif
00019         #ifndef __USE_LARGEFILE64
00020                 #define __USE_LARGEFILE64
00021         #endif
00022         #ifndef _LARGEFILE64_SOURCE
00023                 #define _LARGEFILE64_SOURCE
00024         #endif
00025         #ifndef _FILE_OFFSET_BIT
00026                 #define _FILE_OFFSET_BIT 64
00027         #endif
00028 #endif
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <time.h>
00034 #include <errno.h>
00035 #include <fcntl.h>
00036 
00037 #ifdef unix
00038 # include <unistd.h>
00039 # include <utime.h>
00040 #else
00041 # include <direct.h>
00042 # include <io.h>
00043 #endif
00044 
00045 #include "unzip.h"
00046 
00047 #define CASESENSITIVITY (0)
00048 #define WRITEBUFFERSIZE (8192)
00049 #define MAXFILENAME (256)
00050 
00051 #ifdef _WIN32
00052 #define USEWIN32IOAPI
00053 #include "iowin32.h"
00054 #endif
00055 /*
00056   mini unzip, demo of unzip package
00057 
00058   usage :
00059   Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
00060 
00061   list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
00062     if it exists
00063 */
00064 
00065 
00066 /* change_file_date : change the date/time of a file
00067     filename : the filename of the file where date/time must be modified
00068     dosdate : the new date at the MSDos format (4 bytes)
00069     tmu_date : the SAME new date at the tm_unz format */
00070 void change_file_date(filename,dosdate,tmu_date)
00071     const char *filename;
00072     uLong dosdate;
00073     tm_unz tmu_date;
00074 {
00075 #ifdef _WIN32
00076   HANDLE hFile;
00077   FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
00078 
00079   hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
00080                       0,NULL,OPEN_EXISTING,0,NULL);
00081   GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
00082   DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
00083   LocalFileTimeToFileTime(&ftLocal,&ftm);
00084   SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
00085   CloseHandle(hFile);
00086 #else
00087 #ifdef unix
00088   struct utimbuf ut;
00089   struct tm newdate;
00090   newdate.tm_sec = tmu_date.tm_sec;
00091   newdate.tm_min=tmu_date.tm_min;
00092   newdate.tm_hour=tmu_date.tm_hour;
00093   newdate.tm_mday=tmu_date.tm_mday;
00094   newdate.tm_mon=tmu_date.tm_mon;
00095   if (tmu_date.tm_year > 1900)
00096       newdate.tm_year=tmu_date.tm_year - 1900;
00097   else
00098       newdate.tm_year=tmu_date.tm_year ;
00099   newdate.tm_isdst=-1;
00100 
00101   ut.actime=ut.modtime=mktime(&newdate);
00102   utime(filename,&ut);
00103 #endif
00104 #endif
00105 }
00106 
00107 
00108 /* mymkdir and change_file_date are not 100 % portable
00109    As I don't know well Unix, I wait feedback for the unix portion */
00110 
00111 int mymkdir(dirname)
00112     const char* dirname;
00113 {
00114     int ret=0;
00115 #ifdef _WIN32
00116     ret = _mkdir(dirname);
00117 #else
00118 #ifdef unix
00119     ret = mkdir (dirname,0775);
00120 #endif
00121 #endif
00122     return ret;
00123 }
00124 
00125 int makedir (newdir)
00126     char *newdir;
00127 {
00128   char *buffer ;
00129   char *p;
00130   int  len = (int)strlen(newdir);
00131 
00132   if (len <= 0)
00133     return 0;
00134 
00135   buffer = (char*)malloc(len+1);
00136         if (buffer==NULL)
00137         {
00138                 printf("Error allocating memory\n");
00139                 return UNZ_INTERNALERROR;
00140         }
00141   strcpy(buffer,newdir);
00142 
00143   if (buffer[len-1] == '/') {
00144     buffer[len-1] = '\0';
00145   }
00146   if (mymkdir(buffer) == 0)
00147     {
00148       free(buffer);
00149       return 1;
00150     }
00151 
00152   p = buffer+1;
00153   while (1)
00154     {
00155       char hold;
00156 
00157       while(*p && *p != '\\' && *p != '/')
00158         p++;
00159       hold = *p;
00160       *p = 0;
00161       if ((mymkdir(buffer) == -1) && (errno == ENOENT))
00162         {
00163           printf("couldn't create directory %s\n",buffer);
00164           free(buffer);
00165           return 0;
00166         }
00167       if (hold == 0)
00168         break;
00169       *p++ = hold;
00170     }
00171   free(buffer);
00172   return 1;
00173 }
00174 
00175 void do_banner()
00176 {
00177     printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
00178     printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
00179 }
00180 
00181 void do_help()
00182 {
00183     printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
00184            "  -e  Extract without pathname (junk paths)\n" \
00185            "  -x  Extract with pathname\n" \
00186            "  -v  list files\n" \
00187            "  -l  list files\n" \
00188            "  -d  directory to extract into\n" \
00189            "  -o  overwrite files without prompting\n" \
00190            "  -p  extract crypted file using password\n\n");
00191 }
00192 
00193 void Display64BitsSize(ZPOS64_T n, int size_char)
00194 {
00195   /* to avoid compatibility problem , we do here the conversion */
00196   char number[21];
00197   int offset=19;
00198   int pos_string = 19;
00199   number[20]=0;
00200   for (;;) {
00201       number[offset]=(char)((n%10)+'0');
00202       if (number[offset] != '0')
00203           pos_string=offset;
00204       n/=10;
00205       if (offset==0)
00206           break;
00207       offset--;
00208   }
00209   {
00210       int size_display_string = 19-pos_string;
00211       while (size_char > size_display_string)
00212       {
00213           size_char--;
00214           printf(" ");
00215       }
00216   }
00217 
00218   printf("%s",&number[pos_string]);
00219 }
00220 
00221 int do_list(uf)
00222     unzFile uf;
00223 {
00224     uLong i;
00225     unz_global_info64 gi;
00226     int err;
00227 
00228     err = unzGetGlobalInfo64(uf,&gi);
00229     if (err!=UNZ_OK)
00230         printf("error %d with zipfile in unzGetGlobalInfo \n",err);
00231     printf("  Length  Method     Size Ratio   Date    Time   CRC-32     Name\n");
00232     printf("  ------  ------     ---- -----   ----    ----   ------     ----\n");
00233     for (i=0;i<gi.number_entry;i++)
00234     {
00235         char filename_inzip[256];
00236         unz_file_info64 file_info;
00237         uLong ratio=0;
00238         const char *string_method;
00239         char charCrypt=' ';
00240         err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
00241         if (err!=UNZ_OK)
00242         {
00243             printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
00244             break;
00245         }
00246         if (file_info.uncompressed_size>0)
00247             ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
00248 
00249         /* display a '*' if the file is crypted */
00250         if ((file_info.flag & 1) != 0)
00251             charCrypt='*';
00252 
00253         if (file_info.compression_method==0)
00254             string_method="Stored";
00255         else
00256         if (file_info.compression_method==Z_DEFLATED)
00257         {
00258             uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
00259             if (iLevel==0)
00260               string_method="Defl:N";
00261             else if (iLevel==1)
00262               string_method="Defl:X";
00263             else if ((iLevel==2) || (iLevel==3))
00264               string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
00265         }
00266         else
00267         if (file_info.compression_method==Z_BZIP2ED)
00268         {
00269               string_method="BZip2 ";
00270         }
00271         else
00272             string_method="Unkn. ";
00273 
00274         Display64BitsSize(file_info.uncompressed_size,7);
00275         printf("  %6s%c",string_method,charCrypt);
00276         Display64BitsSize(file_info.compressed_size,7);
00277         printf(" %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
00278                 ratio,
00279                 (uLong)file_info.tmu_date.tm_mon + 1,
00280                 (uLong)file_info.tmu_date.tm_mday,
00281                 (uLong)file_info.tmu_date.tm_year % 100,
00282                 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
00283                 (uLong)file_info.crc,filename_inzip);
00284         if ((i+1)<gi.number_entry)
00285         {
00286             err = unzGoToNextFile(uf);
00287             if (err!=UNZ_OK)
00288             {
00289                 printf("error %d with zipfile in unzGoToNextFile\n",err);
00290                 break;
00291             }
00292         }
00293     }
00294 
00295     return 0;
00296 }
00297 
00298 
00299 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
00300     unzFile uf;
00301     const int* popt_extract_without_path;
00302     int* popt_overwrite;
00303     const char* password;
00304 {
00305     char filename_inzip[256];
00306     char* filename_withoutpath;
00307     char* p;
00308     int err=UNZ_OK;
00309     FILE *fout=NULL;
00310     void* buf;
00311     uInt size_buf;
00312 
00313     unz_file_info64 file_info;
00314     uLong ratio=0;
00315     err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
00316 
00317     if (err!=UNZ_OK)
00318     {
00319         printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
00320         return err;
00321     }
00322 
00323     size_buf = WRITEBUFFERSIZE;
00324     buf = (void*)malloc(size_buf);
00325     if (buf==NULL)
00326     {
00327         printf("Error allocating memory\n");
00328         return UNZ_INTERNALERROR;
00329     }
00330 
00331     p = filename_withoutpath = filename_inzip;
00332     while ((*p) != '\0')
00333     {
00334         if (((*p)=='/') || ((*p)=='\\'))
00335             filename_withoutpath = p+1;
00336         p++;
00337     }
00338 
00339     if ((*filename_withoutpath)=='\0')
00340     {
00341         if ((*popt_extract_without_path)==0)
00342         {
00343             printf("creating directory: %s\n",filename_inzip);
00344             mymkdir(filename_inzip);
00345         }
00346     }
00347     else
00348     {
00349         const char* write_filename;
00350         int skip=0;
00351 
00352         if ((*popt_extract_without_path)==0)
00353             write_filename = filename_inzip;
00354         else
00355             write_filename = filename_withoutpath;
00356 
00357         err = unzOpenCurrentFilePassword(uf,password);
00358         if (err!=UNZ_OK)
00359         {
00360             printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
00361         }
00362 
00363         if (((*popt_overwrite)==0) && (err==UNZ_OK))
00364         {
00365             char rep=0;
00366             FILE* ftestexist;
00367             ftestexist = fopen64(write_filename,"rb");
00368             if (ftestexist!=NULL)
00369             {
00370                 fclose(ftestexist);
00371                 do
00372                 {
00373                     char answer[128];
00374                     int ret;
00375 
00376                     printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
00377                     ret = scanf("%1s",answer);
00378                     if (ret != 1)
00379                     {
00380                        exit(EXIT_FAILURE);
00381                     }
00382                     rep = answer[0] ;
00383                     if ((rep>='a') && (rep<='z'))
00384                         rep -= 0x20;
00385                 }
00386                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
00387             }
00388 
00389             if (rep == 'N')
00390                 skip = 1;
00391 
00392             if (rep == 'A')
00393                 *popt_overwrite=1;
00394         }
00395 
00396         if ((skip==0) && (err==UNZ_OK))
00397         {
00398             fout=fopen64(write_filename,"wb");
00399 
00400             /* some zipfile don't contain directory alone before file */
00401             if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
00402                                 (filename_withoutpath!=(char*)filename_inzip))
00403             {
00404                 char c=*(filename_withoutpath-1);
00405                 *(filename_withoutpath-1)='\0';
00406                 makedir(write_filename);
00407                 *(filename_withoutpath-1)=c;
00408                 fout=fopen64(write_filename,"wb");
00409             }
00410 
00411             if (fout==NULL)
00412             {
00413                 printf("error opening %s\n",write_filename);
00414             }
00415         }
00416 
00417         if (fout!=NULL)
00418         {
00419             printf(" extracting: %s\n",write_filename);
00420 
00421             do
00422             {
00423                 err = unzReadCurrentFile(uf,buf,size_buf);
00424                 if (err<0)
00425                 {
00426                     printf("error %d with zipfile in unzReadCurrentFile\n",err);
00427                     break;
00428                 }
00429                 if (err>0)
00430                     if (fwrite(buf,err,1,fout)!=1)
00431                     {
00432                         printf("error in writing extracted file\n");
00433                         err=UNZ_ERRNO;
00434                         break;
00435                     }
00436             }
00437             while (err>0);
00438             if (fout)
00439                     fclose(fout);
00440 
00441             if (err==0)
00442                 change_file_date(write_filename,file_info.dosDate,
00443                                  file_info.tmu_date);
00444         }
00445 
00446         if (err==UNZ_OK)
00447         {
00448             err = unzCloseCurrentFile (uf);
00449             if (err!=UNZ_OK)
00450             {
00451                 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
00452             }
00453         }
00454         else
00455             unzCloseCurrentFile(uf); /* don't lose the error */
00456     }
00457 
00458     free(buf);
00459     return err;
00460 }
00461 
00462 
00463 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
00464     unzFile uf;
00465     int opt_extract_without_path;
00466     int opt_overwrite;
00467     const char* password;
00468 {
00469     uLong i;
00470     unz_global_info64 gi;
00471     int err;
00472     FILE* fout=NULL;
00473 
00474     err = unzGetGlobalInfo64(uf,&gi);
00475     if (err!=UNZ_OK)
00476         printf("error %d with zipfile in unzGetGlobalInfo \n",err);
00477 
00478     for (i=0;i<gi.number_entry;i++)
00479     {
00480         if (do_extract_currentfile(uf,&opt_extract_without_path,
00481                                       &opt_overwrite,
00482                                       password) != UNZ_OK)
00483             break;
00484 
00485         if ((i+1)<gi.number_entry)
00486         {
00487             err = unzGoToNextFile(uf);
00488             if (err!=UNZ_OK)
00489             {
00490                 printf("error %d with zipfile in unzGoToNextFile\n",err);
00491                 break;
00492             }
00493         }
00494     }
00495 
00496     return 0;
00497 }
00498 
00499 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
00500     unzFile uf;
00501     const char* filename;
00502     int opt_extract_without_path;
00503     int opt_overwrite;
00504     const char* password;
00505 {
00506     int err = UNZ_OK;
00507     if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
00508     {
00509         printf("file %s not found in the zipfile\n",filename);
00510         return 2;
00511     }
00512 
00513     if (do_extract_currentfile(uf,&opt_extract_without_path,
00514                                       &opt_overwrite,
00515                                       password) == UNZ_OK)
00516         return 0;
00517     else
00518         return 1;
00519 }
00520 
00521 
00522 int main(argc,argv)
00523     int argc;
00524     char *argv[];
00525 {
00526     const char *zipfilename=NULL;
00527     const char *filename_to_extract=NULL;
00528     const char *password=NULL;
00529     char filename_try[MAXFILENAME+16] = "";
00530     int i;
00531     int ret_value=0;
00532     int opt_do_list=0;
00533     int opt_do_extract=1;
00534     int opt_do_extract_withoutpath=0;
00535     int opt_overwrite=0;
00536     int opt_extractdir=0;
00537     const char *dirname=NULL;
00538     unzFile uf=NULL;
00539 
00540     do_banner();
00541     if (argc==1)
00542     {
00543         do_help();
00544         return 0;
00545     }
00546     else
00547     {
00548         for (i=1;i<argc;i++)
00549         {
00550             if ((*argv[i])=='-')
00551             {
00552                 const char *p=argv[i]+1;
00553 
00554                 while ((*p)!='\0')
00555                 {
00556                     char c=*(p++);;
00557                     if ((c=='l') || (c=='L'))
00558                         opt_do_list = 1;
00559                     if ((c=='v') || (c=='V'))
00560                         opt_do_list = 1;
00561                     if ((c=='x') || (c=='X'))
00562                         opt_do_extract = 1;
00563                     if ((c=='e') || (c=='E'))
00564                         opt_do_extract = opt_do_extract_withoutpath = 1;
00565                     if ((c=='o') || (c=='O'))
00566                         opt_overwrite=1;
00567                     if ((c=='d') || (c=='D'))
00568                     {
00569                         opt_extractdir=1;
00570                         dirname=argv[i+1];
00571                     }
00572 
00573                     if (((c=='p') || (c=='P')) && (i+1<argc))
00574                     {
00575                         password=argv[i+1];
00576                         i++;
00577                     }
00578                 }
00579             }
00580             else
00581             {
00582                 if (zipfilename == NULL)
00583                     zipfilename = argv[i];
00584                 else if ((filename_to_extract==NULL) && (!opt_extractdir))
00585                         filename_to_extract = argv[i] ;
00586             }
00587         }
00588     }
00589 
00590     if (zipfilename!=NULL)
00591     {
00592 
00593 #        ifdef USEWIN32IOAPI
00594         zlib_filefunc64_def ffunc;
00595 #        endif
00596 
00597         strncpy(filename_try, zipfilename,MAXFILENAME-1);
00598         /* strncpy doesnt append the trailing NULL, of the string is too long. */
00599         filename_try[ MAXFILENAME ] = '\0';
00600 
00601 #        ifdef USEWIN32IOAPI
00602         fill_win32_filefunc64A(&ffunc);
00603         uf = unzOpen2_64(zipfilename,&ffunc);
00604 #        else
00605         uf = unzOpen64(zipfilename);
00606 #        endif
00607         if (uf==NULL)
00608         {
00609             strcat(filename_try,".zip");
00610 #            ifdef USEWIN32IOAPI
00611             uf = unzOpen2_64(filename_try,&ffunc);
00612 #            else
00613             uf = unzOpen64(filename_try);
00614 #            endif
00615         }
00616     }
00617 
00618     if (uf==NULL)
00619     {
00620         printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
00621         return 1;
00622     }
00623     printf("%s opened\n",filename_try);
00624 
00625     if (opt_do_list==1)
00626         ret_value = do_list(uf);
00627     else if (opt_do_extract==1)
00628     {
00629 #ifdef _WIN32
00630         if (opt_extractdir && _chdir(dirname))
00631 #else
00632         if (opt_extractdir && chdir(dirname))
00633 #endif
00634         {
00635           printf("Error changing into %s, aborting\n", dirname);
00636           exit(-1);
00637         }
00638 
00639         if (filename_to_extract == NULL)
00640             ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
00641         else
00642             ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
00643     }
00644 
00645     unzClose(uf);
00646 
00647     return ret_value;
00648 }

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