kusano fc6ab3
/*
kusano fc6ab3
   miniunz.c
kusano fc6ab3
   Version 1.1, February 14h, 2010
kusano fc6ab3
   sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
kusano fc6ab3
kusano fc6ab3
         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
kusano fc6ab3
kusano fc6ab3
         Modifications of Unzip for Zip64
kusano fc6ab3
         Copyright (C) 2007-2008 Even Rouault
kusano fc6ab3
kusano fc6ab3
         Modifications for Zip64 support on both zip and unzip
kusano fc6ab3
         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
kusano fc6ab3
*/
kusano fc6ab3
kusano fc6ab3
#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
kusano fc6ab3
        #ifndef __USE_FILE_OFFSET64
kusano fc6ab3
                #define __USE_FILE_OFFSET64
kusano fc6ab3
        #endif
kusano fc6ab3
        #ifndef __USE_LARGEFILE64
kusano fc6ab3
                #define __USE_LARGEFILE64
kusano fc6ab3
        #endif
kusano fc6ab3
        #ifndef _LARGEFILE64_SOURCE
kusano fc6ab3
                #define _LARGEFILE64_SOURCE
kusano fc6ab3
        #endif
kusano fc6ab3
        #ifndef _FILE_OFFSET_BIT
kusano fc6ab3
                #define _FILE_OFFSET_BIT 64
kusano fc6ab3
        #endif
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
#ifdef __APPLE__
kusano fc6ab3
// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
kusano fc6ab3
#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
kusano fc6ab3
#define FTELLO_FUNC(stream) ftello(stream)
kusano fc6ab3
#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
kusano fc6ab3
#else
kusano fc6ab3
#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
kusano fc6ab3
#define FTELLO_FUNC(stream) ftello64(stream)
kusano fc6ab3
#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
#include <stdio.h></stdio.h>
kusano fc6ab3
#include <stdlib.h></stdlib.h>
kusano fc6ab3
#include <string.h></string.h>
kusano fc6ab3
#include <time.h></time.h>
kusano fc6ab3
#include <errno.h></errno.h>
kusano fc6ab3
#include <fcntl.h></fcntl.h>
kusano fc6ab3
kusano fc6ab3
#ifdef _WIN32
kusano fc6ab3
# include <direct.h></direct.h>
kusano fc6ab3
# include <io.h></io.h>
kusano fc6ab3
#else
kusano fc6ab3
# include <unistd.h></unistd.h>
kusano fc6ab3
# include <utime.h></utime.h>
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
#include "unzip.h"
kusano fc6ab3
kusano fc6ab3
#define CASESENSITIVITY (0)
kusano fc6ab3
#define WRITEBUFFERSIZE (8192)
kusano fc6ab3
#define MAXFILENAME (256)
kusano fc6ab3
kusano fc6ab3
#ifdef _WIN32
kusano fc6ab3
#define USEWIN32IOAPI
kusano fc6ab3
#include "iowin32.h"
kusano fc6ab3
#endif
kusano fc6ab3
/*
kusano fc6ab3
  mini unzip, demo of unzip package
kusano fc6ab3
kusano fc6ab3
  usage :
kusano fc6ab3
  Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
kusano fc6ab3
kusano fc6ab3
  list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
kusano fc6ab3
    if it exists
kusano fc6ab3
*/
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
/* change_file_date : change the date/time of a file
kusano fc6ab3
    filename : the filename of the file where date/time must be modified
kusano fc6ab3
    dosdate : the new date at the MSDos format (4 bytes)
kusano fc6ab3
    tmu_date : the SAME new date at the tm_unz format */
kusano fc6ab3
void change_file_date(filename,dosdate,tmu_date)
kusano fc6ab3
    const char *filename;
kusano fc6ab3
    uLong dosdate;
kusano fc6ab3
    tm_unz tmu_date;
kusano fc6ab3
{
kusano fc6ab3
#ifdef _WIN32
kusano fc6ab3
  HANDLE hFile;
kusano fc6ab3
  FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
kusano fc6ab3
kusano fc6ab3
  hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
kusano fc6ab3
                      0,NULL,OPEN_EXISTING,0,NULL);
kusano fc6ab3
  GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
