19e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project/*
29e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   minizip.c
3381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes   Version 1.1, February 14h, 2010
4381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes   sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
59e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
6381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
7381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
8381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         Modifications of Unzip for Zip64
9381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         Copyright (C) 2007-2008 Even Rouault
10381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
11381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         Modifications for Zip64 support on both zip and unzip
12381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes         Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
139e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project*/
149e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
15381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
16ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
17381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #ifndef __USE_FILE_OFFSET64
18381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                #define __USE_FILE_OFFSET64
19381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #endif
20381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #ifndef __USE_LARGEFILE64
21381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                #define __USE_LARGEFILE64
22381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #endif
23381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #ifndef _LARGEFILE64_SOURCE
24381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                #define _LARGEFILE64_SOURCE
25381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #endif
26381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #ifndef _FILE_OFFSET_BIT
27381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                #define _FILE_OFFSET_BIT 64
28381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #endif
29381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#endif
30381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
31ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#ifdef __APPLE__
32ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
33ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
34ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#define FTELLO_FUNC(stream) ftello(stream)
35ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
36ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#else
37ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
38ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#define FTELLO_FUNC(stream) ftello64(stream)
39ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
40ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#endif
41ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
42ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
43ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
449e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include <stdio.h>
459e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include <stdlib.h>
469e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include <string.h>
479e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include <time.h>
489e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include <errno.h>
499e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include <fcntl.h>
509e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
51ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#ifdef _WIN32
52ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes# include <direct.h>
53ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes# include <io.h>
54ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#else
559e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project# include <unistd.h>
569e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project# include <utime.h>
579e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project# include <sys/types.h>
589e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project# include <sys/stat.h>
599e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#endif
609e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
619e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#include "zip.h"
629e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
63381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifdef _WIN32
64381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #define USEWIN32IOAPI
65381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        #include "iowin32.h"
669e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#endif
679e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
689e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
699e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
709e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#define WRITEBUFFERSIZE (16384)
719e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#define MAXFILENAME (256)
729e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
73381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes#ifdef _WIN32
749e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source ProjectuLong filetime(f, tmzip, dt)
759e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    char *f;                /* name of file to get info on */
769e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    tm_zip *tmzip;             /* return value: access, modific. and creation times */
779e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    uLong *dt;             /* dostime */
789e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
799e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  int ret = 0;
809e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  {
819e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      FILETIME ftLocal;
829e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      HANDLE hFind;
83381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes      WIN32_FIND_DATAA ff32;
849e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
85381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes      hFind = FindFirstFileA(f,&ff32);
869e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      if (hFind != INVALID_HANDLE_VALUE)
879e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      {
889e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
899e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
909e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        FindClose(hFind);
919e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        ret = 1;
929e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      }
939e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  }
949e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  return ret;
959e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
969e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#else
97ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes#ifdef unix || __APPLE__
989e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source ProjectuLong filetime(f, tmzip, dt)
999e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    char *f;               /* name of file to get info on */
1009e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    tm_zip *tmzip;         /* return value: access, modific. and creation times */
1019e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    uLong *dt;             /* dostime */
1029e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
1039e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  int ret=0;
1049e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  struct stat s;        /* results of stat() */
1059e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  struct tm* filedate;
1069e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  time_t tm_t=0;
1079e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1089e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  if (strcmp(f,"-")!=0)
1099e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  {
1109e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    char name[MAXFILENAME+1];
1119e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int len = strlen(f);
1129e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (len > MAXFILENAME)
1139e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      len = MAXFILENAME;
1149e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1159e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    strncpy(name, f,MAXFILENAME-1);
1169e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    /* strncpy doesnt append the trailing NULL, of the string is too long. */
1179e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    name[ MAXFILENAME ] = '\0';
1189e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1199e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (name[len - 1] == '/')
1209e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      name[len - 1] = '\0';
1219e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    /* not all systems allow stat'ing a file with / appended */
1229e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (stat(name,&s)==0)
1239e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
1249e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      tm_t = s.st_mtime;
1259e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project      ret = 1;
1269e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
1279e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  }
1289e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  filedate = localtime(&tm_t);
1299e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1309e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  tmzip->tm_sec  = filedate->tm_sec;
1319e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  tmzip->tm_min  = filedate->tm_min;
1329e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  tmzip->tm_hour = filedate->tm_hour;
1339e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  tmzip->tm_mday = filedate->tm_mday;
1349e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  tmzip->tm_mon  = filedate->tm_mon ;
1359e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  tmzip->tm_year = filedate->tm_year;
1369e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1379e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project  return ret;
1389e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
1399e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#else
1409e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source ProjectuLong filetime(f, tmzip, dt)
1419e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    char *f;                /* name of file to get info on */
1429e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    tm_zip *tmzip;             /* return value: access, modific. and creation times */
1439e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    uLong *dt;             /* dostime */
1449e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
1459e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    return 0;
1469e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
1479e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#endif
1489e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#endif
1499e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1509e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1519e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1529e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1539e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Projectint check_exist_file(filename)
1549e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    const char* filename;
1559e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
1569e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    FILE* ftestexist;
1579e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int ret = 1;
158ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    ftestexist = FOPEN_FUNC(filename,"rb");
1599e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (ftestexist==NULL)
1609e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        ret = 0;
1619e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    else
1629e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        fclose(ftestexist);
1639e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    return ret;
1649e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
1659e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1669e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Projectvoid do_banner()
1679e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
168381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
169381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
1709e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
1719e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1729e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Projectvoid do_help()
1739e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
174381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
1759e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project           "  -o  Overwrite existing file.zip\n" \
1769e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project           "  -a  Append to existing file.zip\n" \
1779e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project           "  -0  Store only\n" \
1789e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project           "  -1  Compress faster\n" \
179381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes           "  -9  Compress better\n\n" \
180381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes           "  -j  exclude path. store only the file name.\n\n");
1819e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
1829e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1839e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project/* calculate the CRC32 of a file,
1849e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   because to encrypt a file, we need known the CRC32 of the file before */
1859e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Projectint getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
1869e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
1879e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   unsigned long calculate_crc=0;
1889e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   int err=ZIP_OK;
189ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes   FILE * fin = FOPEN_FUNC(filenameinzip,"rb");
190ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes
1919e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   unsigned long size_read = 0;
1929e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   unsigned long total_read = 0;
1939e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   if (fin==NULL)
1949e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   {
1959e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project       err = ZIP_ERRNO;
1969e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project   }
1979e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
1989e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (err == ZIP_OK)
1999e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        do
2009e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        {
2019e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            err = ZIP_OK;
2029e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            size_read = (int)fread(buf,1,size_buf,fin);
2039e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if (size_read < size_buf)
2049e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (feof(fin)==0)
2059e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            {
2069e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                printf("error in reading %s\n",filenameinzip);
2079e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                err = ZIP_ERRNO;
2089e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            }
2099e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2109e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if (size_read>0)
2119e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                calculate_crc = crc32(calculate_crc,buf,size_read);
2129e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            total_read += size_read;
2139e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2149e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        } while ((err == ZIP_OK) && (size_read>0));
2159e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2169e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (fin)
2179e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        fclose(fin);
2189e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2199e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    *result_crc=calculate_crc;
220381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    printf("file %s crc %lx\n", filenameinzip, calculate_crc);
2219e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    return err;
2229e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
2239e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
224381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughesint isLargeFile(const char* filename)
225381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes{
226381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  int largeFile = 0;
227381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  ZPOS64_T pos = 0;
228ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes  FILE* pFile = FOPEN_FUNC(filename, "rb");
229381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
230381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  if(pFile != NULL)
231381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  {
232ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    int n = FSEEKO_FUNC(pFile, 0, SEEK_END);
233ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes    pos = FTELLO_FUNC(pFile);
234381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
235381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                printf("File : %s is %lld bytes\n", filename, pos);
236381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
237381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    if(pos >= 0xffffffff)
238381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes     largeFile = 1;
239381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
240381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                fclose(pFile);
241381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes  }
242381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
243381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes return largeFile;
244381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes}
245381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
2469e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Projectint main(argc,argv)
2479e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int argc;
2489e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    char *argv[];
2499e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project{
2509e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int i;
2519e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int opt_overwrite=0;
2529e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int opt_compress_level=Z_DEFAULT_COMPRESSION;
253381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    int opt_exclude_path=0;
2549e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int zipfilenamearg = 0;
2559e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    char filename_try[MAXFILENAME+16];
2569e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int zipok;
2579e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int err=0;
2589e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    int size_buf=0;
2599e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    void* buf=NULL;
2609e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    const char* password=NULL;
2619e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2629e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2639e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    do_banner();
2649e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (argc==1)
2659e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
2669e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        do_help();
2679e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        return 0;
2689e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
2699e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    else
2709e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
2719e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        for (i=1;i<argc;i++)
2729e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        {
2739e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if ((*argv[i])=='-')
2749e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            {
2759e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                const char *p=argv[i]+1;
2769e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2779e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                while ((*p)!='\0')
2789e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                {
2799e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    char c=*(p++);;
2809e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if ((c=='o') || (c=='O'))
2819e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        opt_overwrite = 1;
2829e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if ((c=='a') || (c=='A'))
2839e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        opt_overwrite = 2;
2849e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if ((c>='0') && (c<='9'))
2859e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        opt_compress_level = c-'0';
286381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                    if ((c=='j') || (c=='J'))
287381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                        opt_exclude_path = 1;
2889e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
2899e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if (((c=='p') || (c=='P')) && (i+1<argc))
2909e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    {
2919e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        password=argv[i+1];
2929e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        i++;
2939e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    }
2949e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                }
2959e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            }
2969e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            else
297381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            {
2989e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (zipfilenamearg == 0)
299381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                {
3009e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    zipfilenamearg = i ;
301381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                }
302381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes            }
3039e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        }
3049e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
3059e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3069e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    size_buf = WRITEBUFFERSIZE;
3079e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    buf = (void*)malloc(size_buf);
3089e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (buf==NULL)
3099e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
3109e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        printf("Error allocating memory\n");
3119e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        return ZIP_INTERNALERROR;
3129e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
3139e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3149e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (zipfilenamearg==0)
315381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    {
3169e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        zipok=0;
317381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes    }
3189e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    else
3199e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
3209e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        int i,len;
3219e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        int dot_found=0;
3229e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3239e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        zipok = 1 ;
3249e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
3259e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        /* strncpy doesnt append the trailing NULL, of the string is too long. */
3269e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        filename_try[ MAXFILENAME ] = '\0';
3279e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3289e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        len=(int)strlen(filename_try);
3299e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        for (i=0;i<len;i++)
3309e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if (filename_try[i]=='.')
3319e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                dot_found=1;
3329e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3339e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        if (dot_found==0)
3349e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            strcat(filename_try,".zip");
3359e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3369e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        if (opt_overwrite==2)
3379e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        {
3389e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            /* if the file don't exist, we not append file */
3399e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if (check_exist_file(filename_try)==0)
3409e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                opt_overwrite=1;
3419e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        }
3429e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        else
3439e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        if (opt_overwrite==0)
3449e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if (check_exist_file(filename_try)!=0)
3459e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            {
3469e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                char rep=0;
3479e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                do
3489e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                {
3499e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    char answer[128];
3509e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    int ret;
3519e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
3529e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    ret = scanf("%1s",answer);
3539e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if (ret != 1)
3549e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    {
3559e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                       exit(EXIT_FAILURE);
3569e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    }
3579e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    rep = answer[0] ;
3589e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if ((rep>='a') && (rep<='z'))
3599e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        rep -= 0x20;
3609e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                }
3619e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                while ((rep!='Y') && (rep!='N') && (rep!='A'));
3629e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (rep=='N')
3639e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    zipok = 0;
3649e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (rep=='A')
3659e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    opt_overwrite = 2;
3669e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            }
3679e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
3689e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3699e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    if (zipok==1)
3709e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
3719e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        zipFile zf;
3729e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        int errclose;
3739e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#        ifdef USEWIN32IOAPI
374381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        zlib_filefunc64_def ffunc;
375381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        fill_win32_filefunc64A(&ffunc);
376381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
3779e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#        else
378381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes        zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
3799e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project#        endif
3809e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3819e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        if (zf == NULL)
3829e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        {
3839e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            printf("error opening %s\n",filename_try);
3849e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            err= ZIP_ERRNO;
3859e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        }
3869e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        else
3879e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            printf("creating %s\n",filename_try);
3889e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
3899e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
3909e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        {
3919e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
3929e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                  ((argv[i][1]=='o') || (argv[i][1]=='O') ||
3939e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                   (argv[i][1]=='a') || (argv[i][1]=='A') ||
3949e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                   (argv[i][1]=='p') || (argv[i][1]=='P') ||
3959e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                   ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
3969e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                  (strlen(argv[i]) == 2)))
3979e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            {
3989e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                FILE * fin;
3999e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                int size_read;
4009e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                const char* filenameinzip = argv[i];
401381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                const char *savefilenameinzip;
4029e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                zip_fileinfo zi;
4039e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                unsigned long crcFile=0;
404381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                int zip64 = 0;
4059e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4069e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
4079e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
4089e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                zi.dosDate = 0;
4099e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                zi.internal_fa = 0;
4109e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                zi.external_fa = 0;
4119e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
4129e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4139e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project/*
4149e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
4159e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 NULL,0,NULL,0,NULL / * comment * /,
4169e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 (opt_compress_level != 0) ? Z_DEFLATED : 0,
4179e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 opt_compress_level);
4189e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project*/
4199e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if ((password != NULL) && (err==ZIP_OK))
4209e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
4219e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
422381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                zip64 = isLargeFile(filenameinzip);
423381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
424381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                                                         /* The path name saved, should not include a leading slash. */
425381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes               /*if it did, windows/xp and dynazip couldn't read the zip file. */
426381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 savefilenameinzip = filenameinzip;
427381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
428381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 {
429381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     savefilenameinzip++;
430381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 }
431381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
432381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 /*should the zip file contain any path at all?*/
433381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 if( opt_exclude_path )
434381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 {
435381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     const char *tmpptr;
436381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     const char *lastslash = 0;
437381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
438381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     {
439381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                         if( *tmpptr == '\\' || *tmpptr == '/')
440381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                         {
441381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                             lastslash = tmpptr;
442381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                         }
443381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     }
444381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     if( lastslash != NULL )
445381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     {
446381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                         savefilenameinzip = lastslash+1; // base filename follows last slash.
447381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                     }
448381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 }
449381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes
450381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                 /**/
451381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
4529e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 NULL,0,NULL,0,NULL /* comment*/,
4539e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 (opt_compress_level != 0) ? Z_DEFLATED : 0,
4549e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 opt_compress_level,0,
4559e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
4569e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
457381716e9396b55b1adb8235b020c37344f60ab07Elliott Hughes                                 password,crcFile, zip64);
4589e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4599e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (err != ZIP_OK)
4609e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    printf("error in opening %s in zipfile\n",filenameinzip);
4619e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                else
4629e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                {
463ee9e11d0d4e3361533860bf04896abb86a291bfbElliott Hughes                    fin = FOPEN_FUNC(filenameinzip,"rb");
4649e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if (fin==NULL)
4659e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    {
4669e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        err=ZIP_ERRNO;
4679e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        printf("error in opening %s for reading\n",filenameinzip);
4689e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    }
4699e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                }
4709e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4719e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (err == ZIP_OK)
4729e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    do
4739e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    {
4749e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        err = ZIP_OK;
4759e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        size_read = (int)fread(buf,1,size_buf,fin);
4769e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        if (size_read < size_buf)
4779e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            if (feof(fin)==0)
4789e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        {
4799e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            printf("error in reading %s\n",filenameinzip);
4809e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            err = ZIP_ERRNO;
4819e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        }
4829e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4839e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        if (size_read>0)
4849e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        {
4859e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            err = zipWriteInFileInZip (zf,buf,size_read);
4869e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            if (err<0)
4879e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            {
4889e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                printf("error in writing %s in the zipfile\n",
4899e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                                 filenameinzip);
4909e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                            }
4919e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4929e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        }
4939e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    } while ((err == ZIP_OK) && (size_read>0));
4949e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4959e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (fin)
4969e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    fclose(fin);
4979e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
4989e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                if (err<0)
4999e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    err=ZIP_ERRNO;
5009e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                else
5019e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                {
5029e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    err = zipCloseFileInZip(zf);
5039e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                    if (err!=ZIP_OK)
5049e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                        printf("error in closing %s in the zipfile\n",
5059e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                                    filenameinzip);
5069e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project                }
5079e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            }
5089e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        }
5099e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        errclose = zipClose(zf,NULL);
5109e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project        if (errclose != ZIP_OK)
5119e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project            printf("error in closing %s\n",filename_try);
5129e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
5139e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    else
5149e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    {
5159e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project       do_help();
5169e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    }
5179e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project
5189e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    free(buf);
5199e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project    return 0;
5209e38dfa2f95fce609707a0941f10af9a785288deThe Android Open Source Project}
521