1 /* zip.c -- IO on .zip files using zlib
2 Version 1.01e, February 12th, 2005
4 27 Dec 2004 Rolf Kalbermatter
5 Modification to zipOpen2 to support globalComment retrieval.
7 Copyright (C) 1998-2005 Gilles Vollant
9 Read zip.h for more info
35 /* compile with -Dlocal if your debugger can't find static symbols */
38 # define VERSIONMADEBY (0x0) /* platform depedent */
42 #define Z_BUFSIZE (16384)
45 #ifndef Z_MAXFILENAMEINZIP
46 #define Z_MAXFILENAMEINZIP (256)
50 # define ALLOC(size) (malloc(size))
53 # define TRYFREE(p) {if (p) free(p);}
57 #define SIZECENTRALDIRITEM (0x2e)
58 #define SIZEZIPLOCALHEADER (0x1e)
61 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
76 #if MAX_MEM_LEVEL >= 8
77 # define DEF_MEM_LEVEL 8
79 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
82 const char zip_copyright[] =
83 " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
86 #define SIZEDATA_INDATABLOCK (4096-(4*4))
88 #define LOCALHEADERMAGIC (0x04034b50)
89 #define CENTRALHEADERMAGIC (0x02014b50)
90 #define ENDHEADERMAGIC (0x06054b50)
92 #define FLAG_LOCALHEADER_OFFSET (0x06)
93 #define CRC_LOCALHEADER_OFFSET (0x0e)
95 #define SIZECENTRALHEADER (0x2e) /* 46 */
97 typedef struct linkedlist_datablock_internal_s
99 struct linkedlist_datablock_internal_s* next_datablock;
100 uLong avail_in_this_block;
101 uLong filled_in_this_block;
102 uLong unused; /* for future use and alignement */
103 unsigned char data[SIZEDATA_INDATABLOCK];
104 } linkedlist_datablock_internal;
106 typedef struct linkedlist_data_s
108 linkedlist_datablock_internal* first_block;
109 linkedlist_datablock_internal* last_block;
115 z_stream stream; /* zLib stream structure for inflate */
116 int stream_initialised; /* 1 is stream is initialised */
117 uInt pos_in_buffered_data; /* last written byte in buffered_data */
119 uLong pos_local_header; /* offset of the local header of the file
121 char* central_header; /* central header data for the current file */
122 uLong size_centralheader; /* size of the central header for cur file */
123 uLong flag; /* flag of the file currently writing */
125 int method; /* compression method of file currenty wr.*/
126 int raw; /* 1 for directly writing raw data */
127 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
132 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
133 const unsigned long* pcrc_32_tab;
134 int crypt_header_size;
140 zlib_filefunc_def z_filefunc;
141 voidpf filestream; /* io structore of the zipfile */
142 linkedlist_data central_dir;/* datablock with central dir in construction*/
143 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
144 curfile_info ci; /* info on the file curretly writing */
146 uLong begin_pos; /* position of the beginning of the zipfile */
147 uLong add_position_when_writting_offset;
149 #ifndef NO_ADDFILEINEXISTINGZIP
157 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
161 local linkedlist_datablock_internal* allocate_new_datablock()
163 linkedlist_datablock_internal* ldi;
164 ldi = (linkedlist_datablock_internal*)
165 ALLOC(sizeof(linkedlist_datablock_internal));
168 ldi->next_datablock = NULL ;
169 ldi->filled_in_this_block = 0 ;
170 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
175 local void free_datablock(linkedlist_datablock_internal*ldi)
179 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
185 local void init_linkedlist(linkedlist_data* ll)
187 ll->first_block = ll->last_block = NULL;
191 local void free_linkedlist(linkedlist_data* ll)
193 free_datablock(ll->first_block);
194 ll->first_block = ll->last_block = NULL;
198 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
200 linkedlist_datablock_internal* ldi;
201 const unsigned char* from_copy;
204 return ZIP_INTERNALERROR;
206 if (ll->last_block == NULL)
208 ll->first_block = ll->last_block = allocate_new_datablock();
209 if (ll->first_block == NULL)
210 return ZIP_INTERNALERROR;
213 ldi = ll->last_block;
214 from_copy = (unsigned char*)buf;
220 unsigned char* to_copy;
222 if (ldi->avail_in_this_block==0)
224 ldi->next_datablock = allocate_new_datablock();
225 if (ldi->next_datablock == NULL)
226 return ZIP_INTERNALERROR;
227 ldi = ldi->next_datablock ;
228 ll->last_block = ldi;
231 if (ldi->avail_in_this_block < len)
232 copy_this = (uInt)ldi->avail_in_this_block;
234 copy_this = (uInt)len;
236 to_copy = &(ldi->data[ldi->filled_in_this_block]);
238 for (i=0;i<copy_this;i++)
239 *(to_copy+i)=*(from_copy+i);
241 ldi->filled_in_this_block += copy_this;
242 ldi->avail_in_this_block -= copy_this;
243 from_copy += copy_this ;
251 /****************************************************************************/
253 #ifndef NO_ADDFILEINEXISTINGZIP
254 /* ===========================================================================
255 Inputs a long in LSB order to the given file
256 nbByte == 1, 2 or 4 (byte, short or long)
259 local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
260 voidpf filestream, uLong x, int nbByte));
261 local int ziplocal_putValue (const zlib_filefunc_def* pzlib_filefunc_def,
262 voidpf filestream, uLong x, int nbByte)
264 unsigned char buf[4];
266 for (n = 0; n < nbByte; n++)
268 buf[n] = (unsigned char)(x & 0xff);
272 { /* data overflow - hack for ZIP64 (X Roche) */
273 for (n = 0; n < nbByte; n++)
279 if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
285 local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
286 local void ziplocal_putValue_inmemory (void* dest, uLong x, int nbByte)
288 unsigned char* buf=(unsigned char*)dest;
290 for (n = 0; n < nbByte; n++) {
291 buf[n] = (unsigned char)(x & 0xff);
296 { /* data overflow - hack for ZIP64 */
297 for (n = 0; n < nbByte; n++)
304 /****************************************************************************/
307 local uLong ziplocal_TmzDateToDosDate(const tm_zip* ptm, uLong /* dosDate */)
309 uLong year = (uLong)ptm->tm_year;
315 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
316 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
320 /****************************************************************************/
322 local int ziplocal_getByte OF((
323 const zlib_filefunc_def* pzlib_filefunc_def,
327 local int ziplocal_getByte(const zlib_filefunc_def* pzlib_filefunc_def,
328 voidpf filestream, int *pi)
331 int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
339 if (ZERROR(*pzlib_filefunc_def,filestream))
347 /* ===========================================================================
348 Reads a long in LSB order from the given gz_stream. Sets
350 local int ziplocal_getShort OF((
351 const zlib_filefunc_def* pzlib_filefunc_def,
355 local int ziplocal_getShort ( const zlib_filefunc_def* pzlib_filefunc_def, voidpf filestream, uLong * pX)
361 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
365 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
375 local int ziplocal_getLong OF((
376 const zlib_filefunc_def* pzlib_filefunc_def,
380 local int ziplocal_getLong (const zlib_filefunc_def* pzlib_filefunc_def,
381 voidpf filestream, uLong * pX)
387 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
391 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
395 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
399 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
409 #ifndef BUFREADCOMMENT
410 #define BUFREADCOMMENT (0x400)
413 Locate the Central directory of a zipfile (at the end, just before
416 local uLong ziplocal_SearchCentralDir OF((
417 const zlib_filefunc_def* pzlib_filefunc_def,
420 local uLong ziplocal_SearchCentralDir(const zlib_filefunc_def* pzlib_filefunc_def,
426 uLong uMaxBack=0xffff; /* maximum size of global comment */
429 if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
433 uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
435 if (uMaxBack>uSizeFile)
436 uMaxBack = uSizeFile;
438 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
443 while (uBackRead<uMaxBack)
445 uLong uReadSize,uReadPos ;
447 if (uBackRead+BUFREADCOMMENT>uMaxBack)
448 uBackRead = uMaxBack;
450 uBackRead+=BUFREADCOMMENT;
451 uReadPos = uSizeFile-uBackRead ;
453 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
454 (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
455 if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
458 if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
461 for (i=(int)uReadSize-3; (i--)>0;)
462 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
463 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
465 uPosFound = uReadPos+i;
475 #endif /* !NO_ADDFILEINEXISTINGZIP*/
477 /************************************************************/
478 extern zipFile ZEXPORT zipOpen2 (const char * pathname, int append,
479 zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc_def)
486 if (pzlib_filefunc_def==NULL)
487 fill_fopen_filefunc(&ziinit.z_filefunc);
489 ziinit.z_filefunc = *pzlib_filefunc_def;
491 ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
492 (ziinit.z_filefunc.opaque,
494 (append == APPEND_STATUS_CREATE) ?
495 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
496 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
498 if (ziinit.filestream == NULL)
500 ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
501 ziinit.in_opened_file_inzip = 0;
502 ziinit.ci.stream_initialised = 0;
503 ziinit.number_entry = 0;
504 ziinit.add_position_when_writting_offset = 0;
505 init_linkedlist(&(ziinit.central_dir));
508 zi = (zip_internal*)ALLOC(sizeof(zip_internal));
511 ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
515 /* now we add file in a zipfile */
516 # ifndef NO_ADDFILEINEXISTINGZIP
517 ziinit.globalcomment = NULL;
518 if (append == APPEND_STATUS_ADDINZIP)
520 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
522 uLong size_central_dir; /* size of the central directory */
523 uLong offset_central_dir; /* offset of start of central directory */
524 uLong central_pos,uL;
526 uLong number_disk; /* number of the current dist, used for
527 spaning ZIP, unsupported, always 0*/
528 uLong number_disk_with_CD; /* number the the disk with central dir, used
529 for spaning ZIP, unsupported, always 0*/
531 uLong number_entry_CD; /* total number of entries in
533 (same than number_entry on nospan) */
536 central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
540 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
541 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
544 /* the signature, already checked */
545 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
548 /* number of this disk */
549 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
552 /* number of the disk with the start of the central directory */
553 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
556 /* total number of entries in the central dir on this disk */
557 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
560 /* total number of entries in the central dir */
561 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
564 if ((number_entry_CD!=number_entry) ||
565 (number_disk_with_CD!=0) ||
569 /* size of the central directory */
570 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
573 /* offset of start of central directory with respect to the
574 starting disk number */
575 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
578 /* zipfile global comment length */
579 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
582 if ((central_pos<offset_central_dir+size_central_dir) &&
588 ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
594 ziinit.globalcomment = (char*)ALLOC(size_comment+1);
595 if (ziinit.globalcomment)
597 size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
598 ziinit.globalcomment[size_comment]=0;
602 byte_before_the_zipfile = central_pos -
603 (offset_central_dir+size_central_dir);
604 ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
607 uLong size_central_dir_to_read = size_central_dir;
608 size_t buf_size = SIZEDATA_INDATABLOCK;
609 void* buf_read = (void*)ALLOC(buf_size);
610 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
611 offset_central_dir + byte_before_the_zipfile,
612 ZLIB_FILEFUNC_SEEK_SET) != 0)
615 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
617 uLong read_this = SIZEDATA_INDATABLOCK;
618 if (read_this > size_central_dir_to_read)
619 read_this = size_central_dir_to_read;
620 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
624 err = add_data_in_datablock(&ziinit.central_dir,buf_read,
626 size_central_dir_to_read-=read_this;
630 ziinit.begin_pos = byte_before_the_zipfile;
631 ziinit.number_entry = number_entry_CD;
633 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
634 offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
640 *globalcomment = ziinit.globalcomment;
642 # endif /* !NO_ADDFILEINEXISTINGZIP*/
646 # ifndef NO_ADDFILEINEXISTINGZIP
647 TRYFREE(ziinit.globalcomment);
648 # endif /* !NO_ADDFILEINEXISTINGZIP*/
659 extern zipFile ZEXPORT zipOpen (const char * pathname, int append)
661 return zipOpen2(pathname,append,NULL,NULL);
664 extern int ZEXPORT zipOpenNewFileInZip3 (
666 const char* filename,
667 const zip_fileinfo* zipfi,
668 const void* extrafield_local,
669 uInt size_extrafield_local,
670 const void* extrafield_global,
671 uInt size_extrafield_global,
679 const char* password,
680 uLong crcForCrypting)
689 if (password != NULL)
690 return ZIP_PARAMERROR;
694 return ZIP_PARAMERROR;
695 if ((method!=0) && (method!=Z_DEFLATED))
696 return ZIP_PARAMERROR;
698 zi = (zip_internal*)file;
700 if (zi->in_opened_file_inzip == 1)
702 err = zipCloseFileInZip (file);
714 size_comment = (uInt)strlen(comment);
716 size_filename = (uInt)strlen(filename);
722 if (zipfi->dosDate != 0)
723 zi->ci.dosDate = zipfi->dosDate;
724 else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
728 if ((level==8) || (level==9))
734 if (password != NULL)
738 zi->ci.method = method;
740 zi->ci.stream_initialised = 0;
741 zi->ci.pos_in_buffered_data = 0;
743 zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
744 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
745 size_extrafield_global + size_comment;
746 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
748 ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
750 ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
751 ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
752 ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
753 ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
754 ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
755 ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
756 ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
757 ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
758 ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
759 ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
760 ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
761 ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
764 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
766 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
769 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
771 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
773 ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
775 for (i=0;i<size_filename;i++)
776 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
778 for (i=0;i<size_extrafield_global;i++)
779 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
780 *(((const char*)extrafield_global)+i);
782 for (i=0;i<size_comment;i++)
783 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
784 size_extrafield_global+i) = *(comment+i);
785 if (zi->ci.central_header == NULL)
786 return ZIP_INTERNALERROR;
788 /* write the local header */
789 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
792 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
794 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
797 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
800 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
803 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
805 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
807 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
810 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
813 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
815 if ((err==ZIP_OK) && (size_filename>0))
816 if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
819 if ((err==ZIP_OK) && (size_extrafield_local>0))
820 if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
821 !=size_extrafield_local)
824 zi->ci.stream.avail_in = (uInt)0;
825 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
826 zi->ci.stream.next_out = zi->ci.buffered_data;
827 zi->ci.stream.total_in = 0;
828 zi->ci.stream.total_out = 0;
830 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
832 zi->ci.stream.zalloc = (alloc_func)0;
833 zi->ci.stream.zfree = (free_func)0;
834 zi->ci.stream.opaque = (voidpf)0;
837 windowBits = -windowBits;
839 err = deflateInit2(&zi->ci.stream, level,
840 Z_DEFLATED, windowBits, memLevel, strategy);
843 zi->ci.stream_initialised = 1;
846 zi->ci.crypt_header_size = 0;
847 if ((err==Z_OK) && (password != NULL))
849 unsigned char bufHead[RAND_HEAD_LEN];
850 unsigned int sizeHead;
852 zi->ci.pcrc_32_tab = get_crc_table();
853 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
855 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
856 zi->ci.crypt_header_size = sizeHead;
858 if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
864 zi->in_opened_file_inzip = 1;
868 extern int ZEXPORT zipOpenNewFileInZip2(
870 const char* filename,
871 const zip_fileinfo* zipfi,
872 const void* extrafield_local,
873 uInt size_extrafield_local,
874 const void* extrafield_global,
875 uInt size_extrafield_global,
881 return zipOpenNewFileInZip3 (file, filename, zipfi,
882 extrafield_local, size_extrafield_local,
883 extrafield_global, size_extrafield_global,
884 comment, method, level, raw,
885 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
889 extern int ZEXPORT zipOpenNewFileInZip (
891 const char* filename,
892 const zip_fileinfo* zipfi,
893 const void* extrafield_local,
894 uInt size_extrafield_local,
895 const void* extrafield_global,
896 uInt size_extrafield_global,
901 return zipOpenNewFileInZip2 (file, filename, zipfi,
902 extrafield_local, size_extrafield_local,
903 extrafield_global, size_extrafield_global,
904 comment, method, level, 0);
907 local int zipFlushWriteBuffer(
912 if (zi->ci.encrypt != 0)
917 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
918 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
919 zi->ci.buffered_data[i],t);
922 if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
923 !=zi->ci.pos_in_buffered_data)
925 zi->ci.pos_in_buffered_data = 0;
929 extern int ZEXPORT zipWriteInFileInZip (
938 return ZIP_PARAMERROR;
939 zi = (zip_internal*)file;
941 if (zi->in_opened_file_inzip == 0)
942 return ZIP_PARAMERROR;
944 zi->ci.stream.next_in = (Bytef*)buf;
945 zi->ci.stream.avail_in = len;
946 zi->ci.crc32 = crc32(zi->ci.crc32,(Bytef*)buf,len);
948 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
950 if (zi->ci.stream.avail_out == 0)
952 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
954 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
955 zi->ci.stream.next_out = zi->ci.buffered_data;
962 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
964 uLong uTotalOutBefore = zi->ci.stream.total_out;
965 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
966 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
972 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
973 copy_this = zi->ci.stream.avail_in;
975 copy_this = zi->ci.stream.avail_out;
976 for (i=0;i<copy_this;i++)
977 *(((char*)zi->ci.stream.next_out)+i) =
978 *(((const char*)zi->ci.stream.next_in)+i);
980 zi->ci.stream.avail_in -= copy_this;
981 zi->ci.stream.avail_out-= copy_this;
982 zi->ci.stream.next_in+= copy_this;
983 zi->ci.stream.next_out+= copy_this;
984 zi->ci.stream.total_in+= copy_this;
985 zi->ci.stream.total_out+= copy_this;
986 zi->ci.pos_in_buffered_data += copy_this;
994 extern int ZEXPORT zipCloseFileInZipRaw (
996 uLong uncompressed_size,
1000 uLong compressed_size;
1004 return ZIP_PARAMERROR;
1005 zi = (zip_internal*)file;
1007 if (zi->in_opened_file_inzip == 0)
1008 return ZIP_PARAMERROR;
1009 zi->ci.stream.avail_in = 0;
1011 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1014 uLong uTotalOutBefore;
1015 if (zi->ci.stream.avail_out == 0)
1017 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
1019 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1020 zi->ci.stream.next_out = zi->ci.buffered_data;
1022 uTotalOutBefore = zi->ci.stream.total_out;
1023 err=deflate(&zi->ci.stream, Z_FINISH);
1024 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1027 if (err==Z_STREAM_END)
1028 err=ZIP_OK; /* this is normal */
1030 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1031 if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
1034 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1036 err=deflateEnd(&zi->ci.stream);
1037 zi->ci.stream_initialised = 0;
1042 crc32 = (uLong)zi->ci.crc32;
1043 uncompressed_size = (uLong)zi->ci.stream.total_in;
1045 compressed_size = (uLong)zi->ci.stream.total_out;
1047 compressed_size += zi->ci.crypt_header_size;
1050 ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1051 ziplocal_putValue_inmemory(zi->ci.central_header+20,
1052 compressed_size,4); /*compr size*/
1053 if (zi->ci.stream.data_type == Z_ASCII)
1054 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1055 ziplocal_putValue_inmemory(zi->ci.central_header+24,
1056 uncompressed_size,4); /*uncompr size*/
1059 err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
1060 (uLong)zi->ci.size_centralheader);
1061 free(zi->ci.central_header);
1065 long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1066 if (ZSEEK(zi->z_filefunc,zi->filestream,
1067 zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1071 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1073 if (err==ZIP_OK) /* compressed size, unknown */
1074 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1076 if (err==ZIP_OK) /* uncompressed size, unknown */
1077 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1079 if (ZSEEK(zi->z_filefunc,zi->filestream,
1080 cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1084 zi->number_entry ++;
1085 zi->in_opened_file_inzip = 0;
1090 extern int ZEXPORT zipCloseFileInZip (zipFile file)
1092 return zipCloseFileInZipRaw (file,0,0);
1095 extern int ZEXPORT zipClose (zipFile file,
1096 const char* global_comment)
1100 uLong size_centraldir = 0;
1101 uLong centraldir_pos_inzip;
1102 uInt size_global_comment;
1104 return ZIP_PARAMERROR;
1105 zi = (zip_internal*)file;
1107 if (zi->in_opened_file_inzip == 1)
1109 err = zipCloseFileInZip (file);
1112 #ifndef NO_ADDFILEINEXISTINGZIP
1113 if (global_comment==NULL)
1114 global_comment = zi->globalcomment;
1116 if (global_comment==NULL)
1117 size_global_comment = 0;
1119 size_global_comment = (uInt)strlen(global_comment);
1121 centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1124 linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
1127 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1128 if (ZWRITE(zi->z_filefunc,zi->filestream,
1129 ldi->data,ldi->filled_in_this_block)
1130 !=ldi->filled_in_this_block )
1133 size_centraldir += ldi->filled_in_this_block;
1134 ldi = ldi->next_datablock;
1137 free_datablock(zi->central_dir.first_block);
1139 if (err==ZIP_OK) /* Magic End */
1140 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1142 if (err==ZIP_OK) /* number of this disk */
1143 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1145 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1146 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1148 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1149 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1151 if (err==ZIP_OK) /* total number of entries in the central dir */
1152 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1154 if (err==ZIP_OK) /* size of the central directory */
1155 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1157 if (err==ZIP_OK) /* offset of start of central directory with respect to the
1158 starting disk number */
1159 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
1160 (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1162 if (err==ZIP_OK) /* zipfile comment length */
1163 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1165 if ((err==ZIP_OK) && (size_global_comment>0))
1166 if (ZWRITE(zi->z_filefunc,zi->filestream,
1167 global_comment,size_global_comment) != size_global_comment)
1170 if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
1174 #ifndef NO_ADDFILEINEXISTINGZIP
1175 TRYFREE(zi->globalcomment);