minizip.c 15 KB

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