miniunz.c 17 KB

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