zip.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:36k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* zip.c -- IO on .zip files using zlib
  2.    Version 1.01e, February 12th, 2005
  3.    27 Dec 2004 Rolf Kalbermatter
  4.    Modification to zipOpen2 to support globalComment retrieval.
  5.    Copyright (C) 1998-2005 Gilles Vollant
  6.    Read zip.h for more info
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include "zlib.h"
  13. #include "zip.h"
  14. #ifdef STDC
  15. #  include <stddef.h>
  16. #  include <string.h>
  17. #  include <stdlib.h>
  18. #endif
  19. #ifdef NO_ERRNO_H
  20.     extern int errno;
  21. #else
  22. #   include <errno.h>
  23. #endif
  24. #ifndef local
  25. #  define local static
  26. #endif
  27. /* compile with -Dlocal if your debugger can't find static symbols */
  28. #ifndef VERSIONMADEBY
  29. # define VERSIONMADEBY   (0x0) /* platform depedent */
  30. #endif
  31. #ifndef Z_BUFSIZE
  32. #define Z_BUFSIZE (16384)
  33. #endif
  34. #ifndef Z_MAXFILENAMEINZIP
  35. #define Z_MAXFILENAMEINZIP (256)
  36. #endif
  37. #ifndef ALLOC
  38. # define ALLOC(size) (malloc(size))
  39. #endif
  40. #ifndef TRYFREE
  41. # define TRYFREE(p) {if (p) free(p);}
  42. #endif
  43. /*
  44. #define SIZECENTRALDIRITEM (0x2e)
  45. #define SIZEZIPLOCALHEADER (0x1e)
  46. */
  47. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  48. #ifndef SEEK_CUR
  49. #define SEEK_CUR    1
  50. #endif
  51. #ifndef SEEK_END
  52. #define SEEK_END    2
  53. #endif
  54. #ifndef SEEK_SET
  55. #define SEEK_SET    0
  56. #endif
  57. #ifndef DEF_MEM_LEVEL
  58. #if MAX_MEM_LEVEL >= 8
  59. #  define DEF_MEM_LEVEL 8
  60. #else
  61. #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
  62. #endif
  63. #endif
  64. const char zip_copyright[] =
  65.    " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
  66. #define SIZEDATA_INDATABLOCK (4096-(4*4))
  67. #define LOCALHEADERMAGIC    (0x04034b50)
  68. #define CENTRALHEADERMAGIC  (0x02014b50)
  69. #define ENDHEADERMAGIC      (0x06054b50)
  70. #define FLAG_LOCALHEADER_OFFSET (0x06)
  71. #define CRC_LOCALHEADER_OFFSET  (0x0e)
  72. #define SIZECENTRALHEADER (0x2e) /* 46 */
  73. typedef struct linkedlist_datablock_internal_s
  74. {
  75.   struct linkedlist_datablock_internal_s* next_datablock;
  76.   uLong  avail_in_this_block;
  77.   uLong  filled_in_this_block;
  78.   uLong  unused; /* for future use and alignement */
  79.   unsigned char data[SIZEDATA_INDATABLOCK];
  80. } linkedlist_datablock_internal;
  81. typedef struct linkedlist_data_s
  82. {
  83.     linkedlist_datablock_internal* first_block;
  84.     linkedlist_datablock_internal* last_block;
  85. } linkedlist_data;
  86. typedef struct
  87. {
  88.     z_stream stream;            /* zLib stream structure for inflate */
  89.     int  stream_initialised;    /* 1 is stream is initialised */
  90.     uInt pos_in_buffered_data;  /* last written byte in buffered_data */
  91.     uLong pos_local_header;     /* offset of the local header of the file
  92.                                      currenty writing */
  93.     char* central_header;       /* central header data for the current file */
  94.     uLong size_centralheader;   /* size of the central header for cur file */
  95.     uLong flag;                 /* flag of the file currently writing */
  96.     int  method;                /* compression method of file currenty wr.*/
  97.     int  raw;                   /* 1 for directly writing raw data */
  98.     Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
  99.     uLong dosDate;
  100.     uLong crc32;
  101.     int  encrypt;
  102. #ifndef NOCRYPT
  103.     unsigned long keys[3];     /* keys defining the pseudo-random sequence */
  104.     const unsigned long* pcrc_32_tab;
  105.     int crypt_header_size;
  106. #endif
  107. } curfile_info;
  108. typedef struct
  109. {
  110.     zlib_filefunc_def z_filefunc;
  111.     voidpf filestream;        /* io structore of the zipfile */
  112.     linkedlist_data central_dir;/* datablock with central dir in construction*/
  113.     int  in_opened_file_inzip;  /* 1 if a file in the zip is currently writ.*/
  114.     curfile_info ci;            /* info on the file curretly writing */
  115.     uLong begin_pos;            /* position of the beginning of the zipfile */
  116.     uLong add_position_when_writting_offset;
  117.     uLong number_entry;
  118. #ifndef NO_ADDFILEINEXISTINGZIP
  119.     char *globalcomment;
  120. #endif
  121. } zip_internal;
  122. #ifndef NOCRYPT
  123. #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
  124. #include "crypt.h"
  125. #endif
  126. local linkedlist_datablock_internal* allocate_new_datablock()
  127. {
  128.     linkedlist_datablock_internal* ldi;
  129.     ldi = (linkedlist_datablock_internal*)
  130.                  ALLOC(sizeof(linkedlist_datablock_internal));
  131.     if (ldi!=NULL)
  132.     {
  133.         ldi->next_datablock = NULL ;
  134.         ldi->filled_in_this_block = 0 ;
  135.         ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
  136.     }
  137.     return ldi;
  138. }
  139. local void free_datablock(ldi)
  140.     linkedlist_datablock_internal* ldi;
  141. {
  142.     while (ldi!=NULL)
  143.     {
  144.         linkedlist_datablock_internal* ldinext = ldi->next_datablock;
  145.         TRYFREE(ldi);
  146.         ldi = ldinext;
  147.     }
  148. }
  149. local void init_linkedlist(ll)
  150.     linkedlist_data* ll;
  151. {
  152.     ll->first_block = ll->last_block = NULL;
  153. }
  154. local void free_linkedlist(ll)
  155.     linkedlist_data* ll;
  156. {
  157.     free_datablock(ll->first_block);
  158.     ll->first_block = ll->last_block = NULL;
  159. }
  160. local int add_data_in_datablock(ll,buf,len)
  161.     linkedlist_data* ll;
  162.     const void* buf;
  163.     uLong len;
  164. {
  165.     linkedlist_datablock_internal* ldi;
  166.     const unsigned char* from_copy;
  167.     if (ll==NULL)
  168.         return ZIP_INTERNALERROR;
  169.     if (ll->last_block == NULL)
  170.     {
  171.         ll->first_block = ll->last_block = allocate_new_datablock();
  172.         if (ll->first_block == NULL)
  173.             return ZIP_INTERNALERROR;
  174.     }
  175.     ldi = ll->last_block;
  176.     from_copy = (unsigned char*)buf;
  177.     while (len>0)
  178.     {
  179.         uInt copy_this;
  180.         uInt i;
  181.         unsigned char* to_copy;
  182.         if (ldi->avail_in_this_block==0)
  183.         {
  184.             ldi->next_datablock = allocate_new_datablock();
  185.             if (ldi->next_datablock == NULL)
  186.                 return ZIP_INTERNALERROR;
  187.             ldi = ldi->next_datablock ;
  188.             ll->last_block = ldi;
  189.         }
  190.         if (ldi->avail_in_this_block < len)
  191.             copy_this = (uInt)ldi->avail_in_this_block;
  192.         else
  193.             copy_this = (uInt)len;
  194.         to_copy = &(ldi->data[ldi->filled_in_this_block]);
  195.         for (i=0;i<copy_this;i++)
  196.             *(to_copy+i)=*(from_copy+i);
  197.         ldi->filled_in_this_block += copy_this;
  198.         ldi->avail_in_this_block -= copy_this;
  199.         from_copy += copy_this ;
  200.         len -= copy_this;
  201.     }
  202.     return ZIP_OK;
  203. }
  204. /****************************************************************************/
  205. #ifndef NO_ADDFILEINEXISTINGZIP
  206. /* ===========================================================================
  207.    Inputs a long in LSB order to the given file
  208.    nbByte == 1, 2 or 4 (byte, short or long)
  209. */
  210. local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
  211.                                 voidpf filestream, uLong x, int nbByte));
  212. local int ziplocal_putValue (pzlib_filefunc_def, filestream, x, nbByte)
  213.     const zlib_filefunc_def* pzlib_filefunc_def;
  214.     voidpf filestream;
  215.     uLong x;
  216.     int nbByte;
  217. {
  218.     unsigned char buf[4];
  219.     int n;
  220.     for (n = 0; n < nbByte; n++)
  221.     {
  222.         buf[n] = (unsigned char)(x & 0xff);
  223.         x >>= 8;
  224.     }
  225.     if (x != 0)
  226.       {     /* data overflow - hack for ZIP64 (X Roche) */
  227.       for (n = 0; n < nbByte; n++)
  228.         {
  229.           buf[n] = 0xff;
  230.         }
  231.       }
  232.     if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
  233.         return ZIP_ERRNO;
  234.     else
  235.         return ZIP_OK;
  236. }
  237. local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
  238. local void ziplocal_putValue_inmemory (dest, x, nbByte)
  239.     void* dest;
  240.     uLong x;
  241.     int nbByte;
  242. {
  243.     unsigned char* buf=(unsigned char*)dest;
  244.     int n;
  245.     for (n = 0; n < nbByte; n++) {
  246.         buf[n] = (unsigned char)(x & 0xff);
  247.         x >>= 8;
  248.     }
  249.     if (x != 0)
  250.     {     /* data overflow - hack for ZIP64 */
  251.        for (n = 0; n < nbByte; n++)
  252.        {
  253.           buf[n] = 0xff;
  254.        }
  255.     }
  256. }
  257. /****************************************************************************/
  258. local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
  259.     const tm_zip* ptm;
  260.     uLong dosDate;
  261. {
  262.     uLong year = (uLong)ptm->tm_year;
  263.     if (year>1980)
  264.         year-=1980;
  265.     else if (year>80)
  266.         year-=80;
  267.     return
  268.       (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
  269.         ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
  270. }
  271. /****************************************************************************/
  272. local int ziplocal_getByte OF((
  273.     const zlib_filefunc_def* pzlib_filefunc_def,
  274.     voidpf filestream,
  275.     int *pi));
  276. local int ziplocal_getByte(pzlib_filefunc_def,filestream,pi)
  277.     const zlib_filefunc_def* pzlib_filefunc_def;
  278.     voidpf filestream;
  279.     int *pi;
  280. {
  281.     unsigned char c;
  282.     int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
  283.     if (err==1)
  284.     {
  285.         *pi = (int)c;
  286.         return ZIP_OK;
  287.     }
  288.     else
  289.     {
  290.         if (ZERROR(*pzlib_filefunc_def,filestream))
  291.             return ZIP_ERRNO;
  292.         else
  293.             return ZIP_EOF;
  294.     }
  295. }
  296. /* ===========================================================================
  297.    Reads a long in LSB order from the given gz_stream. Sets
  298. */
  299. local int ziplocal_getShort OF((
  300.     const zlib_filefunc_def* pzlib_filefunc_def,
  301.     voidpf filestream,
  302.     uLong *pX));
  303. local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX)
  304.     const zlib_filefunc_def* pzlib_filefunc_def;
  305.     voidpf filestream;
  306.     uLong *pX;
  307. {
  308.     uLong x ;
  309.     int i;
  310.     int err;
  311.     err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
  312.     x = (uLong)i;
  313.     if (err==ZIP_OK)
  314.         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
  315.     x += ((uLong)i)<<8;
  316.     if (err==ZIP_OK)
  317.         *pX = x;
  318.     else
  319.         *pX = 0;
  320.     return err;
  321. }
  322. local int ziplocal_getLong OF((
  323.     const zlib_filefunc_def* pzlib_filefunc_def,
  324.     voidpf filestream,
  325.     uLong *pX));
  326. local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
  327.     const zlib_filefunc_def* pzlib_filefunc_def;
  328.     voidpf filestream;
  329.     uLong *pX;
  330. {
  331.     uLong x ;
  332.     int i;
  333.     int err;
  334.     err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
  335.     x = (uLong)i;
  336.     if (err==ZIP_OK)
  337.         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
  338.     x += ((uLong)i)<<8;
  339.     if (err==ZIP_OK)
  340.         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
  341.     x += ((uLong)i)<<16;
  342.     if (err==ZIP_OK)
  343.         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
  344.     x += ((uLong)i)<<24;
  345.     if (err==ZIP_OK)
  346.         *pX = x;
  347.     else
  348.         *pX = 0;
  349.     return err;
  350. }
  351. #ifndef BUFREADCOMMENT
  352. #define BUFREADCOMMENT (0x400)
  353. #endif
  354. /*
  355.   Locate the Central directory of a zipfile (at the end, just before
  356.     the global comment)
  357. */
  358. local uLong ziplocal_SearchCentralDir OF((
  359.     const zlib_filefunc_def* pzlib_filefunc_def,
  360.     voidpf filestream));
  361. local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream)
  362.     const zlib_filefunc_def* pzlib_filefunc_def;
  363.     voidpf filestream;
  364. {
  365.     unsigned char* buf;
  366.     uLong uSizeFile;
  367.     uLong uBackRead;
  368.     uLong uMaxBack=0xffff; /* maximum size of global comment */
  369.     uLong uPosFound=0;
  370.     if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
  371.         return 0;
  372.     uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
  373.     if (uMaxBack>uSizeFile)
  374.         uMaxBack = uSizeFile;
  375.     buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  376.     if (buf==NULL)
  377.         return 0;
  378.     uBackRead = 4;
  379.     while (uBackRead<uMaxBack)
  380.     {
  381.         uLong uReadSize,uReadPos ;
  382.         int i;
  383.         if (uBackRead+BUFREADCOMMENT>uMaxBack)
  384.             uBackRead = uMaxBack;
  385.         else
  386.             uBackRead+=BUFREADCOMMENT;
  387.         uReadPos = uSizeFile-uBackRead ;
  388.         uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  389.                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
  390.         if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  391.             break;
  392.         if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
  393.             break;
  394.         for (i=(int)uReadSize-3; (i--)>0;)
  395.             if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
  396.                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  397.             {
  398.                 uPosFound = uReadPos+i;
  399.                 break;
  400.             }
  401.         if (uPosFound!=0)
  402.             break;
  403.     }
  404.     TRYFREE(buf);
  405.     return uPosFound;
  406. }
  407. #endif /* !NO_ADDFILEINEXISTINGZIP*/
  408. /************************************************************/
  409. extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc_def)
  410.     const char *pathname;
  411.     int append;
  412.     zipcharpc* globalcomment;
  413.     zlib_filefunc_def* pzlib_filefunc_def;
  414. {
  415.     zip_internal ziinit;
  416.     zip_internal* zi;
  417.     int err=ZIP_OK;
  418.     if (pzlib_filefunc_def==NULL)
  419.         fill_fopen_filefunc(&ziinit.z_filefunc);
  420.     else
  421.         ziinit.z_filefunc = *pzlib_filefunc_def;
  422.     ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
  423.                  (ziinit.z_filefunc.opaque,
  424.                   pathname,
  425.                   (append == APPEND_STATUS_CREATE) ?
  426.                   (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
  427.                     (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
  428.     if (ziinit.filestream == NULL)
  429.         return NULL;
  430.     ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
  431.     ziinit.in_opened_file_inzip = 0;
  432.     ziinit.ci.stream_initialised = 0;
  433.     ziinit.number_entry = 0;
  434.     ziinit.add_position_when_writting_offset = 0;
  435.     init_linkedlist(&(ziinit.central_dir));
  436.     zi = (zip_internal*)ALLOC(sizeof(zip_internal));
  437.     if (zi==NULL)
  438.     {
  439.         ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
  440.         return NULL;
  441.     }
  442.     /* now we add file in a zipfile */
  443. #    ifndef NO_ADDFILEINEXISTINGZIP
  444.     ziinit.globalcomment = NULL;
  445.     if (append == APPEND_STATUS_ADDINZIP)
  446.     {
  447.         uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  448.         uLong size_central_dir;     /* size of the central directory  */
  449.         uLong offset_central_dir;   /* offset of start of central directory */
  450.         uLong central_pos,uL;
  451.         uLong number_disk;          /* number of the current dist, used for
  452.                                     spaning ZIP, unsupported, always 0*/
  453.         uLong number_disk_with_CD;  /* number the the disk with central dir, used
  454.                                     for spaning ZIP, unsupported, always 0*/
  455.         uLong number_entry;
  456.         uLong number_entry_CD;      /* total number of entries in
  457.                                     the central dir
  458.                                     (same than number_entry on nospan) */
  459.         uLong size_comment;
  460.         central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
  461.         if (central_pos==0)
  462.             err=ZIP_ERRNO;
  463.         if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
  464.                                         central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  465.             err=ZIP_ERRNO;
  466.         /* the signature, already checked */
  467.         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
  468.             err=ZIP_ERRNO;
  469.         /* number of this disk */
  470.         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
  471.             err=ZIP_ERRNO;
  472.         /* number of the disk with the start of the central directory */
  473.         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
  474.             err=ZIP_ERRNO;
  475.         /* total number of entries in the central dir on this disk */
  476.         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
  477.             err=ZIP_ERRNO;
  478.         /* total number of entries in the central dir */
  479.         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
  480.             err=ZIP_ERRNO;
  481.         if ((number_entry_CD!=number_entry) ||
  482.             (number_disk_with_CD!=0) ||
  483.             (number_disk!=0))
  484.             err=ZIP_BADZIPFILE;
  485.         /* size of the central directory */
  486.         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
  487.             err=ZIP_ERRNO;
  488.         /* offset of start of central directory with respect to the
  489.             starting disk number */
  490.         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
  491.             err=ZIP_ERRNO;
  492.         /* zipfile global comment length */
  493.         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
  494.             err=ZIP_ERRNO;
  495.         if ((central_pos<offset_central_dir+size_central_dir) &&
  496.             (err==ZIP_OK))
  497.             err=ZIP_BADZIPFILE;
  498.         if (err!=ZIP_OK)
  499.         {
  500.             ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
  501.             return NULL;
  502.         }
  503.         if (size_comment>0)
  504.         {
  505.             ziinit.globalcomment = ALLOC(size_comment+1);
  506.             if (ziinit.globalcomment)
  507.             {
  508.                size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
  509.                ziinit.globalcomment[size_comment]=0;
  510.             }
  511.         }
  512.         byte_before_the_zipfile = central_pos -
  513.                                 (offset_central_dir+size_central_dir);
  514.         ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
  515.         {
  516.             uLong size_central_dir_to_read = size_central_dir;
  517.             size_t buf_size = SIZEDATA_INDATABLOCK;
  518.             void* buf_read = (void*)ALLOC(buf_size);
  519.             if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
  520.                   offset_central_dir + byte_before_the_zipfile,
  521.                   ZLIB_FILEFUNC_SEEK_SET) != 0)
  522.                   err=ZIP_ERRNO;
  523.             while ((size_central_dir_to_read>0) && (err==ZIP_OK))
  524.             {
  525.                 uLong read_this = SIZEDATA_INDATABLOCK;
  526.                 if (read_this > size_central_dir_to_read)
  527.                     read_this = size_central_dir_to_read;
  528.                 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
  529.                     err=ZIP_ERRNO;
  530.                 if (err==ZIP_OK)
  531.                     err = add_data_in_datablock(&ziinit.central_dir,buf_read,
  532.                                                 (uLong)read_this);
  533.                 size_central_dir_to_read-=read_this;
  534.             }
  535.             TRYFREE(buf_read);
  536.         }
  537.         ziinit.begin_pos = byte_before_the_zipfile;
  538.         ziinit.number_entry = number_entry_CD;
  539.         if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
  540.                   offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
  541.             err=ZIP_ERRNO;
  542.     }
  543.     if (globalcomment)
  544.     {
  545.       *globalcomment = ziinit.globalcomment;
  546.     }
  547. #    endif /* !NO_ADDFILEINEXISTINGZIP*/
  548.     if (err != ZIP_OK)
  549.     {
  550. #    ifndef NO_ADDFILEINEXISTINGZIP
  551.         TRYFREE(ziinit.globalcomment);
  552. #    endif /* !NO_ADDFILEINEXISTINGZIP*/
  553.         TRYFREE(zi);
  554.         return NULL;
  555.     }
  556.     else
  557.     {
  558.         *zi = ziinit;
  559.         return (zipFile)zi;
  560.     }
  561. }
  562. extern zipFile ZEXPORT zipOpen (pathname, append)
  563.     const char *pathname;
  564.     int append;
  565. {
  566.     return zipOpen2(pathname,append,NULL,NULL);
  567. }
  568. extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
  569.                                          extrafield_local, size_extrafield_local,
  570.                                          extrafield_global, size_extrafield_global,
  571.                                          comment, method, level, raw,
  572.                                          windowBits, memLevel, strategy,
  573.                                          password, crcForCrypting)
  574.     zipFile file;
  575.     const char* filename;
  576.     const zip_fileinfo* zipfi;
  577.     const void* extrafield_local;
  578.     uInt size_extrafield_local;
  579.     const void* extrafield_global;
  580.     uInt size_extrafield_global;
  581.     const char* comment;
  582.     int method;
  583.     int level;
  584.     int raw;
  585.     int windowBits;
  586.     int memLevel;
  587.     int strategy;
  588.     const char* password;
  589.     uLong crcForCrypting;
  590. {
  591.     zip_internal* zi;
  592.     uInt size_filename;
  593.     uInt size_comment;
  594.     uInt i;
  595.     int err = ZIP_OK;
  596. #    ifdef NOCRYPT
  597.     if (password != NULL)
  598.         return ZIP_PARAMERROR;
  599. #    endif
  600.     if (file == NULL)
  601.         return ZIP_PARAMERROR;
  602.     if ((method!=0) && (method!=Z_DEFLATED))
  603.         return ZIP_PARAMERROR;
  604.     zi = (zip_internal*)file;
  605.     if (zi->in_opened_file_inzip == 1)
  606.     {
  607.         err = zipCloseFileInZip (file);
  608.         if (err != ZIP_OK)
  609.             return err;
  610.     }
  611.     if (filename==NULL)
  612.         filename="-";
  613.     if (comment==NULL)
  614.         size_comment = 0;
  615.     else
  616.         size_comment = (uInt)strlen(comment);
  617.     size_filename = (uInt)strlen(filename);
  618.     if (zipfi == NULL)
  619.         zi->ci.dosDate = 0;
  620.     else
  621.     {
  622.         if (zipfi->dosDate != 0)
  623.             zi->ci.dosDate = zipfi->dosDate;
  624.         else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
  625.     }
  626.     zi->ci.flag = 0;
  627.     if ((level==8) || (level==9))
  628.       zi->ci.flag |= 2;
  629.     if ((level==2))
  630.       zi->ci.flag |= 4;
  631.     if ((level==1))
  632.       zi->ci.flag |= 6;
  633.     if (password != NULL)
  634.       zi->ci.flag |= 1;
  635.     zi->ci.crc32 = 0;
  636.     zi->ci.method = method;
  637.     zi->ci.encrypt = 0;
  638.     zi->ci.stream_initialised = 0;
  639.     zi->ci.pos_in_buffered_data = 0;
  640.     zi->ci.raw = raw;
  641.     zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
  642.     zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
  643.                                       size_extrafield_global + size_comment;
  644.     zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
  645.     ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
  646.     /* version info */
  647.     ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
  648.     ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
  649.     ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
  650.     ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
  651.     ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
  652.     ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
  653.     ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
  654.     ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
  655.     ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
  656.     ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
  657.     ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
  658.     ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
  659.     if (zipfi==NULL)
  660.         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
  661.     else
  662.         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
  663.     if (zipfi==NULL)
  664.         ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
  665.     else
  666.         ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
  667.     ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
  668.     for (i=0;i<size_filename;i++)
  669.         *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
  670.     for (i=0;i<size_extrafield_global;i++)
  671.         *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
  672.               *(((const char*)extrafield_global)+i);
  673.     for (i=0;i<size_comment;i++)
  674.         *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
  675.               size_extrafield_global+i) = *(comment+i);
  676.     if (zi->ci.central_header == NULL)
  677.         return ZIP_INTERNALERROR;
  678.     /* write the local header */
  679.     err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
  680.     if (err==ZIP_OK)
  681.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
  682.     if (err==ZIP_OK)
  683.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
  684.     if (err==ZIP_OK)
  685.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
  686.     if (err==ZIP_OK)
  687.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
  688.     if (err==ZIP_OK)
  689.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
  690.     if (err==ZIP_OK)
  691.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
  692.     if (err==ZIP_OK)
  693.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
  694.     if (err==ZIP_OK)
  695.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
  696.     if (err==ZIP_OK)
  697.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
  698.     if ((err==ZIP_OK) && (size_filename>0))
  699.         if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
  700.                 err = ZIP_ERRNO;
  701.     if ((err==ZIP_OK) && (size_extrafield_local>0))
  702.         if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
  703.                                                                            !=size_extrafield_local)
  704.                 err = ZIP_ERRNO;
  705.     zi->ci.stream.avail_in = (uInt)0;
  706.     zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  707.     zi->ci.stream.next_out = zi->ci.buffered_data;
  708.     zi->ci.stream.total_in = 0;
  709.     zi->ci.stream.total_out = 0;
  710.     if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  711.     {
  712.         zi->ci.stream.zalloc = (alloc_func)0;
  713.         zi->ci.stream.zfree = (free_func)0;
  714.         zi->ci.stream.opaque = (voidpf)0;
  715.         if (windowBits>0)
  716.             windowBits = -windowBits;
  717.         err = deflateInit2(&zi->ci.stream, level,
  718.                Z_DEFLATED, windowBits, memLevel, strategy);
  719.         if (err==Z_OK)
  720.             zi->ci.stream_initialised = 1;
  721.     }
  722. #    ifndef NOCRYPT
  723.     zi->ci.crypt_header_size = 0;
  724.     if ((err==Z_OK) && (password != NULL))
  725.     {
  726.         unsigned char bufHead[RAND_HEAD_LEN];
  727.         unsigned int sizeHead;
  728.         zi->ci.encrypt = 1;
  729.         zi->ci.pcrc_32_tab = get_crc_table();
  730.         /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
  731.         sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
  732.         zi->ci.crypt_header_size = sizeHead;
  733.         if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
  734.                 err = ZIP_ERRNO;
  735.     }
  736. #    endif
  737.     if (err==Z_OK)
  738.         zi->in_opened_file_inzip = 1;
  739.     return err;
  740. }
  741. extern int ZEXPORT zipOpenNewFileInZip2(file, filename, zipfi,
  742.                                         extrafield_local, size_extrafield_local,
  743.                                         extrafield_global, size_extrafield_global,
  744.                                         comment, method, level, raw)
  745.     zipFile file;
  746.     const char* filename;
  747.     const zip_fileinfo* zipfi;
  748.     const void* extrafield_local;
  749.     uInt size_extrafield_local;
  750.     const void* extrafield_global;
  751.     uInt size_extrafield_global;
  752.     const char* comment;
  753.     int method;
  754.     int level;
  755.     int raw;
  756. {
  757.     return zipOpenNewFileInZip3 (file, filename, zipfi,
  758.                                  extrafield_local, size_extrafield_local,
  759.                                  extrafield_global, size_extrafield_global,
  760.                                  comment, method, level, raw,
  761.                                  -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  762.                                  NULL, 0);
  763. }
  764. extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
  765.                                         extrafield_local, size_extrafield_local,
  766.                                         extrafield_global, size_extrafield_global,
  767.                                         comment, method, level)
  768.     zipFile file;
  769.     const char* filename;
  770.     const zip_fileinfo* zipfi;
  771.     const void* extrafield_local;
  772.     uInt size_extrafield_local;
  773.     const void* extrafield_global;
  774.     uInt size_extrafield_global;
  775.     const char* comment;
  776.     int method;
  777.     int level;
  778. {
  779.     return zipOpenNewFileInZip2 (file, filename, zipfi,
  780.                                  extrafield_local, size_extrafield_local,
  781.                                  extrafield_global, size_extrafield_global,
  782.                                  comment, method, level, 0);
  783. }
  784. local int zipFlushWriteBuffer(zi)
  785.   zip_internal* zi;
  786. {
  787.     int err=ZIP_OK;
  788.     if (zi->ci.encrypt != 0)
  789.     {
  790. #ifndef NOCRYPT
  791.         uInt i;
  792.         int t;
  793.         for (i=0;i<zi->ci.pos_in_buffered_data;i++)
  794.             zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
  795.                                        zi->ci.buffered_data[i],t);
  796. #endif
  797.     }
  798.     if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
  799.                                                                     !=zi->ci.pos_in_buffered_data)
  800.       err = ZIP_ERRNO;
  801.     zi->ci.pos_in_buffered_data = 0;
  802.     return err;
  803. }
  804. extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
  805.     zipFile file;
  806.     const void* buf;
  807.     unsigned len;
  808. {
  809.     zip_internal* zi;
  810.     int err=ZIP_OK;
  811.     if (file == NULL)
  812.         return ZIP_PARAMERROR;
  813.     zi = (zip_internal*)file;
  814.     if (zi->in_opened_file_inzip == 0)
  815.         return ZIP_PARAMERROR;
  816.     zi->ci.stream.next_in = (void*)buf;
  817.     zi->ci.stream.avail_in = len;
  818.     zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
  819.     while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
  820.     {
  821.         if (zi->ci.stream.avail_out == 0)
  822.         {
  823.             if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
  824.                 err = ZIP_ERRNO;
  825.             zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  826.             zi->ci.stream.next_out = zi->ci.buffered_data;
  827.         }
  828.         if(err != ZIP_OK)
  829.             break;
  830.         if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  831.         {
  832.             uLong uTotalOutBefore = zi->ci.stream.total_out;
  833.             err=deflate(&zi->ci.stream,  Z_NO_FLUSH);
  834.             zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
  835.         }
  836.         else
  837.         {
  838.             uInt copy_this,i;
  839.             if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
  840.                 copy_this = zi->ci.stream.avail_in;
  841.             else
  842.                 copy_this = zi->ci.stream.avail_out;
  843.             for (i=0;i<copy_this;i++)
  844.                 *(((char*)zi->ci.stream.next_out)+i) =
  845.                     *(((const char*)zi->ci.stream.next_in)+i);
  846.             {
  847.                 zi->ci.stream.avail_in -= copy_this;
  848.                 zi->ci.stream.avail_out-= copy_this;
  849.                 zi->ci.stream.next_in+= copy_this;
  850.                 zi->ci.stream.next_out+= copy_this;
  851.                 zi->ci.stream.total_in+= copy_this;
  852.                 zi->ci.stream.total_out+= copy_this;
  853.                 zi->ci.pos_in_buffered_data += copy_this;
  854.             }
  855.         }
  856.     }
  857.     return err;
  858. }
  859. extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32)
  860.     zipFile file;
  861.     uLong uncompressed_size;
  862.     uLong crc32;
  863. {
  864.     zip_internal* zi;
  865.     uLong compressed_size;
  866.     int err=ZIP_OK;
  867.     if (file == NULL)
  868.         return ZIP_PARAMERROR;
  869.     zi = (zip_internal*)file;
  870.     if (zi->in_opened_file_inzip == 0)
  871.         return ZIP_PARAMERROR;
  872.     zi->ci.stream.avail_in = 0;
  873.     if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  874.         while (err==ZIP_OK)
  875.     {
  876.         uLong uTotalOutBefore;
  877.         if (zi->ci.stream.avail_out == 0)
  878.         {
  879.             if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
  880.                 err = ZIP_ERRNO;
  881.             zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  882.             zi->ci.stream.next_out = zi->ci.buffered_data;
  883.         }
  884.         uTotalOutBefore = zi->ci.stream.total_out;
  885.         err=deflate(&zi->ci.stream,  Z_FINISH);
  886.         zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
  887.     }
  888.     if (err==Z_STREAM_END)
  889.         err=ZIP_OK; /* this is normal */
  890.     if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
  891.         if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
  892.             err = ZIP_ERRNO;
  893.     if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  894.     {
  895.         err=deflateEnd(&zi->ci.stream);
  896.         zi->ci.stream_initialised = 0;
  897.     }
  898.     if (!zi->ci.raw)
  899.     {
  900.         crc32 = (uLong)zi->ci.crc32;
  901.         uncompressed_size = (uLong)zi->ci.stream.total_in;
  902.     }
  903.     compressed_size = (uLong)zi->ci.stream.total_out;
  904. #    ifndef NOCRYPT
  905.     compressed_size += zi->ci.crypt_header_size;
  906. #    endif
  907.     ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
  908.     ziplocal_putValue_inmemory(zi->ci.central_header+20,
  909.                                 compressed_size,4); /*compr size*/
  910.     if (zi->ci.stream.data_type == Z_ASCII)
  911.         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
  912.     ziplocal_putValue_inmemory(zi->ci.central_header+24,
  913.                                 uncompressed_size,4); /*uncompr size*/
  914.     if (err==ZIP_OK)
  915.         err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
  916.                                        (uLong)zi->ci.size_centralheader);
  917.     free(zi->ci.central_header);
  918.     if (err==ZIP_OK)
  919.     {
  920.         long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
  921.         if (ZSEEK(zi->z_filefunc,zi->filestream,
  922.                   zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
  923.             err = ZIP_ERRNO;
  924.         if (err==ZIP_OK)
  925.             err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
  926.         if (err==ZIP_OK) /* compressed size, unknown */
  927.             err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
  928.         if (err==ZIP_OK) /* uncompressed size, unknown */
  929.             err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
  930.         if (ZSEEK(zi->z_filefunc,zi->filestream,
  931.                   cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
  932.             err = ZIP_ERRNO;
  933.     }
  934.     zi->number_entry ++;
  935.     zi->in_opened_file_inzip = 0;
  936.     return err;
  937. }
  938. extern int ZEXPORT zipCloseFileInZip (file)
  939.     zipFile file;
  940. {
  941.     return zipCloseFileInZipRaw (file,0,0);
  942. }
  943. extern int ZEXPORT zipClose (file, global_comment)
  944.     zipFile file;
  945.     const char* global_comment;
  946. {
  947.     zip_internal* zi;
  948.     int err = 0;
  949.     uLong size_centraldir = 0;
  950.     uLong centraldir_pos_inzip;
  951.     uInt size_global_comment;
  952.     if (file == NULL)
  953.         return ZIP_PARAMERROR;
  954.     zi = (zip_internal*)file;
  955.     if (zi->in_opened_file_inzip == 1)
  956.     {
  957.         err = zipCloseFileInZip (file);
  958.     }
  959. #ifndef NO_ADDFILEINEXISTINGZIP
  960.     if (global_comment==NULL)
  961.         global_comment = zi->globalcomment;
  962. #endif
  963.     if (global_comment==NULL)
  964.         size_global_comment = 0;
  965.     else
  966.         size_global_comment = (uInt)strlen(global_comment);
  967.     centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
  968.     if (err==ZIP_OK)
  969.     {
  970.         linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
  971.         while (ldi!=NULL)
  972.         {
  973.             if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
  974.                 if (ZWRITE(zi->z_filefunc,zi->filestream,
  975.                            ldi->data,ldi->filled_in_this_block)
  976.                               !=ldi->filled_in_this_block )
  977.                     err = ZIP_ERRNO;
  978.             size_centraldir += ldi->filled_in_this_block;
  979.             ldi = ldi->next_datablock;
  980.         }
  981.     }
  982.     free_datablock(zi->central_dir.first_block);
  983.     if (err==ZIP_OK) /* Magic End */
  984.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
  985.     if (err==ZIP_OK) /* number of this disk */
  986.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
  987.     if (err==ZIP_OK) /* number of the disk with the start of the central directory */
  988.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
  989.     if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
  990.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
  991.     if (err==ZIP_OK) /* total number of entries in the central dir */
  992.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
  993.     if (err==ZIP_OK) /* size of the central directory */
  994.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
  995.     if (err==ZIP_OK) /* offset of start of central directory with respect to the
  996.                             starting disk number */
  997.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
  998.                                 (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
  999.     if (err==ZIP_OK) /* zipfile comment length */
  1000.         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
  1001.     if ((err==ZIP_OK) && (size_global_comment>0))
  1002.         if (ZWRITE(zi->z_filefunc,zi->filestream,
  1003.                    global_comment,size_global_comment) != size_global_comment)
  1004.                 err = ZIP_ERRNO;
  1005.     if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
  1006.         if (err == ZIP_OK)
  1007.             err = ZIP_ERRNO;
  1008. #ifndef NO_ADDFILEINEXISTINGZIP
  1009.     TRYFREE(zi->globalcomment);
  1010. #endif
  1011.     TRYFREE(zi);
  1012.     return err;
  1013. }