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. static int filetime(f, tmzip, dt)
  59. const 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. #if defined(unix) || defined(__APPLE__)
  81. static int filetime(f, tmzip, dt)
  82. const 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. (void)dt;
  87. int ret=0;
  88. struct stat s; /* results of stat() */
  89. struct tm* filedate;
  90. time_t tm_t=0;
  91. if (strcmp(f,"-")!=0)
  92. {
  93. char name[MAXFILENAME+1];
  94. size_t len = strlen(f);
  95. if (len > MAXFILENAME)
  96. len = MAXFILENAME;
  97. strncpy(name, f,MAXFILENAME-1);
  98. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  99. name[ MAXFILENAME ] = '\0';
  100. if (name[len - 1] == '/')
  101. name[len - 1] = '\0';
  102. /* not all systems allow stat'ing a file with / appended */
  103. if (stat(name,&s)==0)
  104. {
  105. tm_t = s.st_mtime;
  106. ret = 1;
  107. }
  108. }
  109. filedate = localtime(&tm_t);
  110. tmzip->tm_sec = filedate->tm_sec;
  111. tmzip->tm_min = filedate->tm_min;
  112. tmzip->tm_hour = filedate->tm_hour;
  113. tmzip->tm_mday = filedate->tm_mday;
  114. tmzip->tm_mon = filedate->tm_mon ;
  115. tmzip->tm_year = filedate->tm_year;
  116. return ret;
  117. }
  118. #else
  119. uLong filetime(f, tmzip, dt)
  120. const char *f; /* name of file to get info on */
  121. tm_zip *tmzip; /* return value: access, modific. and creation times */
  122. uLong *dt; /* dostime */
  123. {
  124. return 0;
  125. }
  126. #endif
  127. #endif
  128. static int check_exist_file(filename)
  129. const char* filename;
  130. {
  131. FILE* ftestexist;
  132. int ret = 1;
  133. ftestexist = FOPEN_FUNC(filename,"rb");
  134. if (ftestexist==NULL)
  135. ret = 0;
  136. else
  137. fclose(ftestexist);
  138. return ret;
  139. }
  140. static void do_banner()
  141. {
  142. printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
  143. printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
  144. }
  145. static void do_help()
  146. {
  147. printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
  148. " -o Overwrite existing file.zip\n" \
  149. " -a Append to existing file.zip\n" \
  150. " -0 Store only\n" \
  151. " -1 Compress faster\n" \
  152. " -9 Compress better\n\n" \
  153. " -j exclude path. store only the file name.\n\n");
  154. }
  155. /* calculate the CRC32 of a file,
  156. because to encrypt a file, we need known the CRC32 of the file before */
  157. static int getFileCrc(const char* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc)
  158. {
  159. unsigned long calculate_crc=0;
  160. int err=ZIP_OK;
  161. FILE * fin = FOPEN_FUNC(filenameinzip,"rb");
  162. unsigned long size_read = 0;
  163. unsigned long total_read = 0;
  164. if (fin==NULL)
  165. {
  166. err = ZIP_ERRNO;
  167. }
  168. if (err == ZIP_OK)
  169. do
  170. {
  171. err = ZIP_OK;
  172. size_read = fread(buf,1,size_buf,fin);
  173. if (size_read < size_buf)
  174. if (feof(fin)==0)
  175. {
  176. printf("error in reading %s\n",filenameinzip);
  177. err = ZIP_ERRNO;
  178. }
  179. if (size_read>0)
  180. calculate_crc = crc32_z(calculate_crc,buf,size_read);
  181. total_read += size_read;
  182. } while ((err == ZIP_OK) && (size_read>0));
  183. if (fin)
  184. fclose(fin);
  185. *result_crc=calculate_crc;
  186. printf("file %s crc %lx\n", filenameinzip, calculate_crc);
  187. return err;
  188. }
  189. static int isLargeFile(const char* filename)
  190. {
  191. int largeFile = 0;
  192. ZPOS64_T pos = 0;
  193. FILE* pFile = FOPEN_FUNC(filename, "rb");
  194. if(pFile != NULL)
  195. {
  196. FSEEKO_FUNC(pFile, 0, SEEK_END);
  197. pos = (ZPOS64_T)FTELLO_FUNC(pFile);
  198. printf("File : %s is %lld bytes\n", filename, pos);
  199. if(pos >= 0xffffffff)
  200. largeFile = 1;
  201. fclose(pFile);
  202. }
  203. return largeFile;
  204. }
  205. int main(argc,argv)
  206. int argc;
  207. char *argv[];
  208. {
  209. int i;
  210. int opt_overwrite=0;
  211. int opt_compress_level=Z_DEFAULT_COMPRESSION;
  212. int opt_exclude_path=0;
  213. int zipfilenamearg = 0;
  214. char filename_try[MAXFILENAME+16];
  215. int zipok;
  216. int err=0;
  217. size_t size_buf=0;
  218. void* buf=NULL;
  219. const char* password=NULL;
  220. do_banner();
  221. if (argc==1)
  222. {
  223. do_help();
  224. return 0;
  225. }
  226. else
  227. {
  228. for (i=1;i<argc;i++)
  229. {
  230. if ((*argv[i])=='-')
  231. {
  232. const char *p=argv[i]+1;
  233. while ((*p)!='\0')
  234. {
  235. char c=*(p++);;
  236. if ((c=='o') || (c=='O'))
  237. opt_overwrite = 1;
  238. if ((c=='a') || (c=='A'))
  239. opt_overwrite = 2;
  240. if ((c>='0') && (c<='9'))
  241. opt_compress_level = c-'0';
  242. if ((c=='j') || (c=='J'))
  243. opt_exclude_path = 1;
  244. if (((c=='p') || (c=='P')) && (i+1<argc))
  245. {
  246. password=argv[i+1];
  247. i++;
  248. }
  249. }
  250. }
  251. else
  252. {
  253. if (zipfilenamearg == 0)
  254. {
  255. zipfilenamearg = i ;
  256. }
  257. }
  258. }
  259. }
  260. size_buf = WRITEBUFFERSIZE;
  261. buf = (void*)malloc(size_buf);
  262. if (buf==NULL)
  263. {
  264. printf("Error allocating memory\n");
  265. return ZIP_INTERNALERROR;
  266. }
  267. if (zipfilenamearg==0)
  268. {
  269. zipok=0;
  270. }
  271. else
  272. {
  273. int i,len;
  274. int dot_found=0;
  275. zipok = 1 ;
  276. strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
  277. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  278. filename_try[ MAXFILENAME ] = '\0';
  279. len=(int)strlen(filename_try);
  280. for (i=0;i<len;i++)
  281. if (filename_try[i]=='.')
  282. dot_found=1;
  283. if (dot_found==0)
  284. strcat(filename_try,".zip");
  285. if (opt_overwrite==2)
  286. {
  287. /* if the file don't exist, we not append file */
  288. if (check_exist_file(filename_try)==0)
  289. opt_overwrite=1;
  290. }
  291. else
  292. if (opt_overwrite==0)
  293. if (check_exist_file(filename_try)!=0)
  294. {
  295. char rep=0;
  296. do
  297. {
  298. char answer[128];
  299. int ret;
  300. printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
  301. ret = scanf("%1s",answer);
  302. if (ret != 1)
  303. {
  304. exit(EXIT_FAILURE);
  305. }
  306. rep = answer[0] ;
  307. if ((rep>='a') && (rep<='z'))
  308. rep -= 0x20;
  309. }
  310. while ((rep!='Y') && (rep!='N') && (rep!='A'));
  311. if (rep=='N')
  312. zipok = 0;
  313. if (rep=='A')
  314. opt_overwrite = 2;
  315. }
  316. }
  317. if (zipok==1)
  318. {
  319. zipFile zf;
  320. int errclose;
  321. # ifdef USEWIN32IOAPI
  322. zlib_filefunc64_def ffunc;
  323. fill_win32_filefunc64A(&ffunc);
  324. zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
  325. # else
  326. zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
  327. # endif
  328. if (zf == NULL)
  329. {
  330. printf("error opening %s\n",filename_try);
  331. err= ZIP_ERRNO;
  332. }
  333. else
  334. printf("creating %s\n",filename_try);
  335. for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
  336. {
  337. if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
  338. ((argv[i][1]=='o') || (argv[i][1]=='O') ||
  339. (argv[i][1]=='a') || (argv[i][1]=='A') ||
  340. (argv[i][1]=='p') || (argv[i][1]=='P') ||
  341. ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
  342. (strlen(argv[i]) == 2)))
  343. {
  344. FILE * fin;
  345. size_t size_read;
  346. const char* filenameinzip = argv[i];
  347. const char *savefilenameinzip;
  348. zip_fileinfo zi;
  349. unsigned long crcFile=0;
  350. int zip64 = 0;
  351. zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  352. zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  353. zi.dosDate = 0;
  354. zi.internal_fa = 0;
  355. zi.external_fa = 0;
  356. filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
  357. /*
  358. err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
  359. NULL,0,NULL,0,NULL / * comment * /,
  360. (opt_compress_level != 0) ? Z_DEFLATED : 0,
  361. opt_compress_level);
  362. */
  363. if ((password != NULL) && (err==ZIP_OK))
  364. err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
  365. zip64 = isLargeFile(filenameinzip);
  366. /* The path name saved, should not include a leading slash. */
  367. /*if it did, windows/xp and dynazip couldn't read the zip file. */
  368. savefilenameinzip = filenameinzip;
  369. while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
  370. {
  371. savefilenameinzip++;
  372. }
  373. /*should the zip file contain any path at all?*/
  374. if( opt_exclude_path )
  375. {
  376. const char *tmpptr;
  377. const char *lastslash = 0;
  378. for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
  379. {
  380. if( *tmpptr == '\\' || *tmpptr == '/')
  381. {
  382. lastslash = tmpptr;
  383. }
  384. }
  385. if( lastslash != NULL )
  386. {
  387. savefilenameinzip = lastslash+1; // base filename follows last slash.
  388. }
  389. }
  390. /**/
  391. err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
  392. NULL,0,NULL,0,NULL /* comment*/,
  393. (opt_compress_level != 0) ? Z_DEFLATED : 0,
  394. opt_compress_level,0,
  395. /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
  396. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  397. password,crcFile, zip64);
  398. if (err != ZIP_OK)
  399. printf("error in opening %s in zipfile\n",filenameinzip);
  400. else
  401. {
  402. fin = FOPEN_FUNC(filenameinzip,"rb");
  403. if (fin==NULL)
  404. {
  405. err=ZIP_ERRNO;
  406. printf("error in opening %s for reading\n",filenameinzip);
  407. }
  408. }
  409. if (err == ZIP_OK)
  410. do
  411. {
  412. err = ZIP_OK;
  413. size_read = fread(buf,1,size_buf,fin);
  414. if (size_read < size_buf)
  415. if (feof(fin)==0)
  416. {
  417. printf("error in reading %s\n",filenameinzip);
  418. err = ZIP_ERRNO;
  419. }
  420. if (size_read>0)
  421. {
  422. err = zipWriteInFileInZip (zf,buf,(unsigned)size_read);
  423. if (err<0)
  424. {
  425. printf("error in writing %s in the zipfile\n",
  426. filenameinzip);
  427. }
  428. }
  429. } while ((err == ZIP_OK) && (size_read>0));
  430. if (fin)
  431. fclose(fin);
  432. if (err<0)
  433. err=ZIP_ERRNO;
  434. else
  435. {
  436. err = zipCloseFileInZip(zf);
  437. if (err!=ZIP_OK)
  438. printf("error in closing %s in the zipfile\n",
  439. filenameinzip);
  440. }
  441. }
  442. }
  443. errclose = zipClose(zf,NULL);
  444. if (errclose != ZIP_OK)
  445. printf("error in closing %s\n",filename_try);
  446. }
  447. else
  448. {
  449. do_help();
  450. }
  451. free(buf);
  452. return 0;
  453. }