minizip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. minizip.c
  3. Version 1.1, February 14h, 2010
  4. sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
  5. Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
  6. Modifications of Unzip for Zip64
  7. Copyright (C) 2007-2008 Even Rouault
  8. Modifications for Zip64 support on both zip and unzip
  9. Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
  10. */
  11. #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
  12. #ifndef __USE_FILE_OFFSET64
  13. #define __USE_FILE_OFFSET64
  14. #endif
  15. #ifndef __USE_LARGEFILE64
  16. #define __USE_LARGEFILE64
  17. #endif
  18. #ifndef _LARGEFILE64_SOURCE
  19. #define _LARGEFILE64_SOURCE
  20. #endif
  21. #ifndef _FILE_OFFSET_BIT
  22. #define _FILE_OFFSET_BIT 64
  23. #endif
  24. #endif
  25. #ifdef __APPLE__
  26. // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
  27. #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
  28. #define FTELLO_FUNC(stream) ftello(stream)
  29. #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
  30. #else
  31. #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
  32. #define FTELLO_FUNC(stream) ftello64(stream)
  33. #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
  34. #endif
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #ifdef _WIN32
  42. # include <direct.h>
  43. # include <io.h>
  44. #else
  45. # include <unistd.h>
  46. # include <utime.h>
  47. # include <sys/types.h>
  48. # include <sys/stat.h>
  49. #endif
  50. #include "zip.h"
  51. #ifdef _WIN32
  52. #define USEWIN32IOAPI
  53. #include "iowin32.h"
  54. #endif
  55. #define WRITEBUFFERSIZE (16384)
  56. #define MAXFILENAME (256)
  57. #ifdef _WIN32
  58. uLong filetime(f, tmzip, dt)
  59. char *f; /* name of file to get info on */
  60. tm_zip *tmzip; /* return value: access, modific. and creation times */
  61. uLong *dt; /* dostime */
  62. {
  63. int ret = 0;
  64. {
  65. FILETIME ftLocal;
  66. HANDLE hFind;
  67. WIN32_FIND_DATAA ff32;
  68. hFind = FindFirstFileA(f,&ff32);
  69. if (hFind != INVALID_HANDLE_VALUE)
  70. {
  71. FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
  72. FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
  73. FindClose(hFind);
  74. ret = 1;
  75. }
  76. }
  77. return ret;
  78. }
  79. #else
  80. #ifdef unix || __APPLE__
  81. uLong filetime(f, tmzip, dt)
  82. char *f; /* name of file to get info on */
  83. tm_zip *tmzip; /* return value: access, modific. and creation times */
  84. uLong *dt; /* dostime */
  85. {
  86. int ret=0;
  87. struct stat s; /* results of stat() */
  88. struct tm* filedate;
  89. time_t tm_t=0;
  90. if (strcmp(f,"-")!=0)
  91. {
  92. char name[MAXFILENAME+1];
  93. int len = strlen(f);
  94. if (len > MAXFILENAME)
  95. len = MAXFILENAME;
  96. strncpy(name, f,MAXFILENAME-1);
  97. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  98. name[ MAXFILENAME ] = '\0';
  99. if (name[len - 1] == '/')
  100. name[len - 1] = '\0';
  101. /* not all systems allow stat'ing a file with / appended */
  102. if (stat(name,&s)==0)
  103. {
  104. tm_t = s.st_mtime;
  105. ret = 1;
  106. }
  107. }
  108. filedate = localtime(&tm_t);
  109. tmzip->tm_sec = filedate->tm_sec;
  110. tmzip->tm_min = filedate->tm_min;
  111. tmzip->tm_hour = filedate->tm_hour;
  112. tmzip->tm_mday = filedate->tm_mday;
  113. tmzip->tm_mon = filedate->tm_mon ;
  114. tmzip->tm_year = filedate->tm_year;
  115. return ret;
  116. }
  117. #else
  118. uLong filetime(f, tmzip, dt)
  119. char *f; /* name of file to get info on */
  120. tm_zip *tmzip; /* return value: access, modific. and creation times */
  121. uLong *dt; /* dostime */
  122. {
  123. return 0;
  124. }
  125. #endif
  126. #endif
  127. int check_exist_file(filename)
  128. const char* filename;
  129. {
  130. FILE* ftestexist;
  131. int ret = 1;
  132. ftestexist = FOPEN_FUNC(filename,"rb");
  133. if (ftestexist==NULL)
  134. ret = 0;
  135. else
  136. fclose(ftestexist);
  137. return ret;
  138. }
  139. void do_banner()
  140. {
  141. printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
  142. printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
  143. }
  144. void do_help()
  145. {
  146. printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
  147. " -o Overwrite existing file.zip\n" \
  148. " -a Append to existing file.zip\n" \
  149. " -0 Store only\n" \
  150. " -1 Compress faster\n" \
  151. " -9 Compress better\n\n" \
  152. " -j exclude path. store only the file name.\n\n");
  153. }
  154. /* calculate the CRC32 of a file,
  155. because to encrypt a file, we need known the CRC32 of the file before */
  156. int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
  157. {
  158. unsigned long calculate_crc=0;
  159. int err=ZIP_OK;
  160. FILE * fin = FOPEN_FUNC(filenameinzip,"rb");
  161. unsigned long size_read = 0;
  162. unsigned long total_read = 0;
  163. if (fin==NULL)
  164. {
  165. err = ZIP_ERRNO;
  166. }
  167. if (err == ZIP_OK)
  168. do
  169. {
  170. err = ZIP_OK;
  171. size_read = (int)fread(buf,1,size_buf,fin);
  172. if (size_read < size_buf)
  173. if (feof(fin)==0)
  174. {
  175. printf("error in reading %s\n",filenameinzip);
  176. err = ZIP_ERRNO;
  177. }
  178. if (size_read>0)
  179. calculate_crc = crc32(calculate_crc,buf,size_read);
  180. total_read += size_read;
  181. } while ((err == ZIP_OK) && (size_read>0));
  182. if (fin)
  183. fclose(fin);
  184. *result_crc=calculate_crc;
  185. printf("file %s crc %lx\n", filenameinzip, calculate_crc);
  186. return err;
  187. }
  188. int isLargeFile(const char* filename)
  189. {
  190. int largeFile = 0;
  191. ZPOS64_T pos = 0;
  192. FILE* pFile = FOPEN_FUNC(filename, "rb");
  193. if(pFile != NULL)
  194. {
  195. int n = FSEEKO_FUNC(pFile, 0, SEEK_END);
  196. pos = FTELLO_FUNC(pFile);
  197. printf("File : %s is %lld bytes\n", filename, pos);
  198. if(pos >= 0xffffffff)
  199. largeFile = 1;
  200. fclose(pFile);
  201. }
  202. return largeFile;
  203. }
  204. int main(argc,argv)
  205. int argc;
  206. char *argv[];
  207. {
  208. int i;
  209. int opt_overwrite=0;
  210. int opt_compress_level=Z_DEFAULT_COMPRESSION;
  211. int opt_exclude_path=0;
  212. int zipfilenamearg = 0;
  213. char filename_try[MAXFILENAME+16];
  214. int zipok;
  215. int err=0;
  216. int size_buf=0;
  217. void* buf=NULL;
  218. const char* password=NULL;
  219. do_banner();
  220. if (argc==1)
  221. {
  222. do_help();
  223. return 0;
  224. }
  225. else
  226. {
  227. for (i=1;i<argc;i++)
  228. {
  229. if ((*argv[i])=='-')
  230. {
  231. const char *p=argv[i]+1;
  232. while ((*p)!='\0')
  233. {
  234. char c=*(p++);;
  235. if ((c=='o') || (c=='O'))
  236. opt_overwrite = 1;
  237. if ((c=='a') || (c=='A'))
  238. opt_overwrite = 2;
  239. if ((c>='0') && (c<='9'))
  240. opt_compress_level = c-'0';
  241. if ((c=='j') || (c=='J'))
  242. opt_exclude_path = 1;
  243. if (((c=='p') || (c=='P')) && (i+1<argc))
  244. {
  245. password=argv[i+1];
  246. i++;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. if (zipfilenamearg == 0)
  253. {
  254. zipfilenamearg = i ;
  255. }
  256. }
  257. }
  258. }
  259. size_buf = WRITEBUFFERSIZE;
  260. buf = (void*)malloc(size_buf);
  261. if (buf==NULL)
  262. {
  263. printf("Error allocating memory\n");
  264. return ZIP_INTERNALERROR;
  265. }
  266. if (zipfilenamearg==0)
  267. {
  268. zipok=0;
  269. }
  270. else
  271. {
  272. int i,len;
  273. int dot_found=0;
  274. zipok = 1 ;
  275. strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
  276. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  277. filename_try[ MAXFILENAME ] = '\0';
  278. len=(int)strlen(filename_try);
  279. for (i=0;i<len;i++)
  280. if (filename_try[i]=='.')
  281. dot_found=1;
  282. if (dot_found==0)
  283. strcat(filename_try,".zip");
  284. if (opt_overwrite==2)
  285. {
  286. /* if the file don't exist, we not append file */
  287. if (check_exist_file(filename_try)==0)
  288. opt_overwrite=1;
  289. }
  290. else
  291. if (opt_overwrite==0)
  292. if (check_exist_file(filename_try)!=0)
  293. {
  294. char rep=0;
  295. do
  296. {
  297. char answer[128];
  298. int ret;
  299. printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
  300. ret = scanf("%1s",answer);
  301. if (ret != 1)
  302. {
  303. exit(EXIT_FAILURE);
  304. }
  305. rep = answer[0] ;
  306. if ((rep>='a') && (rep<='z'))
  307. rep -= 0x20;
  308. }
  309. while ((rep!='Y') && (rep!='N') && (rep!='A'));
  310. if (rep=='N')
  311. zipok = 0;
  312. if (rep=='A')
  313. opt_overwrite = 2;
  314. }
  315. }
  316. if (zipok==1)
  317. {
  318. zipFile zf;
  319. int errclose;
  320. # ifdef USEWIN32IOAPI
  321. zlib_filefunc64_def ffunc;
  322. fill_win32_filefunc64A(&ffunc);
  323. zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
  324. # else
  325. zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
  326. # endif
  327. if (zf == NULL)
  328. {
  329. printf("error opening %s\n",filename_try);
  330. err= ZIP_ERRNO;
  331. }
  332. else
  333. printf("creating %s\n",filename_try);
  334. for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
  335. {
  336. if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
  337. ((argv[i][1]=='o') || (argv[i][1]=='O') ||
  338. (argv[i][1]=='a') || (argv[i][1]=='A') ||
  339. (argv[i][1]=='p') || (argv[i][1]=='P') ||
  340. ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
  341. (strlen(argv[i]) == 2)))
  342. {
  343. FILE * fin;
  344. int size_read;
  345. const char* filenameinzip = argv[i];
  346. const char *savefilenameinzip;
  347. zip_fileinfo zi;
  348. unsigned long crcFile=0;
  349. int zip64 = 0;
  350. zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  351. zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  352. zi.dosDate = 0;
  353. zi.internal_fa = 0;
  354. zi.external_fa = 0;
  355. filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
  356. /*
  357. err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
  358. NULL,0,NULL,0,NULL / * comment * /,
  359. (opt_compress_level != 0) ? Z_DEFLATED : 0,
  360. opt_compress_level);
  361. */
  362. if ((password != NULL) && (err==ZIP_OK))
  363. err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
  364. zip64 = isLargeFile(filenameinzip);
  365. /* The path name saved, should not include a leading slash. */
  366. /*if it did, windows/xp and dynazip couldn't read the zip file. */
  367. savefilenameinzip = filenameinzip;
  368. while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
  369. {
  370. savefilenameinzip++;
  371. }
  372. /*should the zip file contain any path at all?*/
  373. if( opt_exclude_path )
  374. {
  375. const char *tmpptr;
  376. const char *lastslash = 0;
  377. for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
  378. {
  379. if( *tmpptr == '\\' || *tmpptr == '/')
  380. {
  381. lastslash = tmpptr;
  382. }
  383. }
  384. if( lastslash != NULL )
  385. {
  386. savefilenameinzip = lastslash+1; // base filename follows last slash.
  387. }
  388. }
  389. /**/
  390. err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
  391. NULL,0,NULL,0,NULL /* comment*/,
  392. (opt_compress_level != 0) ? Z_DEFLATED : 0,
  393. opt_compress_level,0,
  394. /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
  395. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  396. password,crcFile, zip64);
  397. if (err != ZIP_OK)
  398. printf("error in opening %s in zipfile\n",filenameinzip);
  399. else
  400. {
  401. fin = FOPEN_FUNC(filenameinzip,"rb");
  402. if (fin==NULL)
  403. {
  404. err=ZIP_ERRNO;
  405. printf("error in opening %s for reading\n",filenameinzip);
  406. }
  407. }
  408. if (err == ZIP_OK)
  409. do
  410. {
  411. err = ZIP_OK;
  412. size_read = (int)fread(buf,1,size_buf,fin);
  413. if (size_read < size_buf)
  414. if (feof(fin)==0)
  415. {
  416. printf("error in reading %s\n",filenameinzip);
  417. err = ZIP_ERRNO;
  418. }
  419. if (size_read>0)
  420. {
  421. err = zipWriteInFileInZip (zf,buf,size_read);
  422. if (err<0)
  423. {
  424. printf("error in writing %s in the zipfile\n",
  425. filenameinzip);
  426. }
  427. }
  428. } while ((err == ZIP_OK) && (size_read>0));
  429. if (fin)
  430. fclose(fin);
  431. if (err<0)
  432. err=ZIP_ERRNO;
  433. else
  434. {
  435. err = zipCloseFileInZip(zf);
  436. if (err!=ZIP_OK)
  437. printf("error in closing %s in the zipfile\n",
  438. filenameinzip);
  439. }
  440. }
  441. }
  442. errclose = zipClose(zf,NULL);
  443. if (errclose != ZIP_OK)
  444. printf("error in closing %s\n",filename_try);
  445. }
  446. else
  447. {
  448. do_help();
  449. }
  450. free(buf);
  451. return 0;
  452. }