2011-09-10 02:20:15 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifdef unix
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <utime.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#else
|
|
|
|
# include <direct.h>
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "zip.h"
|
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
#define USEWIN32IOAPI
|
|
|
|
#include "iowin32.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-09-10 02:20:15 -04:00
|
|
|
|
|
|
|
#define WRITEBUFFERSIZE (16384)
|
|
|
|
#define MAXFILENAME (256)
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
uLong filetime(f, tmzip, dt)
|
|
|
|
char *f; /* name of file to get info on */
|
|
|
|
tm_zip *tmzip; /* return value: access, modific. and creation times */
|
|
|
|
uLong *dt; /* dostime */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
{
|
|
|
|
FILETIME ftLocal;
|
|
|
|
HANDLE hFind;
|
|
|
|
WIN32_FIND_DATA ff32;
|
|
|
|
|
|
|
|
hFind = FindFirstFile(f,&ff32);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
|
|
|
|
FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
|
|
|
|
FindClose(hFind);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef unix
|
|
|
|
uLong filetime(f, tmzip, dt)
|
|
|
|
char *f; /* name of file to get info on */
|
|
|
|
tm_zip *tmzip; /* return value: access, modific. and creation times */
|
|
|
|
uLong *dt; /* dostime */
|
|
|
|
{
|
|
|
|
int ret=0;
|
|
|
|
struct stat s; /* results of stat() */
|
|
|
|
struct tm* filedate;
|
|
|
|
time_t tm_t=0;
|
2011-09-10 02:21:57 -04:00
|
|
|
|
2011-09-10 02:20:15 -04:00
|
|
|
if (strcmp(f,"-")!=0)
|
|
|
|
{
|
2011-09-10 02:21:57 -04:00
|
|
|
char name[MAXFILENAME+1];
|
2011-09-10 02:20:15 -04:00
|
|
|
int len = strlen(f);
|
2011-09-10 02:21:57 -04:00
|
|
|
|
|
|
|
strncpy(name, f,MAXFILENAME-1);
|
|
|
|
/* strncpy doesnt append the trailing NULL, of the string is too long. */
|
|
|
|
name[ MAXFILENAME ] = '\0';
|
|
|
|
|
2011-09-10 02:20:15 -04:00
|
|
|
if (name[len - 1] == '/')
|
|
|
|
name[len - 1] = '\0';
|
|
|
|
/* not all systems allow stat'ing a file with / appended */
|
|
|
|
if (stat(name,&s)==0)
|
|
|
|
{
|
|
|
|
tm_t = s.st_mtime;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filedate = localtime(&tm_t);
|
|
|
|
|
|
|
|
tmzip->tm_sec = filedate->tm_sec;
|
|
|
|
tmzip->tm_min = filedate->tm_min;
|
|
|
|
tmzip->tm_hour = filedate->tm_hour;
|
|
|
|
tmzip->tm_mday = filedate->tm_mday;
|
|
|
|
tmzip->tm_mon = filedate->tm_mon ;
|
|
|
|
tmzip->tm_year = filedate->tm_year;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
uLong filetime(f, tmzip, dt)
|
|
|
|
char *f; /* name of file to get info on */
|
|
|
|
tm_zip *tmzip; /* return value: access, modific. and creation times */
|
|
|
|
uLong *dt; /* dostime */
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int check_exist_file(filename)
|
|
|
|
const char* filename;
|
|
|
|
{
|
2011-09-10 02:21:57 -04:00
|
|
|
FILE* ftestexist;
|
2011-09-10 02:20:15 -04:00
|
|
|
int ret = 1;
|
2011-09-10 02:21:57 -04:00
|
|
|
ftestexist = fopen(filename,"rb");
|
|
|
|
if (ftestexist==NULL)
|
2011-09-10 02:20:15 -04:00
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
fclose(ftestexist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_banner()
|
|
|
|
{
|
2011-09-10 02:21:57 -04:00
|
|
|
printf("MiniZip 0.15, demo of zLib + Zip package written by Gilles Vollant\n");
|
|
|
|
printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
|
2011-09-10 02:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_help()
|
2011-09-10 02:21:57 -04:00
|
|
|
{
|
|
|
|
printf("Usage : minizip [-o] file.zip [files_to_add]\n\n") ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the CRC32 of a file,
|
|
|
|
because to encrypt a file, we need known the CRC32 of the file before */
|
|
|
|
int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
|
|
|
|
{
|
|
|
|
unsigned long calculate_crc=0;
|
|
|
|
int err=ZIP_OK;
|
|
|
|
FILE * fin = fopen(filenameinzip,"rb");
|
|
|
|
unsigned long size_read = 0;
|
|
|
|
unsigned long total_read = 0;
|
|
|
|
if (fin==NULL)
|
|
|
|
{
|
|
|
|
err = ZIP_ERRNO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == ZIP_OK)
|
|
|
|
do
|
|
|
|
{
|
|
|
|
err = ZIP_OK;
|
|
|
|
size_read = (int)fread(buf,1,size_buf,fin);
|
|
|
|
if (size_read < size_buf)
|
|
|
|
if (feof(fin)==0)
|
|
|
|
{
|
|
|
|
printf("error in reading %s\n",filenameinzip);
|
|
|
|
err = ZIP_ERRNO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_read>0)
|
|
|
|
calculate_crc = crc32(calculate_crc,buf,size_read);
|
|
|
|
total_read += size_read;
|
|
|
|
|
|
|
|
} while ((err == ZIP_OK) && (size_read>0));
|
|
|
|
|
|
|
|
if (fin)
|
|
|
|
fclose(fin);
|
|
|
|
|
|
|
|
*result_crc=calculate_crc;
|
|
|
|
printf("file %s crc %x\n",filenameinzip,calculate_crc);
|
|
|
|
return err;
|
2011-09-10 02:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(argc,argv)
|
2011-09-10 02:21:57 -04:00
|
|
|
int argc;
|
|
|
|
char *argv[];
|
2011-09-10 02:20:15 -04:00
|
|
|
{
|
2011-09-10 02:21:57 -04:00
|
|
|
int i;
|
|
|
|
int opt_overwrite=0;
|
2011-09-10 02:20:15 -04:00
|
|
|
int opt_compress_level=Z_DEFAULT_COMPRESSION;
|
|
|
|
int zipfilenamearg = 0;
|
2011-09-10 02:21:57 -04:00
|
|
|
char filename_try[MAXFILENAME+16];
|
2011-09-10 02:20:15 -04:00
|
|
|
int zipok;
|
|
|
|
int err=0;
|
|
|
|
int size_buf=0;
|
2011-09-10 02:21:57 -04:00
|
|
|
void* buf=NULL;
|
|
|
|
const char* password=NULL;
|
2011-09-10 02:20:15 -04:00
|
|
|
|
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
do_banner();
|
|
|
|
if (argc==1)
|
|
|
|
{
|
|
|
|
do_help();
|
2011-09-10 02:20:15 -04:00
|
|
|
return 0;
|
2011-09-10 02:21:57 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i=1;i<argc;i++)
|
|
|
|
{
|
|
|
|
if ((*argv[i])=='-')
|
|
|
|
{
|
|
|
|
const char *p=argv[i]+1;
|
|
|
|
|
|
|
|
while ((*p)!='\0')
|
|
|
|
{
|
|
|
|
char c=*(p++);;
|
|
|
|
if ((c=='o') || (c=='O'))
|
|
|
|
opt_overwrite = 1;
|
|
|
|
if ((c=='a') || (c=='A'))
|
|
|
|
opt_overwrite = 2;
|
2011-09-10 02:20:15 -04:00
|
|
|
if ((c>='0') && (c<='9'))
|
|
|
|
opt_compress_level = c-'0';
|
2011-09-10 02:21:57 -04:00
|
|
|
|
|
|
|
if (((c=='p') || (c=='P')) && (i+1<argc))
|
|
|
|
{
|
|
|
|
password=argv[i+1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (zipfilenamearg == 0)
|
2011-09-10 02:20:15 -04:00
|
|
|
zipfilenamearg = i ;
|
2011-09-10 02:21:57 -04:00
|
|
|
}
|
|
|
|
}
|
2011-09-10 02:20:15 -04:00
|
|
|
|
|
|
|
size_buf = WRITEBUFFERSIZE;
|
|
|
|
buf = (void*)malloc(size_buf);
|
|
|
|
if (buf==NULL)
|
|
|
|
{
|
|
|
|
printf("Error allocating memory\n");
|
|
|
|
return ZIP_INTERNALERROR;
|
|
|
|
}
|
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
if (zipfilenamearg==0)
|
2011-09-10 02:20:15 -04:00
|
|
|
zipok=0;
|
|
|
|
else
|
2011-09-10 02:21:57 -04:00
|
|
|
{
|
2011-09-10 02:20:15 -04:00
|
|
|
int i,len;
|
|
|
|
int dot_found=0;
|
|
|
|
|
|
|
|
zipok = 1 ;
|
2011-09-10 02:21:57 -04:00
|
|
|
strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
|
|
|
|
/* strncpy doesnt append the trailing NULL, of the string is too long. */
|
|
|
|
filename_try[ MAXFILENAME ] = '\0';
|
|
|
|
|
|
|
|
len=(int)strlen(filename_try);
|
2011-09-10 02:20:15 -04:00
|
|
|
for (i=0;i<len;i++)
|
|
|
|
if (filename_try[i]=='.')
|
|
|
|
dot_found=1;
|
|
|
|
|
|
|
|
if (dot_found==0)
|
|
|
|
strcat(filename_try,".zip");
|
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
if (opt_overwrite==2)
|
|
|
|
{
|
|
|
|
/* if the file don't exist, we not append file */
|
|
|
|
if (check_exist_file(filename_try)==0)
|
|
|
|
opt_overwrite=1;
|
|
|
|
}
|
|
|
|
else
|
2011-09-10 02:20:15 -04:00
|
|
|
if (opt_overwrite==0)
|
|
|
|
if (check_exist_file(filename_try)!=0)
|
2011-09-10 02:21:57 -04:00
|
|
|
{
|
|
|
|
char rep=0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char answer[128];
|
|
|
|
printf("The file %s exist. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
|
|
|
|
scanf("%1s",answer);
|
|
|
|
rep = answer[0] ;
|
|
|
|
if ((rep>='a') && (rep<='z'))
|
|
|
|
rep -= 0x20;
|
|
|
|
}
|
|
|
|
while ((rep!='Y') && (rep!='N') && (rep!='A'));
|
2011-09-10 02:20:15 -04:00
|
|
|
if (rep=='N')
|
|
|
|
zipok = 0;
|
2011-09-10 02:21:57 -04:00
|
|
|
if (rep=='A')
|
|
|
|
opt_overwrite = 2;
|
|
|
|
}
|
2011-09-10 02:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (zipok==1)
|
|
|
|
{
|
|
|
|
zipFile zf;
|
|
|
|
int errclose;
|
2011-09-10 02:21:57 -04:00
|
|
|
#ifdef USEWIN32IOAPI
|
|
|
|
zlib_filefunc_def ffunc;
|
|
|
|
fill_win32_filefunc(&ffunc);
|
|
|
|
zf = zipOpen2(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
|
|
|
|
#else
|
|
|
|
zf = zipOpen(filename_try,(opt_overwrite==2) ? 2 : 0);
|
|
|
|
#endif
|
|
|
|
|
2011-09-10 02:20:15 -04:00
|
|
|
if (zf == NULL)
|
|
|
|
{
|
|
|
|
printf("error opening %s\n",filename_try);
|
|
|
|
err= ZIP_ERRNO;
|
|
|
|
}
|
2011-09-10 02:21:57 -04:00
|
|
|
else
|
2011-09-10 02:20:15 -04:00
|
|
|
printf("creating %s\n",filename_try);
|
|
|
|
|
|
|
|
for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
|
|
|
|
{
|
|
|
|
if (((*(argv[i]))!='-') && ((*(argv[i]))!='/'))
|
|
|
|
{
|
|
|
|
FILE * fin;
|
|
|
|
int size_read;
|
|
|
|
const char* filenameinzip = argv[i];
|
|
|
|
zip_fileinfo zi;
|
2011-09-10 02:21:57 -04:00
|
|
|
unsigned long crcFile=0;
|
2011-09-10 02:20:15 -04:00
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
|
|
|
|
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
|
2011-09-10 02:20:15 -04:00
|
|
|
zi.dosDate = 0;
|
|
|
|
zi.internal_fa = 0;
|
|
|
|
zi.external_fa = 0;
|
|
|
|
filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
|
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
/*
|
2011-09-10 02:20:15 -04:00
|
|
|
err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
|
2011-09-10 02:21:57 -04:00
|
|
|
NULL,0,NULL,0,NULL / * comment * /,
|
2011-09-10 02:20:15 -04:00
|
|
|
(opt_compress_level != 0) ? Z_DEFLATED : 0,
|
|
|
|
opt_compress_level);
|
2011-09-10 02:21:57 -04:00
|
|
|
*/
|
|
|
|
if ((password != NULL) && (err==ZIP_OK))
|
|
|
|
err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
|
|
|
|
|
|
|
|
err = zipOpenNewFileInZip3(zf,filenameinzip,&zi,
|
|
|
|
NULL,0,NULL,0,NULL /* comment*/,
|
|
|
|
(opt_compress_level != 0) ? Z_DEFLATED : 0,
|
|
|
|
opt_compress_level,0,
|
|
|
|
/* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
|
|
|
|
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
|
|
|
|
password,crcFile);
|
2011-09-10 02:20:15 -04:00
|
|
|
|
|
|
|
if (err != ZIP_OK)
|
|
|
|
printf("error in opening %s in zipfile\n",filenameinzip);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fin = fopen(filenameinzip,"rb");
|
|
|
|
if (fin==NULL)
|
|
|
|
{
|
|
|
|
err=ZIP_ERRNO;
|
|
|
|
printf("error in opening %s for reading\n",filenameinzip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == ZIP_OK)
|
|
|
|
do
|
|
|
|
{
|
|
|
|
err = ZIP_OK;
|
2011-09-10 02:21:57 -04:00
|
|
|
size_read = (int)fread(buf,1,size_buf,fin);
|
2011-09-10 02:20:15 -04:00
|
|
|
if (size_read < size_buf)
|
|
|
|
if (feof(fin)==0)
|
|
|
|
{
|
|
|
|
printf("error in reading %s\n",filenameinzip);
|
|
|
|
err = ZIP_ERRNO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_read>0)
|
|
|
|
{
|
|
|
|
err = zipWriteInFileInZip (zf,buf,size_read);
|
|
|
|
if (err<0)
|
|
|
|
{
|
|
|
|
printf("error in writing %s in the zipfile\n",
|
|
|
|
filenameinzip);
|
|
|
|
}
|
2011-09-10 02:21:57 -04:00
|
|
|
|
2011-09-10 02:20:15 -04:00
|
|
|
}
|
|
|
|
} while ((err == ZIP_OK) && (size_read>0));
|
|
|
|
|
2011-09-10 02:21:57 -04:00
|
|
|
if (fin)
|
|
|
|
fclose(fin);
|
|
|
|
|
2011-09-10 02:20:15 -04:00
|
|
|
if (err<0)
|
|
|
|
err=ZIP_ERRNO;
|
|
|
|
else
|
2011-09-10 02:21:57 -04:00
|
|
|
{
|
2011-09-10 02:20:15 -04:00
|
|
|
err = zipCloseFileInZip(zf);
|
|
|
|
if (err!=ZIP_OK)
|
|
|
|
printf("error in closing %s in the zipfile\n",
|
|
|
|
filenameinzip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errclose = zipClose(zf,NULL);
|
|
|
|
if (errclose != ZIP_OK)
|
|
|
|
printf("error in closing %s\n",filename_try);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
2011-09-10 02:21:57 -04:00
|
|
|
return 0;
|
2011-09-10 02:20:15 -04:00
|
|
|
}
|