minizip.c
上传用户:zlt_tm
上传日期:2007-01-06
资源大小:214k
文件大小:8k
源码类别:

压缩解压

开发平台:

WINDOWS

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