You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

508 lines
11 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #ifdef unix
  8. # include <unistd.h>
  9. # include <utime.h>
  10. #else
  11. # include <direct.h>
  12. # include <io.h>
  13. #endif
  14. #include "unzip.h"
  15. #define CASESENSITIVITY (0)
  16. #define WRITEBUFFERSIZE (8192)
  17. /*
  18. mini unzip, demo of unzip package
  19. usage :
  20. Usage : miniunz [-exvlo] file.zip [file_to_extract]
  21. list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
  22. if it exists
  23. */
  24. /* change_file_date : change the date/time of a file
  25. filename : the filename of the file where date/time must be modified
  26. dosdate : the new date at the MSDos format (4 bytes)
  27. tmu_date : the SAME new date at the tm_unz format */
  28. void change_file_date(filename,dosdate,tmu_date)
  29. const char *filename;
  30. uLong dosdate;
  31. tm_unz tmu_date;
  32. {
  33. #ifdef WIN32
  34. HANDLE hFile;
  35. FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
  36. hFile = CreateFile(filename,GENERIC_READ | GENERIC_WRITE,
  37. 0,NULL,OPEN_EXISTING,0,NULL);
  38. GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
  39. DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
  40. LocalFileTimeToFileTime(&ftLocal,&ftm);
  41. SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
  42. CloseHandle(hFile);
  43. #else
  44. #ifdef unix
  45. struct utimbuf ut;
  46. struct tm newdate;
  47. newdate.tm_sec = tmu_date.tm_sec;
  48. newdate.tm_min=tmu_date.tm_min;
  49. newdate.tm_hour=tmu_date.tm_hour;
  50. newdate.tm_mday=tmu_date.tm_mday;
  51. newdate.tm_mon=tmu_date.tm_mon;
  52. if (tmu_date.tm_year > 1900)
  53. newdate.tm_year=tmu_date.tm_year - 1900;
  54. else
  55. newdate.tm_year=tmu_date.tm_year ;
  56. newdate.tm_isdst=-1;
  57. ut.actime=ut.modtime=mktime(&newdate);
  58. utime(filename,&ut);
  59. #endif
  60. #endif
  61. }
  62. /* mymkdir and change_file_date are not 100 % portable
  63. As I don't know well Unix, I wait feedback for the unix portion */
  64. int mymkdir(dirname)
  65. const char* dirname;
  66. {
  67. int ret=0;
  68. #ifdef WIN32
  69. ret = mkdir(dirname);
  70. #else
  71. #ifdef unix
  72. ret = mkdir (dirname,0775);
  73. #endif
  74. #endif
  75. return ret;
  76. }
  77. int makedir (newdir)
  78. char *newdir;
  79. {
  80. char *buffer ;
  81. char *p;
  82. int len = strlen(newdir);
  83. if (len <= 0)
  84. return 0;
  85. buffer = (char*)malloc(len+1);
  86. strcpy(buffer,newdir);
  87. if (buffer[len-1] == '/') {
  88. buffer[len-1] = '\0';
  89. }
  90. if (mymkdir(buffer) == 0)
  91. {
  92. free(buffer);
  93. return 1;
  94. }
  95. p = buffer+1;
  96. while (1)
  97. {
  98. char hold;
  99. while(*p && *p != '\\' && *p != '/')
  100. p++;
  101. hold = *p;
  102. *p = 0;
  103. if ((mymkdir(buffer) == -1) && (errno == ENOENT))
  104. {
  105. printf("couldn't create directory %s\n",buffer);
  106. free(buffer);
  107. return 0;
  108. }
  109. if (hold == 0)
  110. break;
  111. *p++ = hold;
  112. }
  113. free(buffer);
  114. return 1;
  115. }
  116. void do_banner()
  117. {
  118. printf("MiniUnz 0.15, demo of zLib + Unz package written by Gilles Vollant\n");
  119. printf("more info at http://wwww.winimage/zLibDll/unzip.htm\n\n");
  120. }
  121. void do_help()
  122. {
  123. printf("Usage : miniunz [-exvlo] file.zip [file_to_extract]\n\n") ;
  124. }
  125. int do_list(uf)
  126. unzFile uf;
  127. {
  128. uLong i;
  129. unz_global_info gi;
  130. int err;
  131. err = unzGetGlobalInfo (uf,&gi);
  132. if (err!=UNZ_OK)
  133. printf("error %d with zipfile in unzGetGlobalInfo \n",err);
  134. printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
  135. printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
  136. for (i=0;i<gi.number_entry;i++)
  137. {
  138. char filename_inzip[256];
  139. unz_file_info file_info;
  140. uLong ratio=0;
  141. const char *string_method;
  142. err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
  143. if (err!=UNZ_OK)
  144. {
  145. printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
  146. break;
  147. }
  148. if (file_info.uncompressed_size>0)
  149. ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
  150. if (file_info.compression_method==0)
  151. string_method="Stored";
  152. else
  153. if (file_info.compression_method==Z_DEFLATED)
  154. {
  155. uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
  156. if (iLevel==0)
  157. string_method="Defl:N";
  158. else if (iLevel==1)
  159. string_method="Defl:X";
  160. else if ((iLevel==2) || (iLevel==3))
  161. string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
  162. }
  163. else
  164. string_method="Unkn. ";
  165. printf("%7lu %6s %7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
  166. file_info.uncompressed_size,string_method,file_info.compressed_size,
  167. ratio,
  168. (uLong)file_info.tmu_date.tm_mon + 1,
  169. (uLong)file_info.tmu_date.tm_mday,
  170. (uLong)file_info.tmu_date.tm_year % 100,
  171. (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
  172. (uLong)file_info.crc,filename_inzip);
  173. if ((i+1)<gi.number_entry)
  174. {
  175. err = unzGoToNextFile(uf);
  176. if (err!=UNZ_OK)
  177. {
  178. printf("error %d with zipfile in unzGoToNextFile\n",err);
  179. break;
  180. }
  181. }
  182. }
  183. return 0;
  184. }
  185. int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite)
  186. unzFile uf;
  187. const int* popt_extract_without_path;
  188. int* popt_overwrite;
  189. {
  190. char filename_inzip[256];
  191. char* filename_withoutpath;
  192. char* p;
  193. int err=UNZ_OK;
  194. FILE *fout=NULL;
  195. void* buf;
  196. uInt size_buf;
  197. unz_file_info file_info;
  198. uLong ratio=0;
  199. err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
  200. if (err!=UNZ_OK)
  201. {
  202. printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
  203. return err;
  204. }
  205. size_buf = WRITEBUFFERSIZE;
  206. buf = (void*)malloc(size_buf);
  207. if (buf==NULL)
  208. {
  209. printf("Error allocating memory\n");
  210. return UNZ_INTERNALERROR;
  211. }
  212. p = filename_withoutpath = filename_inzip;
  213. while ((*p) != '\0')
  214. {
  215. if (((*p)=='/') || ((*p)=='\\'))
  216. filename_withoutpath = p+1;
  217. p++;
  218. }
  219. if ((*filename_withoutpath)=='\0')
  220. {
  221. if ((*popt_extract_without_path)==0)
  222. {
  223. printf("creating directory: %s\n",filename_inzip);
  224. mymkdir(filename_inzip);
  225. }
  226. }
  227. else
  228. {
  229. const char* write_filename;
  230. int skip=0;
  231. if ((*popt_extract_without_path)==0)
  232. write_filename = filename_inzip;
  233. else
  234. write_filename = filename_withoutpath;
  235. err = unzOpenCurrentFile(uf);
  236. if (err!=UNZ_OK)
  237. {
  238. printf("error %d with zipfile in unzOpenCurrentFile\n",err);
  239. }
  240. if (((*popt_overwrite)==0) && (err==UNZ_OK))
  241. {
  242. char rep;
  243. FILE* ftestexist;
  244. ftestexist = fopen(write_filename,"rb");
  245. if (ftestexist!=NULL)
  246. {
  247. fclose(ftestexist);
  248. do
  249. {
  250. char answer[128];
  251. printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
  252. scanf("%1s",answer);
  253. rep = answer[0] ;
  254. if ((rep>='a') && (rep<='z'))
  255. rep -= 0x20;
  256. }
  257. while ((rep!='Y') && (rep!='N') && (rep!='A'));
  258. }
  259. if (rep == 'N')
  260. skip = 1;
  261. if (rep == 'A')
  262. *popt_overwrite=1;
  263. }
  264. if ((skip==0) && (err==UNZ_OK))
  265. {
  266. fout=fopen(write_filename,"wb");
  267. /* some zipfile don't contain directory alone before file */
  268. if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
  269. (filename_withoutpath!=(char*)filename_inzip))
  270. {
  271. char c=*(filename_withoutpath-1);
  272. *(filename_withoutpath-1)='\0';
  273. makedir(write_filename);
  274. *(filename_withoutpath-1)=c;
  275. fout=fopen(write_filename,"wb");
  276. }
  277. if (fout==NULL)
  278. {
  279. printf("error opening %s\n",write_filename);
  280. }
  281. }
  282. if (fout!=NULL)
  283. {
  284. printf(" extracting: %s\n",write_filename);
  285. do
  286. {
  287. err = unzReadCurrentFile(uf,buf,size_buf);
  288. if (err<0)
  289. {
  290. printf("error %d with zipfile in unzReadCurrentFile\n",err);
  291. break;
  292. }
  293. if (err>0)
  294. if (fwrite(buf,err,1,fout)!=1)
  295. {
  296. printf("error in writing extracted file\n");
  297. err=UNZ_ERRNO;
  298. break;
  299. }
  300. }
  301. while (err>0);
  302. fclose(fout);
  303. if (err==0)
  304. change_file_date(write_filename,file_info.dosDate,
  305. file_info.tmu_date);
  306. }
  307. if (err==UNZ_OK)
  308. {
  309. err = unzCloseCurrentFile (uf);
  310. if (err!=UNZ_OK)
  311. {
  312. printf("error %d with zipfile in unzCloseCurrentFile\n",err);
  313. }
  314. }
  315. else
  316. unzCloseCurrentFile(uf); /* don't lose the error */
  317. }
  318. free(buf);
  319. return err;
  320. }
  321. int do_extract(uf,opt_extract_without_path,opt_overwrite)
  322. unzFile uf;
  323. int opt_extract_without_path;
  324. int opt_overwrite;
  325. {
  326. uLong i;
  327. unz_global_info gi;
  328. int err;
  329. FILE* fout=NULL;
  330. err = unzGetGlobalInfo (uf,&gi);
  331. if (err!=UNZ_OK)
  332. printf("error %d with zipfile in unzGetGlobalInfo \n",err);
  333. for (i=0;i<gi.number_entry;i++)
  334. {
  335. if (do_extract_currentfile(uf,&opt_extract_without_path,
  336. &opt_overwrite) != UNZ_OK)
  337. break;
  338. if ((i+1)<gi.number_entry)
  339. {
  340. err = unzGoToNextFile(uf);
  341. if (err!=UNZ_OK)
  342. {
  343. printf("error %d with zipfile in unzGoToNextFile\n",err);
  344. break;
  345. }
  346. }
  347. }
  348. return 0;
  349. }
  350. int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite)
  351. unzFile uf;
  352. const char* filename;
  353. int opt_extract_without_path;
  354. int opt_overwrite;
  355. {
  356. int err = UNZ_OK;
  357. if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
  358. {
  359. printf("file %s not found in the zipfile\n",filename);
  360. return 2;
  361. }
  362. if (do_extract_currentfile(uf,&opt_extract_without_path,
  363. &opt_overwrite) == UNZ_OK)
  364. return 0;
  365. else
  366. return 1;
  367. }
  368. int main(argc,argv)
  369. int argc;
  370. char *argv[];
  371. {
  372. const char *zipfilename=NULL;
  373. const char *filename_to_extract=NULL;
  374. int i;
  375. int opt_do_list=0;
  376. int opt_do_extract=1;
  377. int opt_do_extract_withoutpath=0;
  378. int opt_overwrite=0;
  379. char filename_try[512];
  380. unzFile uf=NULL;
  381. do_banner();
  382. if (argc==1)
  383. {
  384. do_help();
  385. exit(0);
  386. }
  387. else
  388. {
  389. for (i=1;i<argc;i++)
  390. {
  391. if ((*argv[i])=='-')
  392. {
  393. const char *p=argv[i]+1;
  394. while ((*p)!='\0')
  395. {
  396. char c=*(p++);;
  397. if ((c=='l') || (c=='L'))
  398. opt_do_list = 1;
  399. if ((c=='v') || (c=='V'))
  400. opt_do_list = 1;
  401. if ((c=='x') || (c=='X'))
  402. opt_do_extract = 1;
  403. if ((c=='e') || (c=='E'))
  404. opt_do_extract = opt_do_extract_withoutpath = 1;
  405. if ((c=='o') || (c=='O'))
  406. opt_overwrite=1;
  407. }
  408. }
  409. else
  410. {
  411. if (zipfilename == NULL)
  412. zipfilename = argv[i];
  413. else if (filename_to_extract==NULL)
  414. filename_to_extract = argv[i] ;
  415. }
  416. }
  417. }
  418. if (zipfilename!=NULL)
  419. {
  420. strcpy(filename_try,zipfilename);
  421. uf = unzOpen(zipfilename);
  422. if (uf==NULL)
  423. {
  424. strcat(filename_try,".zip");
  425. uf = unzOpen(filename_try);
  426. }
  427. }
  428. if (uf==NULL)
  429. {
  430. printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
  431. exit (1);
  432. }
  433. printf("%s opened\n",filename_try);
  434. if (opt_do_list==1)
  435. return do_list(uf);
  436. else if (opt_do_extract==1)
  437. {
  438. if (filename_to_extract == NULL)
  439. return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite);
  440. else
  441. return do_extract_onefile(uf,filename_to_extract,
  442. opt_do_extract_withoutpath,opt_overwrite);
  443. }
  444. unzCloseCurrentFile(uf);
  445. return 0; /* to avoid warning */
  446. }