Home | History | Annotate | Line # | Download | only in minizip
miniunz.c revision 1.1.1.2
      1      1.1  christos /*
      2      1.1  christos    miniunz.c
      3  1.1.1.2  christos    Version 1.1, February 14h, 2010
      4  1.1.1.2  christos    sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
      5  1.1.1.2  christos 
      6  1.1.1.2  christos          Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
      7  1.1.1.2  christos 
      8  1.1.1.2  christos          Modifications of Unzip for Zip64
      9  1.1.1.2  christos          Copyright (C) 2007-2008 Even Rouault
     10      1.1  christos 
     11  1.1.1.2  christos          Modifications for Zip64 support on both zip and unzip
     12  1.1.1.2  christos          Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
     13      1.1  christos */
     14      1.1  christos 
     15  1.1.1.2  christos #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
     16  1.1.1.2  christos         #ifndef __USE_FILE_OFFSET64
     17  1.1.1.2  christos                 #define __USE_FILE_OFFSET64
     18  1.1.1.2  christos         #endif
     19  1.1.1.2  christos         #ifndef __USE_LARGEFILE64
     20  1.1.1.2  christos                 #define __USE_LARGEFILE64
     21  1.1.1.2  christos         #endif
     22  1.1.1.2  christos         #ifndef _LARGEFILE64_SOURCE
     23  1.1.1.2  christos                 #define _LARGEFILE64_SOURCE
     24  1.1.1.2  christos         #endif
     25  1.1.1.2  christos         #ifndef _FILE_OFFSET_BIT
     26  1.1.1.2  christos                 #define _FILE_OFFSET_BIT 64
     27  1.1.1.2  christos         #endif
     28  1.1.1.2  christos #endif
     29  1.1.1.2  christos 
     30  1.1.1.2  christos #ifdef __APPLE__
     31  1.1.1.2  christos // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
     32  1.1.1.2  christos #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
     33  1.1.1.2  christos #define FTELLO_FUNC(stream) ftello(stream)
     34  1.1.1.2  christos #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
     35  1.1.1.2  christos #else
     36  1.1.1.2  christos #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
     37  1.1.1.2  christos #define FTELLO_FUNC(stream) ftello64(stream)
     38  1.1.1.2  christos #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
     39  1.1.1.2  christos #endif
     40  1.1.1.2  christos 
     41      1.1  christos 
     42      1.1  christos #include <stdio.h>
     43      1.1  christos #include <stdlib.h>
     44      1.1  christos #include <string.h>
     45      1.1  christos #include <time.h>
     46      1.1  christos #include <errno.h>
     47      1.1  christos #include <fcntl.h>
     48      1.1  christos 
     49  1.1.1.2  christos #ifdef _WIN32
     50      1.1  christos # include <direct.h>
     51      1.1  christos # include <io.h>
     52  1.1.1.2  christos #else
     53  1.1.1.2  christos # include <unistd.h>
     54  1.1.1.2  christos # include <utime.h>
     55      1.1  christos #endif
     56      1.1  christos 
     57  1.1.1.2  christos 
     58      1.1  christos #include "unzip.h"
     59      1.1  christos 
     60      1.1  christos #define CASESENSITIVITY (0)
     61      1.1  christos #define WRITEBUFFERSIZE (8192)
     62      1.1  christos #define MAXFILENAME (256)
     63      1.1  christos 
     64  1.1.1.2  christos #ifdef _WIN32
     65      1.1  christos #define USEWIN32IOAPI
     66      1.1  christos #include "iowin32.h"
     67      1.1  christos #endif
     68      1.1  christos /*
     69      1.1  christos   mini unzip, demo of unzip package
     70      1.1  christos 
     71      1.1  christos   usage :
     72      1.1  christos   Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
     73      1.1  christos 
     74      1.1  christos   list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
     75      1.1  christos     if it exists
     76      1.1  christos */
     77      1.1  christos 
     78      1.1  christos 
     79      1.1  christos /* change_file_date : change the date/time of a file
     80      1.1  christos     filename : the filename of the file where date/time must be modified
     81      1.1  christos     dosdate : the new date at the MSDos format (4 bytes)
     82      1.1  christos     tmu_date : the SAME new date at the tm_unz format */
     83      1.1  christos void change_file_date(filename,dosdate,tmu_date)
     84      1.1  christos     const char *filename;
     85      1.1  christos     uLong dosdate;
     86      1.1  christos     tm_unz tmu_date;
     87      1.1  christos {
     88  1.1.1.2  christos #ifdef _WIN32
     89      1.1  christos   HANDLE hFile;
     90      1.1  christos   FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
     91      1.1  christos 
     92  1.1.1.2  christos   hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
     93      1.1  christos                       0,NULL,OPEN_EXISTING,0,NULL);
     94      1.1  christos   GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
     95      1.1  christos   DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
     96      1.1  christos   LocalFileTimeToFileTime(&ftLocal,&ftm);
     97      1.1  christos   SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
     98      1.1  christos   CloseHandle(hFile);
     99      1.1  christos #else
    100  1.1.1.2  christos #ifdef unix || __APPLE__
    101      1.1  christos   struct utimbuf ut;
    102      1.1  christos   struct tm newdate;
    103      1.1  christos   newdate.tm_sec = tmu_date.tm_sec;
    104      1.1  christos   newdate.tm_min=tmu_date.tm_min;
    105      1.1  christos   newdate.tm_hour=tmu_date.tm_hour;
    106      1.1  christos   newdate.tm_mday=tmu_date.tm_mday;
    107      1.1  christos   newdate.tm_mon=tmu_date.tm_mon;
    108      1.1  christos   if (tmu_date.tm_year > 1900)
    109      1.1  christos       newdate.tm_year=tmu_date.tm_year - 1900;
    110      1.1  christos   else
    111      1.1  christos       newdate.tm_year=tmu_date.tm_year ;
    112      1.1  christos   newdate.tm_isdst=-1;
    113      1.1  christos 
    114      1.1  christos   ut.actime=ut.modtime=mktime(&newdate);
    115      1.1  christos   utime(filename,&ut);
    116      1.1  christos #endif
    117      1.1  christos #endif
    118      1.1  christos }
    119      1.1  christos 
    120      1.1  christos 
    121      1.1  christos /* mymkdir and change_file_date are not 100 % portable
    122      1.1  christos    As I don't know well Unix, I wait feedback for the unix portion */
    123      1.1  christos 
    124      1.1  christos int mymkdir(dirname)
    125      1.1  christos     const char* dirname;
    126      1.1  christos {
    127      1.1  christos     int ret=0;
    128  1.1.1.2  christos #ifdef _WIN32
    129  1.1.1.2  christos     ret = _mkdir(dirname);
    130  1.1.1.2  christos #elif unix
    131  1.1.1.2  christos     ret = mkdir (dirname,0775);
    132  1.1.1.2  christos #elif __APPLE__
    133      1.1  christos     ret = mkdir (dirname,0775);
    134      1.1  christos #endif
    135      1.1  christos     return ret;
    136      1.1  christos }
    137      1.1  christos 
    138      1.1  christos int makedir (newdir)
    139      1.1  christos     char *newdir;
    140      1.1  christos {
    141      1.1  christos   char *buffer ;
    142      1.1  christos   char *p;
    143      1.1  christos   int  len = (int)strlen(newdir);
    144      1.1  christos 
    145      1.1  christos   if (len <= 0)
    146      1.1  christos     return 0;
    147      1.1  christos 
    148      1.1  christos   buffer = (char*)malloc(len+1);
    149  1.1.1.2  christos         if (buffer==NULL)
    150  1.1.1.2  christos         {
    151  1.1.1.2  christos                 printf("Error allocating memory\n");
    152  1.1.1.2  christos                 return UNZ_INTERNALERROR;
    153  1.1.1.2  christos         }
    154      1.1  christos   strcpy(buffer,newdir);
    155      1.1  christos 
    156      1.1  christos   if (buffer[len-1] == '/') {
    157      1.1  christos     buffer[len-1] = '\0';
    158      1.1  christos   }
    159      1.1  christos   if (mymkdir(buffer) == 0)
    160      1.1  christos     {
    161      1.1  christos       free(buffer);
    162      1.1  christos       return 1;
    163      1.1  christos     }
    164      1.1  christos 
    165      1.1  christos   p = buffer+1;
    166      1.1  christos   while (1)
    167      1.1  christos     {
    168      1.1  christos       char hold;
    169      1.1  christos 
    170      1.1  christos       while(*p && *p != '\\' && *p != '/')
    171      1.1  christos         p++;
    172      1.1  christos       hold = *p;
    173      1.1  christos       *p = 0;
    174      1.1  christos       if ((mymkdir(buffer) == -1) && (errno == ENOENT))
    175      1.1  christos         {
    176      1.1  christos           printf("couldn't create directory %s\n",buffer);
    177      1.1  christos           free(buffer);
    178      1.1  christos           return 0;
    179      1.1  christos         }
    180      1.1  christos       if (hold == 0)
    181      1.1  christos         break;
    182      1.1  christos       *p++ = hold;
    183      1.1  christos     }
    184      1.1  christos   free(buffer);
    185      1.1  christos   return 1;
    186      1.1  christos }
    187      1.1  christos 
    188      1.1  christos void do_banner()
    189      1.1  christos {
    190      1.1  christos     printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
    191      1.1  christos     printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
    192      1.1  christos }
    193      1.1  christos 
    194      1.1  christos void do_help()
    195      1.1  christos {
    196      1.1  christos     printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
    197      1.1  christos            "  -e  Extract without pathname (junk paths)\n" \
    198      1.1  christos            "  -x  Extract with pathname\n" \
    199      1.1  christos            "  -v  list files\n" \
    200      1.1  christos            "  -l  list files\n" \
    201      1.1  christos            "  -d  directory to extract into\n" \
    202      1.1  christos            "  -o  overwrite files without prompting\n" \
    203      1.1  christos            "  -p  extract crypted file using password\n\n");
    204      1.1  christos }
    205      1.1  christos 
    206  1.1.1.2  christos void Display64BitsSize(ZPOS64_T n, int size_char)
    207  1.1.1.2  christos {
    208  1.1.1.2  christos   /* to avoid compatibility problem , we do here the conversion */
    209  1.1.1.2  christos   char number[21];
    210  1.1.1.2  christos   int offset=19;
    211  1.1.1.2  christos   int pos_string = 19;
    212  1.1.1.2  christos   number[20]=0;
    213  1.1.1.2  christos   for (;;) {
    214  1.1.1.2  christos       number[offset]=(char)((n%10)+'0');
    215  1.1.1.2  christos       if (number[offset] != '0')
    216  1.1.1.2  christos           pos_string=offset;
    217  1.1.1.2  christos       n/=10;
    218  1.1.1.2  christos       if (offset==0)
    219  1.1.1.2  christos           break;
    220  1.1.1.2  christos       offset--;
    221  1.1.1.2  christos   }
    222  1.1.1.2  christos   {
    223  1.1.1.2  christos       int size_display_string = 19-pos_string;
    224  1.1.1.2  christos       while (size_char > size_display_string)
    225  1.1.1.2  christos       {
    226  1.1.1.2  christos           size_char--;
    227  1.1.1.2  christos           printf(" ");
    228  1.1.1.2  christos       }
    229  1.1.1.2  christos   }
    230  1.1.1.2  christos 
    231  1.1.1.2  christos   printf("%s",&number[pos_string]);
    232  1.1.1.2  christos }
    233      1.1  christos 
    234      1.1  christos int do_list(uf)
    235      1.1  christos     unzFile uf;
    236      1.1  christos {
    237      1.1  christos     uLong i;
    238  1.1.1.2  christos     unz_global_info64 gi;
    239      1.1  christos     int err;
    240      1.1  christos 
    241  1.1.1.2  christos     err = unzGetGlobalInfo64(uf,&gi);
    242      1.1  christos     if (err!=UNZ_OK)
    243      1.1  christos         printf("error %d with zipfile in unzGetGlobalInfo \n",err);
    244  1.1.1.2  christos     printf("  Length  Method     Size Ratio   Date    Time   CRC-32     Name\n");
    245  1.1.1.2  christos     printf("  ------  ------     ---- -----   ----    ----   ------     ----\n");
    246      1.1  christos     for (i=0;i<gi.number_entry;i++)
    247      1.1  christos     {
    248      1.1  christos         char filename_inzip[256];
    249  1.1.1.2  christos         unz_file_info64 file_info;
    250      1.1  christos         uLong ratio=0;
    251      1.1  christos         const char *string_method;
    252      1.1  christos         char charCrypt=' ';
    253  1.1.1.2  christos         err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
    254      1.1  christos         if (err!=UNZ_OK)
    255      1.1  christos         {
    256      1.1  christos             printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
    257      1.1  christos             break;
    258      1.1  christos         }
    259      1.1  christos         if (file_info.uncompressed_size>0)
    260  1.1.1.2  christos             ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
    261      1.1  christos 
    262      1.1  christos         /* display a '*' if the file is crypted */
    263      1.1  christos         if ((file_info.flag & 1) != 0)
    264      1.1  christos             charCrypt='*';
    265      1.1  christos 
    266      1.1  christos         if (file_info.compression_method==0)
    267      1.1  christos             string_method="Stored";
    268      1.1  christos         else
    269      1.1  christos         if (file_info.compression_method==Z_DEFLATED)
    270      1.1  christos         {
    271      1.1  christos             uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
    272      1.1  christos             if (iLevel==0)
    273      1.1  christos               string_method="Defl:N";
    274      1.1  christos             else if (iLevel==1)
    275      1.1  christos               string_method="Defl:X";
    276      1.1  christos             else if ((iLevel==2) || (iLevel==3))
    277      1.1  christos               string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
    278      1.1  christos         }
    279      1.1  christos         else
    280  1.1.1.2  christos         if (file_info.compression_method==Z_BZIP2ED)
    281  1.1.1.2  christos         {
    282  1.1.1.2  christos               string_method="BZip2 ";
    283  1.1.1.2  christos         }
    284  1.1.1.2  christos         else
    285      1.1  christos             string_method="Unkn. ";
    286      1.1  christos 
    287  1.1.1.2  christos         Display64BitsSize(file_info.uncompressed_size,7);
    288  1.1.1.2  christos         printf("  %6s%c",string_method,charCrypt);
    289  1.1.1.2  christos         Display64BitsSize(file_info.compressed_size,7);
    290  1.1.1.2  christos         printf(" %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
    291      1.1  christos                 ratio,
    292      1.1  christos                 (uLong)file_info.tmu_date.tm_mon + 1,
    293      1.1  christos                 (uLong)file_info.tmu_date.tm_mday,
    294      1.1  christos                 (uLong)file_info.tmu_date.tm_year % 100,
    295      1.1  christos                 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
    296      1.1  christos                 (uLong)file_info.crc,filename_inzip);
    297      1.1  christos         if ((i+1)<gi.number_entry)
    298      1.1  christos         {
    299      1.1  christos             err = unzGoToNextFile(uf);
    300      1.1  christos             if (err!=UNZ_OK)
    301      1.1  christos             {
    302      1.1  christos                 printf("error %d with zipfile in unzGoToNextFile\n",err);
    303      1.1  christos                 break;
    304      1.1  christos             }
    305      1.1  christos         }
    306      1.1  christos     }
    307      1.1  christos 
    308      1.1  christos     return 0;
    309      1.1  christos }
    310      1.1  christos 
    311      1.1  christos 
    312      1.1  christos int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
    313      1.1  christos     unzFile uf;
    314      1.1  christos     const int* popt_extract_without_path;
    315      1.1  christos     int* popt_overwrite;
    316      1.1  christos     const char* password;
    317      1.1  christos {
    318      1.1  christos     char filename_inzip[256];
    319      1.1  christos     char* filename_withoutpath;
    320      1.1  christos     char* p;
    321      1.1  christos     int err=UNZ_OK;
    322      1.1  christos     FILE *fout=NULL;
    323      1.1  christos     void* buf;
    324      1.1  christos     uInt size_buf;
    325      1.1  christos 
    326  1.1.1.2  christos     unz_file_info64 file_info;
    327      1.1  christos     uLong ratio=0;
    328  1.1.1.2  christos     err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
    329      1.1  christos 
    330      1.1  christos     if (err!=UNZ_OK)
    331      1.1  christos     {
    332      1.1  christos         printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
    333      1.1  christos         return err;
    334      1.1  christos     }
    335      1.1  christos 
    336      1.1  christos     size_buf = WRITEBUFFERSIZE;
    337      1.1  christos     buf = (void*)malloc(size_buf);
    338      1.1  christos     if (buf==NULL)
    339      1.1  christos     {
    340      1.1  christos         printf("Error allocating memory\n");
    341      1.1  christos         return UNZ_INTERNALERROR;
    342      1.1  christos     }
    343      1.1  christos 
    344      1.1  christos     p = filename_withoutpath = filename_inzip;
    345      1.1  christos     while ((*p) != '\0')
    346      1.1  christos     {
    347      1.1  christos         if (((*p)=='/') || ((*p)=='\\'))
    348      1.1  christos             filename_withoutpath = p+1;
    349      1.1  christos         p++;
    350      1.1  christos     }
    351      1.1  christos 
    352      1.1  christos     if ((*filename_withoutpath)=='\0')
    353      1.1  christos     {
    354      1.1  christos         if ((*popt_extract_without_path)==0)
    355      1.1  christos         {
    356      1.1  christos             printf("creating directory: %s\n",filename_inzip);
    357      1.1  christos             mymkdir(filename_inzip);
    358      1.1  christos         }
    359      1.1  christos     }
    360      1.1  christos     else
    361      1.1  christos     {
    362      1.1  christos         const char* write_filename;
    363      1.1  christos         int skip=0;
    364      1.1  christos 
    365      1.1  christos         if ((*popt_extract_without_path)==0)
    366      1.1  christos             write_filename = filename_inzip;
    367      1.1  christos         else
    368      1.1  christos             write_filename = filename_withoutpath;
    369      1.1  christos 
    370      1.1  christos         err = unzOpenCurrentFilePassword(uf,password);
    371      1.1  christos         if (err!=UNZ_OK)
    372      1.1  christos         {
    373      1.1  christos             printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
    374      1.1  christos         }
    375      1.1  christos 
    376      1.1  christos         if (((*popt_overwrite)==0) && (err==UNZ_OK))
    377      1.1  christos         {
    378      1.1  christos             char rep=0;
    379      1.1  christos             FILE* ftestexist;
    380  1.1.1.2  christos             ftestexist = FOPEN_FUNC(write_filename,"rb");
    381      1.1  christos             if (ftestexist!=NULL)
    382      1.1  christos             {
    383      1.1  christos                 fclose(ftestexist);
    384      1.1  christos                 do
    385      1.1  christos                 {
    386      1.1  christos                     char answer[128];
    387      1.1  christos                     int ret;
    388      1.1  christos 
    389      1.1  christos                     printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
    390      1.1  christos                     ret = scanf("%1s",answer);
    391  1.1.1.2  christos                     if (ret != 1)
    392      1.1  christos                     {
    393      1.1  christos                        exit(EXIT_FAILURE);
    394      1.1  christos                     }
    395      1.1  christos                     rep = answer[0] ;
    396      1.1  christos                     if ((rep>='a') && (rep<='z'))
    397      1.1  christos                         rep -= 0x20;
    398      1.1  christos                 }
    399      1.1  christos                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
    400      1.1  christos             }
    401      1.1  christos 
    402      1.1  christos             if (rep == 'N')
    403      1.1  christos                 skip = 1;
    404      1.1  christos 
    405      1.1  christos             if (rep == 'A')
    406      1.1  christos                 *popt_overwrite=1;
    407      1.1  christos         }
    408      1.1  christos 
    409      1.1  christos         if ((skip==0) && (err==UNZ_OK))
    410      1.1  christos         {
    411  1.1.1.2  christos             fout=FOPEN_FUNC(write_filename,"wb");
    412      1.1  christos             /* some zipfile don't contain directory alone before file */
    413      1.1  christos             if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
    414      1.1  christos                                 (filename_withoutpath!=(char*)filename_inzip))
    415      1.1  christos             {
    416      1.1  christos                 char c=*(filename_withoutpath-1);
    417      1.1  christos                 *(filename_withoutpath-1)='\0';
    418      1.1  christos                 makedir(write_filename);
    419      1.1  christos                 *(filename_withoutpath-1)=c;
    420  1.1.1.2  christos                 fout=FOPEN_FUNC(write_filename,"wb");
    421      1.1  christos             }
    422      1.1  christos 
    423      1.1  christos             if (fout==NULL)
    424      1.1  christos             {
    425      1.1  christos                 printf("error opening %s\n",write_filename);
    426      1.1  christos             }
    427      1.1  christos         }
    428      1.1  christos 
    429      1.1  christos         if (fout!=NULL)
    430      1.1  christos         {
    431      1.1  christos             printf(" extracting: %s\n",write_filename);
    432      1.1  christos 
    433      1.1  christos             do
    434      1.1  christos             {
    435      1.1  christos                 err = unzReadCurrentFile(uf,buf,size_buf);
    436      1.1  christos                 if (err<0)
    437      1.1  christos                 {
    438      1.1  christos                     printf("error %d with zipfile in unzReadCurrentFile\n",err);
    439      1.1  christos                     break;
    440      1.1  christos                 }
    441      1.1  christos                 if (err>0)
    442      1.1  christos                     if (fwrite(buf,err,1,fout)!=1)
    443      1.1  christos                     {
    444      1.1  christos                         printf("error in writing extracted file\n");
    445      1.1  christos                         err=UNZ_ERRNO;
    446      1.1  christos                         break;
    447      1.1  christos                     }
    448      1.1  christos             }
    449      1.1  christos             while (err>0);
    450      1.1  christos             if (fout)
    451      1.1  christos                     fclose(fout);
    452      1.1  christos 
    453      1.1  christos             if (err==0)
    454      1.1  christos                 change_file_date(write_filename,file_info.dosDate,
    455      1.1  christos                                  file_info.tmu_date);
    456      1.1  christos         }
    457      1.1  christos 
    458      1.1  christos         if (err==UNZ_OK)
    459      1.1  christos         {
    460      1.1  christos             err = unzCloseCurrentFile (uf);
    461      1.1  christos             if (err!=UNZ_OK)
    462      1.1  christos             {
    463      1.1  christos                 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
    464      1.1  christos             }
    465      1.1  christos         }
    466      1.1  christos         else
    467      1.1  christos             unzCloseCurrentFile(uf); /* don't lose the error */
    468      1.1  christos     }
    469      1.1  christos 
    470      1.1  christos     free(buf);
    471      1.1  christos     return err;
    472      1.1  christos }
    473      1.1  christos 
    474      1.1  christos 
    475      1.1  christos int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
    476      1.1  christos     unzFile uf;
    477      1.1  christos     int opt_extract_without_path;
    478      1.1  christos     int opt_overwrite;
    479      1.1  christos     const char* password;
    480      1.1  christos {
    481      1.1  christos     uLong i;
    482  1.1.1.2  christos     unz_global_info64 gi;
    483      1.1  christos     int err;
    484      1.1  christos     FILE* fout=NULL;
    485      1.1  christos 
    486  1.1.1.2  christos     err = unzGetGlobalInfo64(uf,&gi);
    487      1.1  christos     if (err!=UNZ_OK)
    488      1.1  christos         printf("error %d with zipfile in unzGetGlobalInfo \n",err);
    489      1.1  christos 
    490      1.1  christos     for (i=0;i<gi.number_entry;i++)
    491      1.1  christos     {
    492      1.1  christos         if (do_extract_currentfile(uf,&opt_extract_without_path,
    493      1.1  christos                                       &opt_overwrite,
    494      1.1  christos                                       password) != UNZ_OK)
    495      1.1  christos             break;
    496      1.1  christos 
    497      1.1  christos         if ((i+1)<gi.number_entry)
    498      1.1  christos         {
    499      1.1  christos             err = unzGoToNextFile(uf);
    500      1.1  christos             if (err!=UNZ_OK)
    501      1.1  christos             {
    502      1.1  christos                 printf("error %d with zipfile in unzGoToNextFile\n",err);
    503      1.1  christos                 break;
    504      1.1  christos             }
    505      1.1  christos         }
    506      1.1  christos     }
    507      1.1  christos 
    508      1.1  christos     return 0;
    509      1.1  christos }
    510      1.1  christos 
    511      1.1  christos int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
    512      1.1  christos     unzFile uf;
    513      1.1  christos     const char* filename;
    514      1.1  christos     int opt_extract_without_path;
    515      1.1  christos     int opt_overwrite;
    516      1.1  christos     const char* password;
    517      1.1  christos {
    518      1.1  christos     int err = UNZ_OK;
    519      1.1  christos     if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
    520      1.1  christos     {
    521      1.1  christos         printf("file %s not found in the zipfile\n",filename);
    522      1.1  christos         return 2;
    523      1.1  christos     }
    524      1.1  christos 
    525      1.1  christos     if (do_extract_currentfile(uf,&opt_extract_without_path,
    526      1.1  christos                                       &opt_overwrite,
    527      1.1  christos                                       password) == UNZ_OK)
    528      1.1  christos         return 0;
    529      1.1  christos     else
    530      1.1  christos         return 1;
    531      1.1  christos }
    532      1.1  christos 
    533      1.1  christos 
    534      1.1  christos int main(argc,argv)
    535      1.1  christos     int argc;
    536      1.1  christos     char *argv[];
    537      1.1  christos {
    538      1.1  christos     const char *zipfilename=NULL;
    539      1.1  christos     const char *filename_to_extract=NULL;
    540      1.1  christos     const char *password=NULL;
    541      1.1  christos     char filename_try[MAXFILENAME+16] = "";
    542      1.1  christos     int i;
    543  1.1.1.2  christos     int ret_value=0;
    544      1.1  christos     int opt_do_list=0;
    545      1.1  christos     int opt_do_extract=1;
    546      1.1  christos     int opt_do_extract_withoutpath=0;
    547      1.1  christos     int opt_overwrite=0;
    548      1.1  christos     int opt_extractdir=0;
    549      1.1  christos     const char *dirname=NULL;
    550      1.1  christos     unzFile uf=NULL;
    551      1.1  christos 
    552      1.1  christos     do_banner();
    553      1.1  christos     if (argc==1)
    554      1.1  christos     {
    555      1.1  christos         do_help();
    556      1.1  christos         return 0;
    557      1.1  christos     }
    558      1.1  christos     else
    559      1.1  christos     {
    560      1.1  christos         for (i=1;i<argc;i++)
    561      1.1  christos         {
    562      1.1  christos             if ((*argv[i])=='-')
    563      1.1  christos             {
    564      1.1  christos                 const char *p=argv[i]+1;
    565      1.1  christos 
    566      1.1  christos                 while ((*p)!='\0')
    567      1.1  christos                 {
    568      1.1  christos                     char c=*(p++);;
    569      1.1  christos                     if ((c=='l') || (c=='L'))
    570      1.1  christos                         opt_do_list = 1;
    571      1.1  christos                     if ((c=='v') || (c=='V'))
    572      1.1  christos                         opt_do_list = 1;
    573      1.1  christos                     if ((c=='x') || (c=='X'))
    574      1.1  christos                         opt_do_extract = 1;
    575      1.1  christos                     if ((c=='e') || (c=='E'))
    576      1.1  christos                         opt_do_extract = opt_do_extract_withoutpath = 1;
    577      1.1  christos                     if ((c=='o') || (c=='O'))
    578      1.1  christos                         opt_overwrite=1;
    579      1.1  christos                     if ((c=='d') || (c=='D'))
    580      1.1  christos                     {
    581      1.1  christos                         opt_extractdir=1;
    582      1.1  christos                         dirname=argv[i+1];
    583      1.1  christos                     }
    584      1.1  christos 
    585      1.1  christos                     if (((c=='p') || (c=='P')) && (i+1<argc))
    586      1.1  christos                     {
    587      1.1  christos                         password=argv[i+1];
    588      1.1  christos                         i++;
    589      1.1  christos                     }
    590      1.1  christos                 }
    591      1.1  christos             }
    592      1.1  christos             else
    593      1.1  christos             {
    594      1.1  christos                 if (zipfilename == NULL)
    595      1.1  christos                     zipfilename = argv[i];
    596      1.1  christos                 else if ((filename_to_extract==NULL) && (!opt_extractdir))
    597      1.1  christos                         filename_to_extract = argv[i] ;
    598      1.1  christos             }
    599      1.1  christos         }
    600      1.1  christos     }
    601      1.1  christos 
    602      1.1  christos     if (zipfilename!=NULL)
    603      1.1  christos     {
    604      1.1  christos 
    605      1.1  christos #        ifdef USEWIN32IOAPI
    606  1.1.1.2  christos         zlib_filefunc64_def ffunc;
    607      1.1  christos #        endif
    608      1.1  christos 
    609      1.1  christos         strncpy(filename_try, zipfilename,MAXFILENAME-1);
    610      1.1  christos         /* strncpy doesnt append the trailing NULL, of the string is too long. */
    611      1.1  christos         filename_try[ MAXFILENAME ] = '\0';
    612      1.1  christos 
    613      1.1  christos #        ifdef USEWIN32IOAPI
    614  1.1.1.2  christos         fill_win32_filefunc64A(&ffunc);
    615  1.1.1.2  christos         uf = unzOpen2_64(zipfilename,&ffunc);
    616      1.1  christos #        else
    617  1.1.1.2  christos         uf = unzOpen64(zipfilename);
    618      1.1  christos #        endif
    619      1.1  christos         if (uf==NULL)
    620      1.1  christos         {
    621      1.1  christos             strcat(filename_try,".zip");
    622      1.1  christos #            ifdef USEWIN32IOAPI
    623  1.1.1.2  christos             uf = unzOpen2_64(filename_try,&ffunc);
    624      1.1  christos #            else
    625  1.1.1.2  christos             uf = unzOpen64(filename_try);
    626      1.1  christos #            endif
    627      1.1  christos         }
    628      1.1  christos     }
    629      1.1  christos 
    630      1.1  christos     if (uf==NULL)
    631      1.1  christos     {
    632      1.1  christos         printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
    633      1.1  christos         return 1;
    634      1.1  christos     }
    635      1.1  christos     printf("%s opened\n",filename_try);
    636      1.1  christos 
    637      1.1  christos     if (opt_do_list==1)
    638  1.1.1.2  christos         ret_value = do_list(uf);
    639      1.1  christos     else if (opt_do_extract==1)
    640      1.1  christos     {
    641  1.1.1.2  christos #ifdef _WIN32
    642  1.1.1.2  christos         if (opt_extractdir && _chdir(dirname))
    643  1.1.1.2  christos #else
    644  1.1.1.2  christos         if (opt_extractdir && chdir(dirname))
    645  1.1.1.2  christos #endif
    646      1.1  christos         {
    647      1.1  christos           printf("Error changing into %s, aborting\n", dirname);
    648      1.1  christos           exit(-1);
    649      1.1  christos         }
    650      1.1  christos 
    651      1.1  christos         if (filename_to_extract == NULL)
    652  1.1.1.2  christos             ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
    653      1.1  christos         else
    654  1.1.1.2  christos             ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
    655      1.1  christos     }
    656      1.1  christos 
    657  1.1.1.2  christos     unzClose(uf);
    658  1.1.1.2  christos 
    659  1.1.1.2  christos     return ret_value;
    660      1.1  christos }
    661