kusano fc6ab3
  DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
kusano fc6ab3
  LocalFileTimeToFileTime(&ftLocal,&ftm);
kusano fc6ab3
  SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
kusano fc6ab3
  CloseHandle(hFile);
kusano fc6ab3
#else
kusano fc6ab3
#ifdef unix || __APPLE__
kusano fc6ab3
  struct utimbuf ut;
kusano fc6ab3
  struct tm newdate;
kusano fc6ab3
  newdate.tm_sec = tmu_date.tm_sec;
kusano fc6ab3
  newdate.tm_min=tmu_date.tm_min;
kusano fc6ab3
  newdate.tm_hour=tmu_date.tm_hour;
kusano fc6ab3
  newdate.tm_mday=tmu_date.tm_mday;
kusano fc6ab3
  newdate.tm_mon=tmu_date.tm_mon;
kusano fc6ab3
  if (tmu_date.tm_year > 1900)
kusano fc6ab3
      newdate.tm_year=tmu_date.tm_year - 1900;
kusano fc6ab3
  else
kusano fc6ab3
      newdate.tm_year=tmu_date.tm_year ;
kusano fc6ab3
  newdate.tm_isdst=-1;
kusano fc6ab3
kusano fc6ab3
  ut.actime=ut.modtime=mktime(&newdate);
kusano fc6ab3
  utime(filename,&ut);
kusano fc6ab3
#endif
kusano fc6ab3
#endif
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
/* mymkdir and change_file_date are not 100 % portable
kusano fc6ab3
   As I don't know well Unix, I wait feedback for the unix portion */
kusano fc6ab3
kusano fc6ab3
int mymkdir(dirname)
kusano fc6ab3
    const char* dirname;
