| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <time.h> |
| 5 |
#include <errno.h> |
| 6 |
#include <fcntl.h> |
| 7 |
|
| 8 |
#ifndef WIN32 |
| 9 |
# include <unistd.h> |
| 10 |
# include <utime.h> |
| 11 |
# include <sys/types.h> |
| 12 |
# include <sys/stat.h> |
| 13 |
#else |
| 14 |
# include <direct.h> |
| 15 |
# include <io.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include "zip.h" |
| 19 |
|
| 20 |
#ifdef WIN32 |
| 21 |
#define USEWIN32IOAPI |
| 22 |
#include "iowin32.h" |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#define WRITEBUFFERSIZE (16384) |
| 28 |
#define MAXFILENAME (256) |
| 29 |
|
| 30 |
#ifdef WIN32 |
| 31 |
uLong filetime(f, tmzip, dt) |
| 32 |
char *f; /* name of file to get info on */ |
| 33 |
tm_zip *tmzip; /* return value: access, modific. and creation times */ |
| 34 |
uLong *dt; /* dostime */ |
| 35 |
{ |
| 36 |
int ret = 0; |
| 37 |
{ |
| 38 |
FILETIME ftLocal; |
| 39 |
HANDLE hFind; |
| 40 |
WIN32_FIND_DATA ff32; |
| 41 |
|
| 42 |
hFind = FindFirstFile(f,&ff32); |
| 43 |
if (hFind != INVALID_HANDLE_VALUE) |
| 44 |
{ |
| 45 |
FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal); |
| 46 |
FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0); |
| 47 |
FindClose(hFind); |
| 48 |
ret = 1; |
| 49 |
} |
| 50 |
} |
| 51 |
return ret; |
| 52 |
} |
| 53 |
#else |
| 54 |
#ifdef unix |
| 55 |
uLong filetime(f, tmzip, dt) |
| 56 |
char *f; /* name of file to get info on */ |
| 57 |
tm_zip *tmzip; /* return value: access, modific. and creation times */ |
| 58 |
uLong *dt; /* dostime */ |
| 59 |
{ |
| 60 |
int ret=0; |
| 61 |
struct stat s; /* results of stat() */ |
| 62 |
struct tm* filedate; |
| 63 |
time_t tm_t=0; |
| 64 |
|
| 65 |
if (strcmp(f,"-")!=0) |
| 66 |
{ |
| 67 |
char name[MAXFILENAME+1]; |
| 68 |
int len = strlen(f); |
| 69 |
|
| 70 |
strncpy(name, f,MAXFILENAME-1); |
| 71 |
/* strncpy doesnt append the trailing NULL, of the string is too long. */ |
| 72 |
name[ MAXFILENAME ] = '\0'; |
| 73 |
|
| 74 |
if (name[len - 1] == '/') |
| 75 |
name[len - 1] = '\0'; |
| 76 |
/* not all systems allow stat'ing a file with / appended */ |
| 77 |
if (stat(name,&s)==0) |
| 78 |
{ |
| 79 |
tm_t = s.st_mtime; |
| 80 |
ret = 1; |
| 81 |
} |
| 82 |
} |
| 83 |
filedate = localtime(&tm_t); |
| 84 |
|
| 85 |
tmzip->tm_sec = filedate->tm_sec; |
| 86 |
tmzip->tm_min = filedate->tm_min; |
| 87 |
tmzip->tm_hour = filedate->tm_hour; |
| 88 |
tmzip->tm_mday = filedate->tm_mday; |
| 89 |
tmzip->tm_mon = filedate->tm_mon ; |
| 90 |
tmzip->tm_year = filedate->tm_year; |
| 91 |
|
| 92 |
return ret; |
| 93 |
} |
| 94 |
#else |
| 95 |
uLong filetime(f, tmzip, dt) |
| 96 |
char *f; /* name of file to get info on */ |
| 97 |
tm_zip *tmzip; /* return value: access, modific. and creation times */ |
| 98 |
uLong *dt; /* dostime */ |
| 99 |
{ |
| 100 |
return 0; |
| 101 |
} |
| 102 |
#endif |
| 103 |
#endif |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
int check_exist_file(filename) |
| 109 |
const char* filename; |
| 110 |
{ |
| 111 |
FILE* ftestexist; |
| 112 |
int ret = 1; |
| 113 |
ftestexist = fopen(filename,"rb"); |
| 114 |
if (ftestexist==NULL) |
| 115 |
ret = 0; |
| 116 |
else |
| 117 |
fclose(ftestexist); |
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
void do_banner() |
| 122 |
{ |
| 123 |
printf("MiniZip 1.00, demo of zLib + Zip package written by Gilles Vollant\n"); |
| 124 |
printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); |
| 125 |
} |
| 126 |
|
| 127 |
void do_help() |
| 128 |
{ |
| 129 |
printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" \ |
| 130 |
" -o Overwrite existing file.zip\n" \ |
| 131 |
" -a Append to existing file.zip\n" \ |
| 132 |
" -0 Store only\n" \ |
| 133 |
" -1 Compress faster\n" \ |
| 134 |
" -9 Compress better\n\n"); |
| 135 |
} |
| 136 |
|
| 137 |
/* calculate the CRC32 of a file, |
| 138 |
because to encrypt a file, we need known the CRC32 of the file before */ |
| 139 |
int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc) |
| 140 |
{ |
| 141 |
unsigned long calculate_crc=0; |
| 142 |
int err=ZIP_OK; |
| 143 |
FILE * fin = fopen(filenameinzip,"rb"); |
| 144 |
unsigned long size_read = 0; |
| 145 |
unsigned long total_read = 0; |
| 146 |
if (fin==NULL) |
| 147 |
{ |
| 148 |
err = ZIP_ERRNO; |
| 149 |
} |
| 150 |
|
| 151 |
if (err == ZIP_OK) |
| 152 |
do |
| 153 |
{ |
| 154 |
err = ZIP_OK; |
| 155 |
size_read = (int)fread(buf,1,size_buf,fin); |
| 156 |
if (size_read < size_buf) |
| 157 |
if (feof(fin)==0) |
| 158 |
{ |
| 159 |
printf("error in reading %s\n",filenameinzip); |
| 160 |
err = ZIP_ERRNO; |
| 161 |
} |
| 162 |
|
| 163 |
if (size_read>0) |
| 164 |
calculate_crc = crc32(calculate_crc,buf,size_read); |
| 165 |
total_read += size_read; |
| 166 |
|
| 167 |
} while ((err == ZIP_OK) && (size_read>0)); |
| 168 |
|
| 169 |
if (fin) |
| 170 |
fclose(fin); |
| 171 |
|
| 172 |
*result_crc=calculate_crc; |
| 173 |
printf("file %s crc %x\n",filenameinzip,calculate_crc); |
| 174 |
return err; |
| 175 |
} |
| 176 |
|
| 177 |
//modified from minizip.c(one of contributions of zlib) |
| 178 |
int zip_main(argc,argv) |
| 179 |
int argc; |
| 180 |
char *argv[]; |
| 181 |
{ |
| 182 |
int i; |
| 183 |
int opt_overwrite=0; |
| 184 |
int opt_compress_level=Z_DEFAULT_COMPRESSION; |
| 185 |
int zipfilenamearg = 0; |
| 186 |
char filename_try[MAXFILENAME+16]; |
| 187 |
int zipok; |
| 188 |
int err=0; |
| 189 |
int size_buf=0; |
| 190 |
void* buf=NULL; |
| 191 |
const char* password=NULL; |
| 192 |
|
| 193 |
|
| 194 |
do_banner(); |
| 195 |
if (argc==1) |
| 196 |
{ |
| 197 |
do_help(); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
for (i=1;i<argc;i++) |
| 203 |
{ |
| 204 |
if ((*argv[i])=='-') |
| 205 |
{ |
| 206 |
const char *p=argv[i]+1; |
| 207 |
|
| 208 |
while ((*p)!='\0') |
| 209 |
{ |
| 210 |
char c=*(p++);; |
| 211 |
if ((c=='o') || (c=='O')) |
| 212 |
opt_overwrite = 1; |
| 213 |
if ((c=='a') || (c=='A')) |
| 214 |
opt_overwrite = 2; |
| 215 |
if ((c>='0') && (c<='9')) |
| 216 |
opt_compress_level = c-'0'; |
| 217 |
|
| 218 |
if (((c=='p') || (c=='P')) && (i+1<argc)) |
| 219 |
{ |
| 220 |
password=argv[i+1]; |
| 221 |
i++; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
else |
| 226 |
if (zipfilenamearg == 0) |
| 227 |
zipfilenamearg = i ; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
size_buf = WRITEBUFFERSIZE; |
| 232 |
buf = (void*)malloc(size_buf); |
| 233 |
if (buf==NULL) |
| 234 |
{ |
| 235 |
printf("Error allocating memory\n"); |
| 236 |
return ZIP_INTERNALERROR; |
| 237 |
} |
| 238 |
|
| 239 |
if (zipfilenamearg==0) |
| 240 |
zipok=0; |
| 241 |
else |
| 242 |
{ |
| 243 |
int i,len; |
| 244 |
int dot_found=0; |
| 245 |
|
| 246 |
zipok = 1 ; |
| 247 |
strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); |
| 248 |
/* strncpy doesnt append the trailing NULL, of the string is too long. */ |
| 249 |
filename_try[ MAXFILENAME ] = '\0'; |
| 250 |
|
| 251 |
len=(int)strlen(filename_try); |
| 252 |
for (i=0;i<len;i++) |
| 253 |
if (filename_try[i]=='.') |
| 254 |
dot_found=1; |
| 255 |
|
| 256 |
if (dot_found==0) |
| 257 |
strcat(filename_try,".zip"); |
| 258 |
|
| 259 |
if (opt_overwrite==2) |
| 260 |
{ |
| 261 |
/* if the file don't exist, we not append file */ |
| 262 |
if (check_exist_file(filename_try)==0) |
| 263 |
opt_overwrite=1; |
| 264 |
} |
| 265 |
else |
| 266 |
if (opt_overwrite==0) |
| 267 |
if (check_exist_file(filename_try)!=0) |
| 268 |
{ |
| 269 |
char rep=0; |
| 270 |
do |
| 271 |
{ |
| 272 |
char answer[128]; |
| 273 |
printf("The file %s exist. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try); |
| 274 |
scanf("%1s",answer); |
| 275 |
rep = answer[0] ; |
| 276 |
if ((rep>='a') && (rep<='z')) |
| 277 |
rep -= 0x20; |
| 278 |
} |
| 279 |
while ((rep!='Y') && (rep!='N') && (rep!='A')); |
| 280 |
if (rep=='N') |
| 281 |
zipok = 0; |
| 282 |
if (rep=='A') |
| 283 |
opt_overwrite = 2; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (zipok==1) |
| 288 |
{ |
| 289 |
zipFile zf; |
| 290 |
int errclose; |
| 291 |
# ifdef USEWIN32IOAPI |
| 292 |
zlib_filefunc_def ffunc; |
| 293 |
fill_win32_filefunc(&ffunc); |
| 294 |
zf = zipOpen2(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); |
| 295 |
# else |
| 296 |
zf = zipOpen(filename_try,(opt_overwrite==2) ? 2 : 0); |
| 297 |
# endif |
| 298 |
|
| 299 |
if (zf == NULL) |
| 300 |
{ |
| 301 |
printf("error opening %s\n",filename_try); |
| 302 |
err= ZIP_ERRNO; |
| 303 |
} |
| 304 |
else |
| 305 |
printf("creating %s\n",filename_try); |
| 306 |
|
| 307 |
for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) |
| 308 |
{ |
| 309 |
if (((*(argv[i]))!='-') && ((*(argv[i]))!='/')) |
| 310 |
{ |
| 311 |
FILE * fin; |
| 312 |
int size_read; |
| 313 |
const char* filenameinzip = argv[i]; |
| 314 |
zip_fileinfo zi; |
| 315 |
unsigned long crcFile=0; |
| 316 |
|
| 317 |
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = |
| 318 |
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; |
| 319 |
zi.dosDate = 0; |
| 320 |
zi.internal_fa = 0; |
| 321 |
zi.external_fa = 0; |
| 322 |
filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); |
| 323 |
|
| 324 |
/* |
| 325 |
err = zipOpenNewFileInZip(zf,filenameinzip,&zi, |
| 326 |
NULL,0,NULL,0,NULL / * comment * /, |
| 327 |
(opt_compress_level != 0) ? Z_DEFLATED : 0, |
| 328 |
opt_compress_level); |
| 329 |
*/ |
| 330 |
if ((password != NULL) && (err==ZIP_OK)) |
| 331 |
err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); |
| 332 |
|
| 333 |
err = zipOpenNewFileInZip3(zf,filenameinzip,&zi, |
| 334 |
NULL,0,NULL,0,NULL /* comment*/, |
| 335 |
(opt_compress_level != 0) ? Z_DEFLATED : 0, |
| 336 |
opt_compress_level,0, |
| 337 |
/* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ |
| 338 |
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, |
| 339 |
password,crcFile); |
| 340 |
|
| 341 |
if (err != ZIP_OK) |
| 342 |
printf("error in opening %s in zipfile\n",filenameinzip); |
| 343 |
else |
| 344 |
{ |
| 345 |
fin = fopen(filenameinzip,"rb"); |
| 346 |
if (fin==NULL) |
| 347 |
{ |
| 348 |
err=ZIP_ERRNO; |
| 349 |
printf("error in opening %s for reading\n",filenameinzip); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (err == ZIP_OK) |
| 354 |
do |
| 355 |
{ |
| 356 |
err = ZIP_OK; |
| 357 |
size_read = (int)fread(buf,1,size_buf,fin); |
| 358 |
if (size_read < size_buf) |
| 359 |
if (feof(fin)==0) |
| 360 |
{ |
| 361 |
printf("error in reading %s\n",filenameinzip); |
| 362 |
err = ZIP_ERRNO; |
| 363 |
} |
| 364 |
|
| 365 |
if (size_read>0) |
| 366 |
{ |
| 367 |
err = zipWriteInFileInZip (zf,buf,size_read); |
| 368 |
if (err<0) |
| 369 |
{ |
| 370 |
printf("error in writing %s in the zipfile\n", |
| 371 |
filenameinzip); |
| 372 |
} |
| 373 |
|
| 374 |
} |
| 375 |
} while ((err == ZIP_OK) && (size_read>0)); |
| 376 |
|
| 377 |
if (fin) |
| 378 |
fclose(fin); |
| 379 |
|
| 380 |
if (err<0) |
| 381 |
err=ZIP_ERRNO; |
| 382 |
else |
| 383 |
{ |
| 384 |
err = zipCloseFileInZip(zf); |
| 385 |
if (err!=ZIP_OK) |
| 386 |
printf("error in closing %s in the zipfile\n", |
| 387 |
filenameinzip); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
errclose = zipClose(zf,NULL); |
| 392 |
if (errclose != ZIP_OK) |
| 393 |
printf("error in closing %s\n",filename_try); |
| 394 |
} |
| 395 |
|
| 396 |
free(buf); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
//int main(int argc,char** argv){zip_main(argc, argv);} |
| 401 |
//gcc -L/usr/local -o minizip minizip.c ioapi.c zip.c -lz |