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.

302 lines
7.4 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. # include <sys/types.h>
  11. # include <sys/stat.h>
  12. #else
  13. # include <direct.h>
  14. # include <io.h>
  15. #endif
  16. #include "zip.h"
  17. #define WRITEBUFFERSIZE (16384)
  18. #define MAXFILENAME (256)
  19. #ifdef WIN32
  20. uLong filetime(f, tmzip, dt)
  21. char *f; /* name of file to get info on */
  22. tm_zip *tmzip; /* return value: access, modific. and creation times */
  23. uLong *dt; /* dostime */
  24. {
  25. int ret = 0;
  26. {
  27. FILETIME ftLocal;
  28. HANDLE hFind;
  29. WIN32_FIND_DATA ff32;
  30. hFind = FindFirstFile(f,&ff32);
  31. if (hFind != INVALID_HANDLE_VALUE)
  32. {
  33. FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
  34. FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
  35. FindClose(hFind);
  36. ret = 1;
  37. }
  38. }
  39. return ret;
  40. }
  41. #else
  42. #ifdef unix
  43. uLong filetime(f, tmzip, dt)
  44. char *f; /* name of file to get info on */
  45. tm_zip *tmzip; /* return value: access, modific. and creation times */
  46. uLong *dt; /* dostime */
  47. {
  48. int ret=0;
  49. struct stat s; /* results of stat() */
  50. struct tm* filedate;
  51. time_t tm_t=0;
  52. if (strcmp(f,"-")!=0)
  53. {
  54. char name[MAXFILENAME];
  55. int len = strlen(f);
  56. strcpy(name, f);
  57. if (name[len - 1] == '/')
  58. name[len - 1] = '\0';
  59. /* not all systems allow stat'ing a file with / appended */
  60. if (stat(name,&s)==0)
  61. {
  62. tm_t = s.st_mtime;
  63. ret = 1;
  64. }
  65. }
  66. filedate = localtime(&tm_t);
  67. tmzip->tm_sec = filedate->tm_sec;
  68. tmzip->tm_min = filedate->tm_min;
  69. tmzip->tm_hour = filedate->tm_hour;
  70. tmzip->tm_mday = filedate->tm_mday;
  71. tmzip->tm_mon = filedate->tm_mon ;
  72. tmzip->tm_year = filedate->tm_year;
  73. return ret;
  74. }
  75. #else
  76. uLong filetime(f, tmzip, dt)
  77. char *f; /* name of file to get info on */
  78. tm_zip *tmzip; /* return value: access, modific. and creation times */
  79. uLong *dt; /* dostime */
  80. {
  81. return 0;
  82. }
  83. #endif
  84. #endif
  85. int check_exist_file(filename)
  86. const char* filename;
  87. {
  88. FILE* ftestexist;
  89. int ret = 1;
  90. ftestexist = fopen(filename,"rb");
  91. if (ftestexist==NULL)
  92. ret = 0;
  93. else
  94. fclose(ftestexist);
  95. return ret;
  96. }
  97. void do_banner()
  98. {
  99. printf("MiniZip 0.15, demo of zLib + Zip package written by Gilles Vollant\n");
  100. printf("more info at http://wwww.winimage/zLibDll/unzip.htm\n\n");
  101. }
  102. void do_help()
  103. {
  104. printf("Usage : minizip [-o] file.zip [files_to_add]\n\n") ;
  105. }
  106. int main(argc,argv)
  107. int argc;
  108. char *argv[];
  109. {
  110. int i;
  111. int opt_overwrite=0;
  112. int opt_compress_level=Z_DEFAULT_COMPRESSION;
  113. int zipfilenamearg = 0;
  114. char filename_try[MAXFILENAME];
  115. int zipok;
  116. int err=0;
  117. int size_buf=0;
  118. void* buf=NULL,
  119. do_banner();
  120. if (argc==1)
  121. {
  122. do_help();
  123. exit(0);
  124. return 0;
  125. }
  126. else
  127. {
  128. for (i=1;i<argc;i++)
  129. {
  130. if ((*argv[i])=='-')
  131. {
  132. const char *p=argv[i]+1;
  133. while ((*p)!='\0')
  134. {
  135. char c=*(p++);;
  136. if ((c=='o') || (c=='O'))
  137. opt_overwrite = 1;
  138. if ((c>='0') && (c<='9'))
  139. opt_compress_level = c-'0';
  140. }
  141. }
  142. else
  143. if (zipfilenamearg == 0)
  144. zipfilenamearg = i ;
  145. }
  146. }
  147. size_buf = WRITEBUFFERSIZE;
  148. buf = (void*)malloc(size_buf);
  149. if (buf==NULL)
  150. {
  151. printf("Error allocating memory\n");
  152. return ZIP_INTERNALERROR;
  153. }
  154. if (zipfilenamearg==0)
  155. zipok=0;
  156. else
  157. {
  158. int i,len;
  159. int dot_found=0;
  160. zipok = 1 ;
  161. strcpy(filename_try,argv[zipfilenamearg]);
  162. len=strlen(filename_try);
  163. for (i=0;i<len;i++)
  164. if (filename_try[i]=='.')
  165. dot_found=1;
  166. if (dot_found==0)
  167. strcat(filename_try,".zip");
  168. if (opt_overwrite==0)
  169. if (check_exist_file(filename_try)!=0)
  170. {
  171. char rep;
  172. do
  173. {
  174. char answer[128];
  175. printf("The file %s exist. Overwrite ? [y]es, [n]o : ",filename_try);
  176. scanf("%1s",answer);
  177. rep = answer[0] ;
  178. if ((rep>='a') && (rep<='z'))
  179. rep -= 0x20;
  180. }
  181. while ((rep!='Y') && (rep!='N'));
  182. if (rep=='N')
  183. zipok = 0;
  184. }
  185. }
  186. if (zipok==1)
  187. {
  188. zipFile zf;
  189. int errclose;
  190. zf = zipOpen(filename_try,0);
  191. if (zf == NULL)
  192. {
  193. printf("error opening %s\n",filename_try);
  194. err= ZIP_ERRNO;
  195. }
  196. else
  197. printf("creating %s\n",filename_try);
  198. for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
  199. {
  200. if (((*(argv[i]))!='-') && ((*(argv[i]))!='/'))
  201. {
  202. FILE * fin;
  203. int size_read;
  204. const char* filenameinzip = argv[i];
  205. zip_fileinfo zi;
  206. zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  207. zi.tmz_date.tm_mday = zi.tmz_date.tm_min = zi.tmz_date.tm_year = 0;
  208. zi.dosDate = 0;
  209. zi.internal_fa = 0;
  210. zi.external_fa = 0;
  211. filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
  212. err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
  213. NULL,0,NULL,0,NULL /* comment*/,
  214. (opt_compress_level != 0) ? Z_DEFLATED : 0,
  215. opt_compress_level);
  216. if (err != ZIP_OK)
  217. printf("error in opening %s in zipfile\n",filenameinzip);
  218. else
  219. {
  220. fin = fopen(filenameinzip,"rb");
  221. if (fin==NULL)
  222. {
  223. err=ZIP_ERRNO;
  224. printf("error in opening %s for reading\n",filenameinzip);
  225. }
  226. }
  227. if (err == ZIP_OK)
  228. do
  229. {
  230. err = ZIP_OK;
  231. size_read = fread(buf,1,size_buf,fin);
  232. if (size_read < size_buf)
  233. if (feof(fin)==0)
  234. {
  235. printf("error in reading %s\n",filenameinzip);
  236. err = ZIP_ERRNO;
  237. }
  238. if (size_read>0)
  239. {
  240. err = zipWriteInFileInZip (zf,buf,size_read);
  241. if (err<0)
  242. {
  243. printf("error in writing %s in the zipfile\n",
  244. filenameinzip);
  245. }
  246. }
  247. } while ((err == ZIP_OK) && (size_read>0));
  248. fclose(fin);
  249. if (err<0)
  250. err=ZIP_ERRNO;
  251. else
  252. {
  253. err = zipCloseFileInZip(zf);
  254. if (err!=ZIP_OK)
  255. printf("error in closing %s in the zipfile\n",
  256. filenameinzip);
  257. }
  258. }
  259. }
  260. errclose = zipClose(zf,NULL);
  261. if (errclose != ZIP_OK)
  262. printf("error in closing %s\n",filename_try);
  263. }
  264. free(buf);
  265. exit(0);
  266. return 0; /* to avoid warning */
  267. }