Example #1
0
/*
  Close a ZipFile opened with unzipOpen.
  If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
    these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  return UNZ_OK if there is no problem. */
extern int ZEXPORT unzClose (unzFile file)
{
    unz_s* s;
    if (file==NULL)
        return UNZ_PARAMERROR;
    s=(unz_s*)file;

    if (s->pfile_in_zip_read!=NULL)
        unzCloseCurrentFile(file);

    ZCLOSE(s->z_filefunc, s->filestream);
    TRYFREE(s);
    return UNZ_OK;
}
Example #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;
    }
}
Example #3
0
extern int ZEXPORT zipClose (
    zipFile file,
    const char* global_comment)
{
    zip_internal* zi;
    int err = 0;
    uLong size_centraldir = 0;
    uLong centraldir_pos_inzip ;
    uInt size_global_comment;
    if (file == NULL)
        return ZIP_PARAMERROR;
    zi = (zip_internal*)file;

    if (zi->in_opened_file_inzip == 1)
    {
        err = zipCloseFileInZip (file);
    }

    if (global_comment==NULL)
        size_global_comment = 0;
    else
        size_global_comment = strlen(global_comment);


    centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
    if (err==ZIP_OK)
    {
        linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
        while (ldi!=NULL)
        {
            if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
                if (ZWRITE(zi->z_filefunc,zi->filestream,
                           ldi->data,ldi->filled_in_this_block)
                              !=ldi->filled_in_this_block )
                    err = ZIP_ERRNO;

            size_centraldir += ldi->filled_in_this_block;
            ldi = ldi->next_datablock;
        }
    }
    free_datablock(zi->central_dir.first_block);

    if (err==ZIP_OK) /* Magic End */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);

    if (err==ZIP_OK) /* number of this disk */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);

    if (err==ZIP_OK) /* number of the disk with the start of the central directory */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);

    if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);

    if (err==ZIP_OK) /* total number of entries in the central dir */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);

    if (err==ZIP_OK) /* size of the central directory */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);

    if (err==ZIP_OK) /* offset of start of central directory with respect to the
                            starting disk number */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
                                (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);

    if (err==ZIP_OK) /* zipfile comment length */
        err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);

    if ((err==ZIP_OK) && (size_global_comment>0))
        if (ZWRITE(zi->z_filefunc,zi->filestream,
                   global_comment,size_global_comment) != size_global_comment)
                err = ZIP_ERRNO;

    if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
        if (err == ZIP_OK)
            err = ZIP_ERRNO;

    TRYFREE(zi);

    return err;
}
Example #4
0
/*
  Open a Zip file. path contain the full pathname (by example,
     on a Windows NT computer "c:\\test\\zlib114.zip" or on a Unix computer
     "zlib/zlib114.zip".
     If the zipfile cannot be opened (file doesn't exist or in not valid), the
       return value is NULL.
     Else, the return value is an unzFile Handle, usable with other function
       of this unzip package.
*/
unzFile ZEXPORT unzOpen2 (
    const char *path,
    zlib_filefunc_def* pzlib_filefunc_def)
{
    unz_s us;
    unz_s *s;
    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_CD;      /* total number of entries in
                                   the central dir
                                   (same than number_entry on nospan) */

    int err=UNZ_OK;

    if (unz_copyright[0]!=' ')
        return NULL;

    if (pzlib_filefunc_def==NULL)
        fill_fopen_filefunc(&us.z_filefunc);
    else
        us.z_filefunc = *pzlib_filefunc_def;

    us.filestream= (*(us.z_filefunc.zopen_file))(us.z_filefunc.opaque,
                                                 path,
                                                 ZLIB_FILEFUNC_MODE_READ |
                                                 ZLIB_FILEFUNC_MODE_EXISTING);
    if (us.filestream==NULL)
        return NULL;

    central_pos = unzlocal_SearchCentralDir(&us.z_filefunc,us.filestream);
    if (central_pos==0)
        err=UNZ_ERRNO;

    if (ZSEEK(us.z_filefunc, us.filestream,
                                      central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
        err=UNZ_ERRNO;

    /* the signature, already checked */
    if (unzlocal_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
        err=UNZ_ERRNO;

    /* number of this disk */
    if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
        err=UNZ_ERRNO;

    /* number of the disk with the start of the central directory */
    if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
        err=UNZ_ERRNO;

    /* total number of entries in the central dir on this disk */
    if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
        err=UNZ_ERRNO;

    /* total number of entries in the central dir */
    if (unzlocal_getShort(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
        err=UNZ_ERRNO;

    if ((number_entry_CD!=us.gi.number_entry) ||
        (number_disk_with_CD!=0) ||
        (number_disk!=0))
        err=UNZ_BADZIPFILE;

    /* size of the central directory */
    if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
        err=UNZ_ERRNO;

    /* offset of start of central directory with respect to the
          starting disk number */
    if (unzlocal_getLong(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
        err=UNZ_ERRNO;

    /* zipfile comment length */
    if (unzlocal_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
        err=UNZ_ERRNO;

    if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
        (err==UNZ_OK))
        err=UNZ_BADZIPFILE;

    if (err!=UNZ_OK)
    {
        ZCLOSE(us.z_filefunc, us.filestream);
        return NULL;
    }

    us.byte_before_the_zipfile = central_pos -
                            (us.offset_central_dir+us.size_central_dir);
    us.central_pos = central_pos;
    us.pfile_in_zip_read = NULL;
    us.encrypted = 0;


    s=(unz_s*)ALLOC(sizeof(unz_s));
    *s=us;
    unzGoToFirstFile((unzFile)s);
    return (unzFile)s;
}