Ejemplo n.º 1
0
extern int ZEXPORT zipCloseFileInZipRaw (
    zipFile file,
    uLong uncompressed_size,
    uLong crc32)
{
    zip_internal* zi;
    uLong compressed_size;
    int err=ZIP_OK;

    if (file == NULL)
        return ZIP_PARAMERROR;
    zi = (zip_internal*)file;

    if (zi->in_opened_file_inzip == 0)
        return ZIP_PARAMERROR;
    zi->ci.stream.avail_in = 0;

    if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
        while (err==ZIP_OK)
    {
        uLong uTotalOutBefore;
        if (zi->ci.stream.avail_out == 0)
        {
            if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
                err = ZIP_ERRNO;
            zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
            zi->ci.stream.next_out = zi->ci.buffered_data;
        }
        uTotalOutBefore = zi->ci.stream.total_out;
        err=deflate(&zi->ci.stream,  Z_FINISH);
        zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
    }

    if (err==Z_STREAM_END)
        err=ZIP_OK; /* this is normal */

    if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
        if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
            err = ZIP_ERRNO;

    if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
    {
        err=deflateEnd(&zi->ci.stream);
        zi->ci.stream_initialised = 0;
    }

    if (!zi->ci.raw)
    {
        crc32 = (uLong)zi->ci.crc32;
        uncompressed_size = (uLong)zi->ci.stream.total_in;
    }
    compressed_size = (uLong)zi->ci.stream.total_out;
#    ifndef NOCRYPT
    compressed_size += zi->ci.crypt_header_size;
#    endif

    ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
    ziplocal_putValue_inmemory(zi->ci.central_header+20,
                                compressed_size,4); /*compr size*/
    if (zi->ci.stream.data_type == Z_ASCII)
        ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
    ziplocal_putValue_inmemory(zi->ci.central_header+24,
                                uncompressed_size,4); /*uncompr size*/

    if (err==ZIP_OK)
        err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
                                       (uLong)zi->ci.size_centralheader);
    free(zi->ci.central_header);

    if (err==ZIP_OK)
    {
        long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
        if (ZSEEK(zi->z_filefunc,zi->filestream,
                  zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
            err = ZIP_ERRNO;

        if (err==ZIP_OK)
            err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */

        if (err==ZIP_OK) /* compressed size, unknown */
            err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);

        if (err==ZIP_OK) /* uncompressed size, unknown */
            err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);

        if (ZSEEK(zi->z_filefunc,zi->filestream,
                  cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
            err = ZIP_ERRNO;
    }

    zi->number_entry ++;
    zi->in_opened_file_inzip = 0;

    return err;
}
Ejemplo n.º 2
0
extern MINIZIP_EXPORT zipFile zipOpen2 (const char *pathname,int append,zipcharpc* globalcomment,zlib_filefunc_def* pzlib_filefunc_def)

{
  zip_internal ziinit;
  zip_internal* zi;
  int err=ZIP_OK;


  if (pzlib_filefunc_def==0)
    fill_fopen_filefunc(&ziinit.z_filefunc);
  else
    ziinit.z_filefunc = *pzlib_filefunc_def;

  ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
    (ziinit.z_filefunc.opaque,
     pathname,
     (append == APPEND_STATUS_CREATE) ?
     (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
     (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));

  if (ziinit.filestream == 0)
    return 0;
  ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
  ziinit.in_opened_file_inzip = 0;
  ziinit.ci.stream_initialised = 0;
  ziinit.number_entry = 0;
  ziinit.add_position_when_writting_offset = 0;
  init_linkedlist(&(ziinit.central_dir));


  zi = (zip_internal*)ALLOC(sizeof(zip_internal));
  if (zi==0)
    {
      ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
      return 0;
    }

  /* now we add file in a zipfile */
#    ifndef NO_ADDFILEINEXISTINGZIP
  ziinit.globalcomment = 0;
  if (append == APPEND_STATUS_ADDINZIP)
    {
      uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/

      uLong size_central_dir;     /* size of the central directory  */
      uLong offset_central_dir;   /* offset of start of central directory */
      uLong central_pos,uL;

      uLong number_disk;          /* number of the current dist, used for
                                     spaning ZIP, unsupported, always 0*/
      uLong number_disk_with_CD;  /* number the the disk with central dir, used
                                     for spaning ZIP, unsupported, always 0*/
      uLong number_entry;
      uLong number_entry_CD;      /* total number of entries in
                                     the central dir
                                     (same than number_entry on nospan) */
      uLong size_comment;

      central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
      if (central_pos==0)
        err=ZIP_ERRNO;

      if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
                central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
        err=ZIP_ERRNO;

      /* the signature, already checked */
      if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
        err=ZIP_ERRNO;

      /* number of this disk */
      if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
        err=ZIP_ERRNO;

      /* number of the disk with the start of the central directory */
      if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
        err=ZIP_ERRNO;

      /* total number of entries in the central dir on this disk */
      if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
        err=ZIP_ERRNO;

      /* total number of entries in the central dir */
      if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
        err=ZIP_ERRNO;

      if ((number_entry_CD!=number_entry) ||
          (number_disk_with_CD!=0) ||
          (number_disk!=0))
        err=ZIP_BADZIPFILE;

      /* size of the central directory */
      if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
        err=ZIP_ERRNO;

      /* offset of start of central directory with respect to the
         starting disk number */
      if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
        err=ZIP_ERRNO;

      /* zipfile global comment length */
      if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
        err=ZIP_ERRNO;

      if ((central_pos<offset_central_dir+size_central_dir) &&
          (err==ZIP_OK))
        err=ZIP_BADZIPFILE;

      if (err!=ZIP_OK)
        {
          ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
          return 0;
        }

      if (size_comment>0)
        {
          ziinit.globalcomment = ALLOC(size_comment+1);
          if (ziinit.globalcomment)
            {
              size_comment = ZREAD(ziinit.z_filefunc,
                                   ziinit.filestream,
                                   ziinit.globalcomment,
                                   size_comment);
              ziinit.globalcomment[size_comment]=0;
            }
        }

      byte_before_the_zipfile = central_pos -
                                (offset_central_dir+size_central_dir);
      ziinit.add_position_when_writting_offset = byte_before_the_zipfile;

      {
        uLong size_central_dir_to_read = size_central_dir;
        size_t buf_size = SIZEDATA_INDATABLOCK;
        void* buf_read = (void*)ALLOC(buf_size);
        if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
                  offset_central_dir + byte_before_the_zipfile,
                  ZLIB_FILEFUNC_SEEK_SET) != 0)
          err=ZIP_ERRNO;

        while ((size_central_dir_to_read>0) && (err==ZIP_OK))
          {
            uLong read_this = SIZEDATA_INDATABLOCK;
            if (read_this > size_central_dir_to_read)
              read_this = size_central_dir_to_read;
            if (ZREAD(ziinit.z_filefunc,
                      ziinit.filestream,
                      buf_read,
                      read_this) != read_this)
              err=ZIP_ERRNO;

            if (err==ZIP_OK)
              err = add_data_in_datablock(&ziinit.central_dir,buf_read,
                                          (uLong)read_this);
            size_central_dir_to_read-=read_this;
          }
        TRYFREE(buf_read);
      }
      ziinit.begin_pos = byte_before_the_zipfile;
      ziinit.number_entry = number_entry_CD;

      if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
                offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
        err=ZIP_ERRNO;
    }

  if (globalcomment)
    {
      *globalcomment = ziinit.globalcomment;
    }
#    endif /* !NO_ADDFILEINEXISTINGZIP*/

  if (err != ZIP_OK)
    {
#    ifndef NO_ADDFILEINEXISTINGZIP
      TRYFREE(ziinit.globalcomment);
#    endif /* !NO_ADDFILEINEXISTINGZIP*/
      TRYFREE(zi);
      return 0;
    }
  else
    {
      *zi = ziinit;
      return (zipFile)zi;
    }
}
Ejemplo n.º 3
0
extern int ZEXPORT zipCloseFileInZip (zipFile file)
{
    zip_internal* zi;
    int err=ZIP_OK;

    if (file == NULL)
        return ZIP_PARAMERROR;
    zi = (zip_internal*)file;

    if (zi->in_opened_file_inzip == 0)    
        return ZIP_PARAMERROR;
    zi->ci.stream.avail_in = 0;
    
    if (zi->ci.method == Z_DEFLATED)
        while (err==ZIP_OK)
    {
        uLong uTotalOutBefore;
        if (zi->ci.stream.avail_out == 0)
        {
            if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
                                                                           !=1)
                err = ZIP_ERRNO;
            zi->ci.pos_in_buffered_data = 0;
            zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
            zi->ci.stream.next_out = zi->ci.buffered_data;
        }
        uTotalOutBefore = zi->ci.stream.total_out;
        err=deflate(&zi->ci.stream,  Z_FINISH);
        zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
    }

    if (err==Z_STREAM_END)
        err=ZIP_OK; /* this is normal */

    if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
        if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
                                                                       !=1)
            err = ZIP_ERRNO;

    if ((zi->ci.method == Z_DEFLATED) && (err==ZIP_OK))
    {
        err=deflateEnd(&zi->ci.stream);
        zi->ci.stream_initialised = 0;
    }

    ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)zi->ci.crc32,4); /*crc*/
    ziplocal_putValue_inmemory(zi->ci.central_header+20,
                                (uLong)zi->ci.stream.total_out,4); /*compr size*/
    if (zi->ci.stream.data_type == Z_ASCII)
        ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2); 
    ziplocal_putValue_inmemory(zi->ci.central_header+24,
                                (uLong)zi->ci.stream.total_in,4); /*uncompr size*/

    if (err==ZIP_OK)
        err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
                                       (uLong)zi->ci.size_centralheader);
    free(zi->ci.central_header);

    if (err==ZIP_OK)
    {
        long cur_pos_inzip = ftell(zi->filezip);
	    if (fseek(zi->filezip,
                  zi->ci.pos_local_header + 14,SEEK_SET)!=0)
		    err = ZIP_ERRNO;

        if (err==ZIP_OK)
            err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.crc32,4); /* crc 32, unknown */

        if (err==ZIP_OK) /* compressed size, unknown */
            err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.stream.total_out,4); 

        if (err==ZIP_OK) /* uncompressed size, unknown */
            err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.stream.total_in,4);

	    if (fseek(zi->filezip,
                  cur_pos_inzip,SEEK_SET)!=0)
		    err = ZIP_ERRNO;
    }

    zi->number_entry ++;
    zi->in_opened_file_inzip = 0;

    return err;
}