kusano fc6ab3
{
kusano fc6ab3
    int ret=0;
kusano fc6ab3
#ifdef _WIN32
kusano fc6ab3
    ret = _mkdir(dirname);
kusano fc6ab3
#elif unix
kusano fc6ab3
    ret = mkdir (dirname,0775);
kusano fc6ab3
#elif __APPLE__
kusano fc6ab3
    ret = mkdir (dirname,0775);
kusano fc6ab3
#endif
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
int makedir (newdir)
kusano fc6ab3
    char *newdir;
kusano fc6ab3
{
kusano fc6ab3
  char *buffer ;
kusano fc6ab3
  char *p;
kusano fc6ab3
  int  len = (int)strlen(newdir);
kusano fc6ab3
kusano fc6ab3
  if (len <= 0)
kusano fc6ab3
    return 0;
kusano fc6ab3
kusano fc6ab3
  buffer = (char*)malloc(len+1);
kusano fc6ab3
        if (buffer==NULL)
kusano fc6ab3
        {
kusano fc6ab3
                printf("Error allocating memory\n");
kusano fc6ab3
                return UNZ_INTERNALERROR;
kusano fc6ab3
        }
kusano fc6ab3
  strcpy(buffer,newdir);
kusano fc6ab3
kusano fc6ab3
  if (buffer[len-1] == '/') {
kusano fc6ab3
    buffer[len-1] = '\0';
kusano fc6ab3
  }
kusano fc6ab3
  if (mymkdir(buffer) == 0)
kusano fc6ab3
    {
kusano fc6ab3
      free(buffer);
kusano fc6ab3
      return 1;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
  p = buffer+1;
kusano fc6ab3
  while (1)
kusano fc6ab3
    {
kusano fc6ab3
      char hold;
kusano fc6ab3
kusano fc6ab3
      while(*p && *p != '\\' && *p != '/')
kusano fc6ab3
        p++;
kusano fc6ab3
      hold = *p;
kusano fc6ab3
      *p = 0;
kusano fc6ab3
      if ((mymkdir(buffer) == -1) && (errno == ENOENT))
kusano fc6ab3
        {
kusano fc6ab3
          printf("couldn't create directory %s\n",buffer);
kusano fc6ab3
          free(buffer);
kusano fc6ab3
          return 0;
kusano fc6ab3
        }
kusano fc6ab3
      if (hold == 0)
kusano fc6ab3
        break;
kusano fc6ab3
      *p++ = hold;
kusano fc6ab3
    }
kusano fc6ab3
  free(buffer);
kusano fc6ab3
  return 1;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void do_banner()
kusano fc6ab3
{
kusano fc6ab3
    printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
kusano fc6ab3
    printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void do_help()
kusano fc6ab3
{
kusano fc6ab3
    printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
kusano fc6ab3
           "  -e  Extract without pathname (junk paths)\n" \
kusano fc6ab3
           "  -x  Extract with pathname\n" \
kusano fc6ab3
           "  -v  list files\n" \
kusano fc6ab3
           "  -l  list files\n" \
kusano fc6ab3
           "  -d  directory to extract into\n" \
kusano fc6ab3
           "  -o  overwrite files without prompting\n" \
kusano fc6ab3
           "  -p  extract crypted file using password\n\n");
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void Display64BitsSize(ZPOS64_T n, int size_char)
kusano fc6ab3
{
kusano fc6ab3
  /* to avoid compatibility problem , we do here the conversion */
kusano fc6ab3
  char number[21];
kusano fc6ab3
  int offset=19;
kusano fc6ab3
  int pos_string = 19;
kusano fc6ab3
  number[20]=0;
kusano fc6ab3
  for (;;) {
kusano fc6ab3
      number[offset]=(char)((n%10)+'0');
kusano fc6ab3
      if (number[offset] != '0')
kusano fc6ab3
          pos_string=offset;
kusano fc6ab3
      n/=10;
kusano fc6ab3
      if (offset==0)
kusano fc6ab3
          break;
kusano fc6ab3
      offset--;
kusano fc6ab3
  }
kusano fc6ab3
  {
kusano fc6ab3
      int size_display_string = 19-pos_string;
kusano fc6ab3
      while (size_char > size_display_string)
kusano fc6ab3
      {
kusano fc6ab3
          size_char--;
kusano fc6ab3
          printf(" ");
kusano fc6ab3
      }
kusano fc6ab3
  }
kusano fc6ab3
kusano fc6ab3
  printf("%s",&number[pos_string]);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
int do_list(uf)
kusano fc6ab3
    unzFile uf;
kusano fc6ab3
{
kusano fc6ab3
    uLong i;
kusano fc6ab3
    unz_global_info64 gi;
kusano fc6ab3
    int err;
kusano fc6ab3
kusano fc6ab3
    err = unzGetGlobalInfo64(uf,&gi);
kusano fc6ab3
    if (err!=UNZ_OK)
kusano fc6ab3
        printf("error %d with zipfile in unzGetGlobalInfo \n",err);
kusano fc6ab3
    printf("  Length  Method     Size Ratio   Date    Time   CRC-32     Name\n");
kusano fc6ab3
    printf("  ------  ------     ---- -----   ----    ----   ------     ----\n");
kusano fc6ab3
    for (i=0;i
kusano fc6ab3
    {
kusano fc6ab3
        char filename_inzip[256];
kusano fc6ab3
        unz_file_info64 file_info;
kusano fc6ab3
        uLong ratio=0;
kusano fc6ab3
        const char *string_method;
kusano fc6ab3
        char charCrypt=' ';
kusano fc6ab3
        err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
kusano fc6ab3
        if (err!=UNZ_OK)
kusano fc6ab3
        {
kusano fc6ab3
            printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
kusano fc6ab3
            break;
kusano fc6ab3
        }
kusano fc6ab3
        if (file_info.uncompressed_size>0)
kusano fc6ab3
            ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
kusano fc6ab3
kusano fc6ab3
        /* display a '*' if the file is crypted */
kusano fc6ab3
        if ((file_info.flag & 1) != 0)
kusano fc6ab3
            charCrypt='*';
kusano fc6ab3
kusano fc6ab3
        if (file_info.compression_method==0)
kusano fc6ab3
            string_method="Stored";
kusano fc6ab3
        else
kusano fc6ab3
        if (file_info.compression_method==Z_DEFLATED)
kusano fc6ab3
        {
kusano fc6ab3
            uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
kusano fc6ab3
            if (iLevel==0)
kusano fc6ab3
              string_method="Defl:N";
kusano fc6ab3
            else if (iLevel==1)
kusano fc6ab3
              string_method="Defl:X";
kusano fc6ab3
            else if ((iLevel==2) || (iLevel==3))
kusano fc6ab3
              string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
kusano fc6ab3
        }
kusano fc6ab3
        else
kusano fc6ab3
        if (file_info.compression_method==Z_BZIP2ED)
kusano fc6ab3
        {
kusano fc6ab3
              string_method="BZip2 ";
kusano fc6ab3
        }
kusano fc6ab3
        else
kusano fc6ab3
            string_method="Unkn. ";
kusano fc6ab3
kusano fc6ab3
        Display64BitsSize(file_info.uncompressed_size,7);
kusano fc6ab3
        printf("  %6s%c",string_method,charCrypt);
kusano fc6ab3
        Display64BitsSize(file_info.compressed_size,7);
kusano fc6ab3
        printf(" %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
kusano fc6ab3
                ratio,
kusano fc6ab3
                (uLong)file_info.tmu_date.tm_mon + 1,
kusano fc6ab3
                (uLong)file_info.tmu_date.tm_mday,
kusano fc6ab3
                (uLong)file_info.tmu_date.tm_year % 100,
kusano fc6ab3
                (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
kusano fc6ab3
                (uLong)file_info.crc,filename_inzip);
kusano fc6ab3
        if ((i+1)
kusano fc6ab3
        {
kusano fc6ab3
            err = unzGoToNextFile(uf);
kusano fc6ab3
            if (err!=UNZ_OK)
kusano fc6ab3
            {
kusano fc6ab3
                printf("error %d with zipfile in unzGoToNextFile\n",err);
kusano fc6ab3
                break;
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
kusano fc6ab3
    unzFile uf;
kusano fc6ab3
    const int* popt_extract_without_path;
kusano fc6ab3
    int* popt_overwrite;
kusano fc6ab3
    const char* password;
kusano fc6ab3
{
kusano fc6ab3
    char filename_inzip[256];
kusano fc6ab3
    char* filename_withoutpath;
kusano fc6ab3
    char* p;
kusano fc6ab3
    int err=UNZ_OK;
kusano fc6ab3
    FILE *fout=NULL;
kusano fc6ab3
    void* buf;
kusano fc6ab3
    uInt size_buf;
kusano fc6ab3
kusano fc6ab3
    unz_file_info64 file_info;
kusano fc6ab3
    uLong ratio=0;
kusano fc6ab3
    err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
kusano fc6ab3
kusano fc6ab3
    if (err!=UNZ_OK)
kusano fc6ab3
    {
kusano fc6ab3
        printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
kusano fc6ab3
        return err;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    size_buf = WRITEBUFFERSIZE;
kusano fc6ab3
    buf = (void*)malloc(size_buf);
kusano fc6ab3
    if (buf==NULL)
kusano fc6ab3
    {
kusano fc6ab3
        printf("Error allocating memory\n");
kusano fc6ab3
        return UNZ_INTERNALERROR;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    p = filename_withoutpath = filename_inzip;
kusano fc6ab3
    while ((*p) != '\0')
kusano fc6ab3
    {
kusano fc6ab3
        if (((*p)=='/') || ((*p)=='\\'))
kusano fc6ab3
            filename_withoutpath = p+1;
kusano fc6ab3
        p++;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    if ((*filename_withoutpath)=='\0')
kusano fc6ab3
    {
kusano fc6ab3
        if ((*popt_extract_without_path)==0)
kusano fc6ab3
        {
kusano fc6ab3
            printf("creating directory: %s\n",filename_inzip);
kusano fc6ab3
            mymkdir(filename_inzip);
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        const char* write_filename;
kusano fc6ab3
        int skip=0;
kusano fc6ab3
kusano fc6ab3
        if ((*popt_extract_without_path)==0)
kusano fc6ab3
            write_filename = filename_inzip;
kusano fc6ab3
        else
kusano fc6ab3
            write_filename = filename_withoutpath;
kusano fc6ab3
kusano fc6ab3
        err = unzOpenCurrentFilePassword(uf,password);
kusano fc6ab3
        if (err!=UNZ_OK)
kusano fc6ab3
        {
kusano fc6ab3
            printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        if (((*popt_overwrite)==0) && (err==UNZ_OK))
kusano fc6ab3
        {
kusano fc6ab3
            char rep=0;
kusano fc6ab3
            FILE* ftestexist;
kusano fc6ab3
            ftestexist = FOPEN_FUNC(write_filename,"rb");
kusano fc6ab3
            if (ftestexist!=NULL)
kusano fc6ab3
            {
kusano fc6ab3
                fclose(ftestexist);
kusano fc6ab3
                do
kusano fc6ab3
                {
kusano fc6ab3
                    char answer[128];
kusano fc6ab3
                    int ret;
kusano fc6ab3
kusano fc6ab3
                    printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
kusano fc6ab3
                    ret = scanf("%1s",answer);
kusano fc6ab3
                    if (ret != 1)
kusano fc6ab3
                    {
kusano fc6ab3
                       exit(EXIT_FAILURE);
kusano fc6ab3
                    }
kusano fc6ab3
                    rep = answer[0] ;
kusano fc6ab3
                    if ((rep>='a') && (rep<='z'))
kusano fc6ab3
                        rep -= 0x20;
kusano fc6ab3
                }
kusano fc6ab3
                while ((rep!='Y') && (rep!='N') && (rep!='A'));
kusano fc6ab3
            }
kusano fc6ab3
kusano fc6ab3
            if (rep == 'N')
kusano fc6ab3
                skip = 1;
kusano fc6ab3
kusano fc6ab3
            if (rep == 'A')
kusano fc6ab3
                *popt_overwrite=1;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        if ((skip==0) && (err==UNZ_OK))
kusano fc6ab3
        {
kusano fc6ab3
            fout=FOPEN_FUNC(write_filename,"wb");
kusano fc6ab3
            /* some zipfile don't contain directory alone before file */
kusano fc6ab3
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
kusano fc6ab3
                                (filename_withoutpath!=(char*)filename_inzip))
kusano fc6ab3
            {
kusano fc6ab3
                char c=*(filename_withoutpath-1);
kusano fc6ab3
                *(filename_withoutpath-1)='\0';
kusano fc6ab3
                makedir(write_filename);
kusano fc6ab3
                *(filename_withoutpath-1)=c;
kusano fc6ab3
                fout=FOPEN_FUNC(write_filename,"wb");
kusano fc6ab3
            }
kusano fc6ab3
kusano fc6ab3
            if (fout==NULL)
kusano fc6ab3
            {
kusano fc6ab3
                printf("error opening %s\n",write_filename);
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        if (fout!=NULL)
kusano fc6ab3
        {
kusano fc6ab3
            printf(" extracting: %s\n",write_filename);
kusano fc6ab3
kusano fc6ab3
            do
kusano fc6ab3
            {
kusano fc6ab3
                err = unzReadCurrentFile(uf,buf,size_buf);
kusano fc6ab3
                if (err<0)
kusano fc6ab3
                {
kusano fc6ab3
                    printf("error %d with zipfile in unzReadCurrentFile\n",err);
kusano fc6ab3
                    break;
kusano fc6ab3
                }
kusano fc6ab3
                if (err>0)
kusano fc6ab3
                    if (fwrite(buf,err,1,fout)!=1)
kusano fc6ab3
                    {
kusano fc6ab3
                        printf("error in writing extracted file\n");
kusano fc6ab3
                        err=UNZ_ERRNO;
kusano fc6ab3
                        break;
kusano fc6ab3
                    }
kusano fc6ab3
            }
kusano fc6ab3
            while (err>0);
kusano fc6ab3
            if (fout)
kusano fc6ab3
                    fclose(fout);
kusano fc6ab3
kusano fc6ab3
            if (err==0)
kusano fc6ab3
                change_file_date(write_filename,file_info.dosDate,
kusano fc6ab3
                                 file_info.tmu_date);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        if (err==UNZ_OK)
kusano fc6ab3
        {
kusano fc6ab3
            err = unzCloseCurrentFile (uf);
kusano fc6ab3
            if (err!=UNZ_OK)
kusano fc6ab3
            {
kusano fc6ab3
                printf("error %d with zipfile in unzCloseCurrentFile\n",err);
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
        else
kusano fc6ab3
            unzCloseCurrentFile(uf); /* don't lose the error */
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    free(buf);
kusano fc6ab3
    return err;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
kusano fc6ab3
    unzFile uf;
kusano fc6ab3
    int opt_extract_without_path;
kusano fc6ab3
    int opt_overwrite;
kusano fc6ab3
    const char* password;
kusano fc6ab3
{
kusano fc6ab3
    uLong i;
kusano fc6ab3
    unz_global_info64 gi;
kusano fc6ab3
    int err;
kusano fc6ab3
    FILE* fout=NULL;
kusano fc6ab3
kusano fc6ab3
    err = unzGetGlobalInfo64(uf,&gi);
kusano fc6ab3
    if (err!=UNZ_OK)
kusano fc6ab3
        printf("error %d with zipfile in unzGetGlobalInfo \n",err);
kusano fc6ab3
kusano fc6ab3
    for (i=0;i
kusano fc6ab3
    {
kusano fc6ab3
        if (do_extract_currentfile(uf,&opt_extract_without_path,
kusano fc6ab3
                                      &opt_overwrite,
kusano fc6ab3
                                      password) != UNZ_OK)
kusano fc6ab3
            break;
kusano fc6ab3
kusano fc6ab3
        if ((i+1)
kusano fc6ab3
        {
kusano fc6ab3
            err = unzGoToNextFile(uf);
kusano fc6ab3
            if (err!=UNZ_OK)
kusano fc6ab3
            {
kusano fc6ab3
                printf("error %d with zipfile in unzGoToNextFile\n",err);
kusano fc6ab3
                break;
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
kusano fc6ab3
    unzFile uf;
kusano fc6ab3
    const char* filename;
kusano fc6ab3
    int opt_extract_without_path;
kusano fc6ab3
    int opt_overwrite;
kusano fc6ab3
    const char* password;
kusano fc6ab3
{
kusano fc6ab3
    int err = UNZ_OK;
kusano fc6ab3
    if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
kusano fc6ab3
    {
kusano fc6ab3
        printf("file %s not found in the zipfile\n",filename);
kusano fc6ab3
        return 2;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    if (do_extract_currentfile(uf,&opt_extract_without_path,
kusano fc6ab3
                                      &opt_overwrite,
kusano fc6ab3
                                      password) == UNZ_OK)
kusano fc6ab3
        return 0;
kusano fc6ab3
    else
kusano fc6ab3
        return 1;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
int main(argc,argv)
kusano fc6ab3
    int argc;
kusano fc6ab3
    char *argv[];
kusano fc6ab3
{
kusano fc6ab3
    const char *zipfilename=NULL;
kusano fc6ab3
    const char *filename_to_extract=NULL;
kusano fc6ab3
    const char *password=NULL;
kusano fc6ab3
    char filename_try[MAXFILENAME+16] = "";
kusano fc6ab3
    int i;
kusano fc6ab3
    int ret_value=0;
kusano fc6ab3
    int opt_do_list=0;
kusano fc6ab3
    int opt_do_extract=1;
kusano fc6ab3
    int opt_do_extract_withoutpath=0;
kusano fc6ab3
    int opt_overwrite=0;
kusano fc6ab3
    int opt_extractdir=0;
kusano fc6ab3
    const char *dirname=NULL;
kusano fc6ab3
    unzFile uf=NULL;
kusano fc6ab3
kusano fc6ab3
    do_banner();
kusano fc6ab3
    if (argc==1)
kusano fc6ab3
    {
kusano fc6ab3
        do_help();
kusano fc6ab3
        return 0;
kusano fc6ab3
    }
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        for (i=1;i
kusano fc6ab3
        {
kusano fc6ab3
            if ((*argv[i])=='-')
kusano fc6ab3
            {
kusano fc6ab3
                const char *p=argv[i]+1;
kusano fc6ab3
kusano fc6ab3
                while ((*p)!='\0')
kusano fc6ab3
                {
kusano fc6ab3
                    char c=*(p++);;
kusano fc6ab3
                    if ((c=='l') || (c=='L'))
kusano fc6ab3
                        opt_do_list = 1;
kusano fc6ab3
                    if ((c=='v') || (c=='V'))
kusano fc6ab3
                        opt_do_list = 1;
kusano fc6ab3
                    if ((c=='x') || (c=='X'))
kusano fc6ab3
                        opt_do_extract = 1;
kusano fc6ab3
                    if ((c=='e') || (c=='E'))
kusano fc6ab3
                        opt_do_extract = opt_do_extract_withoutpath = 1;
kusano fc6ab3
                    if ((c=='o') || (c=='O'))
kusano fc6ab3
                        opt_overwrite=1;
kusano fc6ab3
                    if ((c=='d') || (c=='D'))
kusano fc6ab3
                    {
kusano fc6ab3
                        opt_extractdir=1;
kusano fc6ab3
                        dirname=argv[i+1];
kusano fc6ab3
                    }
kusano fc6ab3
kusano fc6ab3
                    if (((c=='p') || (c=='P')) && (i+1
kusano fc6ab3
                    {
kusano fc6ab3
                        password=argv[i+1];
kusano fc6ab3
                        i++;
kusano fc6ab3
                    }
kusano fc6ab3
                }
kusano fc6ab3
            }
kusano fc6ab3
            else
kusano fc6ab3
            {
kusano fc6ab3
                if (zipfilename == NULL)
kusano fc6ab3
                    zipfilename = argv[i];
kusano fc6ab3
                else if ((filename_to_extract==NULL) && (!opt_extractdir))
kusano fc6ab3
                        filename_to_extract = argv[i] ;
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    if (zipfilename!=NULL)
kusano fc6ab3
    {
kusano fc6ab3
kusano fc6ab3
#        ifdef USEWIN32IOAPI
kusano fc6ab3
        zlib_filefunc64_def ffunc;
kusano fc6ab3
#        endif
kusano fc6ab3
kusano fc6ab3
        strncpy(filename_try, zipfilename,MAXFILENAME-1);
kusano fc6ab3
        /* strncpy doesnt append the trailing NULL, of the string is too long. */
kusano fc6ab3
        filename_try[ MAXFILENAME ] = '\0';
kusano fc6ab3
kusano fc6ab3
#        ifdef USEWIN32IOAPI
kusano fc6ab3
        fill_win32_filefunc64A(&ffunc);
kusano fc6ab3
        uf = unzOpen2_64(zipfilename,&ffunc);
kusano fc6ab3
#        else
kusano fc6ab3
        uf = unzOpen64(zipfilename);
kusano fc6ab3
#        endif
kusano fc6ab3
        if (uf==NULL)
kusano fc6ab3
        {
kusano fc6ab3
            strcat(filename_try,".zip");
kusano fc6ab3
#            ifdef USEWIN32IOAPI
kusano fc6ab3
            uf = unzOpen2_64(filename_try,&ffunc);
kusano fc6ab3
#            else
kusano fc6ab3
            uf = unzOpen64(filename_try);
kusano fc6ab3
#            endif
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    if (uf==NULL)
kusano fc6ab3
    {
kusano fc6ab3
        printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
kusano fc6ab3
        return 1;
kusano fc6ab3
    }
kusano fc6ab3
    printf("%s opened\n",filename_try);
kusano fc6ab3
kusano fc6ab3
    if (opt_do_list==1)
kusano fc6ab3
        ret_value = do_list(uf);
kusano fc6ab3
    else if (opt_do_extract==1)
kusano fc6ab3
    {
kusano fc6ab3
#ifdef _WIN32
kusano fc6ab3
        if (opt_extractdir && _chdir(dirname))
kusano fc6ab3
#else
kusano fc6ab3
        if (opt_extractdir && chdir(dirname))
kusano fc6ab3
#endif
kusano fc6ab3
        {
kusano fc6ab3
          printf("Error changing into %s, aborting\n", dirname);
kusano fc6ab3
          exit(-1);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        if (filename_to_extract == NULL)
kusano fc6ab3
            ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
kusano fc6ab3
        else
kusano fc6ab3
            ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    unzClose(uf);
kusano fc6ab3
kusano fc6ab3
    return ret_value;
kusano fc6ab3
}