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