miniunz.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. miniunz.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. #endif
  48. #include "unzip.h"
  49. #define CASESENSITIVITY (0)
  50. #define WRITEBUFFERSIZE (8192)
  51. #define MAXFILENAME (256)
  52. #ifdef _WIN32
  53. #define USEWIN32IOAPI
  54. #include "iowin32.h"
  55. #endif
  56. /*
  57. mini unzip, demo of unzip package
  58. usage :
  59. Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
  60. list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
  61. if it exists
  62. */
  63. /* change_file_date : change the date/time of a file
  64. filename : the filename of the file where date/time must be modified
  65. dosdate : the new date at the MSDos format (4 bytes)
  66. tmu_date : the SAME new date at the tm_unz format */
  67. void change_file_date(filename,dosdate,tmu_date)
  68. const char *filename;
  69. uLong dosdate;
  70. tm_unz tmu_date;
  71. {
  72. #ifdef _WIN32
  73. HANDLE hFile;
  74. FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
  75. hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
  76. 0,NULL,OPEN_EXISTING,0,NULL);
  77. GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
  78. DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
  79. LocalFileTimeToFileTime(&ftLocal,&ftm);
  80. SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
  81. CloseHandle(hFile);
  82. #else
  83. #ifdef unix || __APPLE__
  84. struct utimbuf ut;
  85. struct tm newdate;
  86. newdate.tm_sec = tmu_date.tm_sec;
  87. newdate.tm_min=tmu_date.tm_min;
  88. newdate.tm_hour=tmu_date.tm_hour;
  89. newdate.tm_mday=tmu_date.tm_mday;
  90. newdate.tm_mon=tmu_date.tm_mon;
  91. if (tmu_date.tm_year > 1900)
  92. newdate.tm_year=tmu_date.tm_year - 1900;
  93. else
  94. newdate.tm_year=tmu_date.tm_year ;
  95. newdate.tm_isdst=-1;
  96. ut.actime=ut.modtime=mktime(&newdate);
  97. utime(filename,&ut);
  98. #endif
  99. #endif
  100. }
  101. /* mymkdir and change_file_date are not 100 % portable
  102. As I don't know well Unix, I wait feedback for the unix portion */
  103. int mymkdir(dirname)
  104. const char* dirname;
  105. {
  106. int ret=0;
  107. #ifdef _WIN32
  108. ret = _mkdir(dirname);
  109. #elif unix
  110. ret = mkdir (dirname,0775);
  111. #elif __APPLE__
  112. ret = mkdir (dirname,0775);
  113. #endif
  114. return ret;
  115. }
  116. int makedir (newdir)
  117. char *newdir;
  118. {
  119. char *buffer ;
  120. char *p;
  121. int len = (int)strlen(newdir);
  122. if (len <= 0)
  123. return 0;
  124. buffer = (char*)malloc(len+1);
  125. if (buffer==NULL)
  126. {
  127. printf("Error allocating memory\n");
  128. return UNZ_INTERNALERROR;
  129. }
  130. strcpy(buffer,newdir);
  131. if (buffer[len-1] == '/') {
  132. buffer[len-1] = '\0';
  133. }
  134. if (mymkdir(buffer) == 0)
  135. {
  136. free(buffer);
  137. return 1;
  138. }
  139. p = buffer+1;
  140. while (1)
  141. {
  142. char hold;
  143. while(*p && *p != '\\' && *p != '/')
  144. p++;
  145. hold = *p;
  146. *p = 0;
  147. if ((mymkdir(buffer) == -1) && (errno == ENOENT))
  148. {
  149. printf("couldn't create directory %s\n",buffer);
  150. free(buffer);
  151. return 0;
  152. }
  153. if (hold == 0)
  154. break;
  155. *p++ = hold;
  156. }
  157. free(buffer);
  158. return 1;
  159. }
  160. void do_banner()
  161. {
  162. printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
  163. printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
  164. }
  165. void do_help()
  166. {
  167. printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
  168. " -e Extract without pathname (junk paths)\n" \
  169. " -x Extract with pathname\n" \
  170. " -v list files\n" \
  171. " -l list files\n" \
  172. " -d directory to extract into\n" \
  173. " -o overwrite files without prompting\n" \
  174. " -p extract crypted file using password\n\n");
  175. }
  176. void Display64BitsSize(ZPOS64_T n, int size_char)
  177. {
  178. /* to avoid compatibility problem , we do here the conversion */
  179. char number[21];
  180. int offset=19;
  181. int pos_string = 19;
  182. number[20]=0;
  183. for (;;) {
  184. number[offset]=(char)((n%10)+'0');
  185. if (number[offset] != '0')
  186. pos_string=offset;
  187. n/=10;
  188. if (offset==0)
  189. break;
  190. offset--;
  191. }
  192. {
  193. int size_display_string = 19-pos_string;
  194. while (size_char > size_display_string)
  195. {
  196. size_char--;
  197. printf(" ");
  198. }
  199. }
  200. printf("%s",&number[pos_string]);
  201. }
  202. int do_list(uf)
  203. unzFile uf;
  204. {
  205. uLong i;
  206. unz_global_info64 gi;
  207. int err;
  208. err = unzGetGlobalInfo64(uf,&gi);
  209. if (err!=UNZ_OK)
  210. printf("error %d with zipfile in unzGetGlobalInfo \n",err);
  211. printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
  212. printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
  213. for (i=0;i<gi.number_entry;i++)
  214. {
  215. char filename_inzip[256];
  216. unz_file_info64 file_info;
  217. uLong ratio=0;
  218. const char *string_method;
  219. char charCrypt=' ';
  220. err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
  221. if (err!=UNZ_OK)
  222. {
  223. printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
  224. break;
  225. }
  226. if (file_info.uncompressed_size>0)
  227. ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
  228. /* display a '*' if the file is crypted */
  229. if ((file_info.flag & 1) != 0)
  230. charCrypt='*';
  231. if (file_info.compression_method==0)
  232. string_method="Stored";
  233. else
  234. if (file_info.compression_method==Z_DEFLATED)
  235. {
  236. uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
  237. if (iLevel==0)
  238. string_method="Defl:N";
  239. else if (iLevel==1)
  240. string_method="Defl:X";
  241. else if ((iLevel==2) || (iLevel==3))
  242. string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
  243. }
  244. else
  245. if (file_info.compression_method==Z_BZIP2ED)
  246. {
  247. string_method="BZip2 ";
  248. }
  249. else
  250. string_method="Unkn. ";
  251. Display64BitsSize(file_info.uncompressed_size,7);
  252. printf(" %6s%c",string_method,charCrypt);
  253. Display64BitsSize(file_info.compressed_size,7);
  254. printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
  255. ratio,
  256. (uLong)file_info.tmu_date.tm_mon + 1,
  257. (uLong)file_info.tmu_date.tm_mday,
  258. (uLong)file_info.tmu_date.tm_year % 100,
  259. (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
  260. (uLong)file_info.crc,filename_inzip);
  261. if ((i+1)<gi.number_entry)
  262. {
  263. err = unzGoToNextFile(uf);
  264. if (err!=UNZ_OK)
  265. {
  266. printf("error %d with zipfile in unzGoToNextFile\n",err);
  267. break;
  268. }
  269. }
  270. }
  271. return 0;
  272. }
  273. int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
  274. unzFile uf;
  275. const int* popt_extract_without_path;
  276. int* popt_overwrite;
  277. const char* password;
  278. {
  279. char filename_inzip[256];
  280. char* filename_withoutpath;
  281. char* p;
  282. int err=UNZ_OK;
  283. FILE *fout=NULL;
  284. void* buf;
  285. uInt size_buf;
  286. unz_file_info64 file_info;
  287. uLong ratio=0;
  288. err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
  289. if (err!=UNZ_OK)
  290. {
  291. printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
  292. return err;
  293. }
  294. size_buf = WRITEBUFFERSIZE;
  295. buf = (void*)malloc(size_buf);
  296. if (buf==NULL)
  297. {
  298. printf("Error allocating memory\n");
  299. return UNZ_INTERNALERROR;
  300. }
  301. p = filename_withoutpath = filename_inzip;
  302. while ((*p) != '\0')
  303. {
  304. if (((*p)=='/') || ((*p)=='\\'))
  305. filename_withoutpath = p+1;
  306. p++;
  307. }
  308. if ((*filename_withoutpath)=='\0')
  309. {
  310. if ((*popt_extract_without_path)==0)
  311. {
  312. printf("creating directory: %s\n",filename_inzip);
  313. mymkdir(filename_inzip);
  314. }
  315. }
  316. else
  317. {
  318. const char* write_filename;
  319. int skip=0;
  320. if ((*popt_extract_without_path)==0)
  321. write_filename = filename_inzip;
  322. else
  323. write_filename = filename_withoutpath;
  324. err = unzOpenCurrentFilePassword(uf,password);
  325. if (err!=UNZ_OK)
  326. {
  327. printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
  328. }
  329. if (((*popt_overwrite)==0) && (err==UNZ_OK))
  330. {
  331. char rep=0;
  332. FILE* ftestexist;
  333. ftestexist = FOPEN_FUNC(write_filename,"rb");
  334. if (ftestexist!=NULL)
  335. {
  336. fclose(ftestexist);
  337. do
  338. {
  339. char answer[128];
  340. int ret;
  341. printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
  342. ret = scanf("%1s",answer);
  343. if (ret != 1)
  344. {
  345. exit(EXIT_FAILURE);
  346. }
  347. rep = answer[0] ;
  348. if ((rep>='a') && (rep<='z'))
  349. rep -= 0x20;
  350. }
  351. while ((rep!='Y') && (rep!='N') && (rep!='A'));
  352. }
  353. if (rep == 'N')
  354. skip = 1;
  355. if (rep == 'A')
  356. *popt_overwrite=1;
  357. }
  358. if ((skip==0) && (err==UNZ_OK))
  359. {
  360. fout=FOPEN_FUNC(write_filename,"wb");
  361. /* some zipfile don't contain directory alone before file */
  362. if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
  363. (filename_withoutpath!=(char*)filename_inzip))
  364. {
  365. char c=*(filename_withoutpath-1);
  366. *(filename_withoutpath-1)='\0';
  367. makedir(write_filename);
  368. *(filename_withoutpath-1)=c;
  369. fout=FOPEN_FUNC(write_filename,"wb");
  370. }
  371. if (fout==NULL)
  372. {
  373. printf("error opening %s\n",write_filename);
  374. }
  375. }
  376. if (fout!=NULL)
  377. {
  378. printf(" extracting: %s\n",write_filename);
  379. do
  380. {
  381. err = unzReadCurrentFile(uf,buf,size_buf);
  382. if (err<0)
  383. {
  384. printf("error %d with zipfile in unzReadCurrentFile\n",err);
  385. break;
  386. }
  387. if (err>0)
  388. if (fwrite(buf,err,1,fout)!=1)
  389. {
  390. printf("error in writing extracted file\n");
  391. err=UNZ_ERRNO;
  392. break;
  393. }
  394. }
  395. while (err>0);
  396. if (fout)
  397. fclose(fout);
  398. if (err==0)
  399. change_file_date(write_filename,file_info.dosDate,
  400. file_info.tmu_date);
  401. }
  402. if (err==UNZ_OK)
  403. {
  404. err = unzCloseCurrentFile (uf);
  405. if (err!=UNZ_OK)
  406. {
  407. printf("error %d with zipfile in unzCloseCurrentFile\n",err);
  408. }
  409. }
  410. else
  411. unzCloseCurrentFile(uf); /* don't lose the error */
  412. }
  413. free(buf);
  414. return err;
  415. }
  416. int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
  417. unzFile uf;
  418. int opt_extract_without_path;
  419. int opt_overwrite;
  420. const char* password;
  421. {
  422. uLong i;
  423. unz_global_info64 gi;
  424. int err;
  425. FILE* fout=NULL;
  426. err = unzGetGlobalInfo64(uf,&gi);
  427. if (err!=UNZ_OK)
  428. printf("error %d with zipfile in unzGetGlobalInfo \n",err);
  429. for (i=0;i<gi.number_entry;i++)
  430. {
  431. if (do_extract_currentfile(uf,&opt_extract_without_path,
  432. &opt_overwrite,
  433. password) != UNZ_OK)
  434. break;
  435. if ((i+1)<gi.number_entry)
  436. {
  437. err = unzGoToNextFile(uf);
  438. if (err!=UNZ_OK)
  439. {
  440. printf("error %d with zipfile in unzGoToNextFile\n",err);
  441. break;
  442. }
  443. }
  444. }
  445. return 0;
  446. }
  447. int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
  448. unzFile uf;
  449. const char* filename;
  450. int opt_extract_without_path;
  451. int opt_overwrite;
  452. const char* password;
  453. {
  454. int err = UNZ_OK;
  455. if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
  456. {
  457. printf("file %s not found in the zipfile\n",filename);
  458. return 2;
  459. }
  460. if (do_extract_currentfile(uf,&opt_extract_without_path,
  461. &opt_overwrite,
  462. password) == UNZ_OK)
  463. return 0;
  464. else
  465. return 1;
  466. }
  467. int main(argc,argv)
  468. int argc;
  469. char *argv[];
  470. {
  471. const char *zipfilename=NULL;
  472. const char *filename_to_extract=NULL;
  473. const char *password=NULL;
  474. char filename_try[MAXFILENAME+16] = "";
  475. int i;
  476. int ret_value=0;
  477. int opt_do_list=0;
  478. int opt_do_extract=1;
  479. int opt_do_extract_withoutpath=0;
  480. int opt_overwrite=0;
  481. int opt_extractdir=0;
  482. const char *dirname=NULL;
  483. unzFile uf=NULL;
  484. do_banner();
  485. if (argc==1)
  486. {
  487. do_help();
  488. return 0;
  489. }
  490. else
  491. {
  492. for (i=1;i<argc;i++)
  493. {
  494. if ((*argv[i])=='-')
  495. {
  496. const char *p=argv[i]+1;
  497. while ((*p)!='\0')
  498. {
  499. char c=*(p++);;
  500. if ((c=='l') || (c=='L'))
  501. opt_do_list = 1;
  502. if ((c=='v') || (c=='V'))
  503. opt_do_list = 1;
  504. if ((c=='x') || (c=='X'))
  505. opt_do_extract = 1;
  506. if ((c=='e') || (c=='E'))
  507. opt_do_extract = opt_do_extract_withoutpath = 1;
  508. if ((c=='o') || (c=='O'))
  509. opt_overwrite=1;
  510. if ((c=='d') || (c=='D'))
  511. {
  512. opt_extractdir=1;
  513. dirname=argv[i+1];
  514. }
  515. if (((c=='p') || (c=='P')) && (i+1<argc))
  516. {
  517. password=argv[i+1];
  518. i++;
  519. }
  520. }
  521. }
  522. else
  523. {
  524. if (zipfilename == NULL)
  525. zipfilename = argv[i];
  526. else if ((filename_to_extract==NULL) && (!opt_extractdir))
  527. filename_to_extract = argv[i] ;
  528. }
  529. }
  530. }
  531. if (zipfilename!=NULL)
  532. {
  533. # ifdef USEWIN32IOAPI
  534. zlib_filefunc64_def ffunc;
  535. # endif
  536. strncpy(filename_try, zipfilename,MAXFILENAME-1);
  537. /* strncpy doesnt append the trailing NULL, of the string is too long. */
  538. filename_try[ MAXFILENAME ] = '\0';
  539. # ifdef USEWIN32IOAPI
  540. fill_win32_filefunc64A(&ffunc);
  541. uf = unzOpen2_64(zipfilename,&ffunc);
  542. # else
  543. uf = unzOpen64(zipfilename);
  544. # endif
  545. if (uf==NULL)
  546. {
  547. strcat(filename_try,".zip");
  548. # ifdef USEWIN32IOAPI
  549. uf = unzOpen2_64(filename_try,&ffunc);
  550. # else
  551. uf = unzOpen64(filename_try);
  552. # endif
  553. }
  554. }
  555. if (uf==NULL)
  556. {
  557. printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
  558. return 1;
  559. }
  560. printf("%s opened\n",filename_try);
  561. if (opt_do_list==1)
  562. ret_value = do_list(uf);
  563. else if (opt_do_extract==1)
  564. {
  565. #ifdef _WIN32
  566. if (opt_extractdir && _chdir(dirname))
  567. #else
  568. if (opt_extractdir && chdir(dirname))
  569. #endif
  570. {
  571. printf("Error changing into %s, aborting\n", dirname);
  572. exit(-1);
  573. }
  574. if (filename_to_extract == NULL)
  575. ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
  576. else
  577. ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
  578. }
  579. unzClose(uf);
  580. return ret_value;
  581. }