minizip.c 14 KB

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