Ejemplo n.º 1
0
int hts_extract_meta(const char* path) {
    char catbuff[CATBUFF_SIZE];
    unzFile zFile = unzOpen(fconcat(catbuff,path,"hts-cache/new.zip"));
    zipFile zFileOut = zipOpen(fconcat(catbuff,path,"hts-cache/meta.zip"), 0);
    if (zFile != NULL && zFileOut != NULL) {
        if (unzGoToFirstFile(zFile) == Z_OK) {
            zip_fileinfo fi;
            unz_file_info ufi;
            char BIGSTK filename[HTS_URLMAXSIZE * 4];
            char BIGSTK comment[8192];
            memset(comment, 0, sizeof(comment));       // for truncated reads
            memset(&fi, 0, sizeof(fi));
            memset(&ufi, 0, sizeof(ufi));
            do  {
                int readSizeHeader;
                filename[0] = '\0';
                comment[0] = '\0';

                if (unzOpenCurrentFile(zFile) == Z_OK) {
                    if (
                        (readSizeHeader = unzGetLocalExtrafield(zFile, comment, sizeof(comment) - 2)) > 0
                        &&
                        unzGetCurrentFileInfo(zFile, &ufi, filename, sizeof(filename) - 2, NULL, 0, NULL, 0) == Z_OK
                    )
                    {
                        comment[readSizeHeader] = '\0';
                        fi.dosDate = ufi.dosDate;
                        fi.internal_fa = ufi.internal_fa;
                        fi.external_fa = ufi.external_fa;
                        if (zipOpenNewFileInZip(zFileOut,
                                                filename,
                                                &fi,
                                                NULL,
                                                0,
                                                NULL,
                                                0,
                                                NULL, /* comment */
                                                Z_DEFLATED,
                                                Z_DEFAULT_COMPRESSION) == Z_OK)
                        {
                            if (zipWriteInFileInZip(zFileOut, comment, (int) strlen(comment)) != Z_OK) {
                            }
                            if (zipCloseFileInZip(zFileOut) != Z_OK) {
                            }
                        }
                    }
                    unzCloseCurrentFile(zFile);
                }
            } while( unzGoToNextFile(zFile) == Z_OK );
        }
        zipClose(zFileOut, "Meta-data extracted by HTTrack/"HTTRACK_VERSION);
        unzClose(zFile);
        return 1;
    }
    return 0;
}
Ejemplo n.º 2
0
static int hb_zipDeleteFile( const char * szZipFile, const char * szFileMask )
{
   char            szTempFile[ HB_PATH_MAX ];
   char            szCurrFile[ HB_PATH_MAX ];
   PHB_FNAME       pFileName;
   HB_FHANDLE      hFile;
   unzFile         hUnzip;
   zipFile         hZip;
   unz_global_info ugi;
   unz_file_info   ufi;
   zip_fileinfo    zfi;
   char *          pszGlobalComment = NULL;
   char *          pszFileComment   = NULL;
   void *          pExtraField      = NULL;
   void *          pLocalExtraField = NULL;
   int    iFilesLeft = 0;
   int    iFilesDel  = 0;
   int    iExtraFieldLen;
   int    method;
   int    level;
   int    iResult;
   char * pszFree;

   /* open source file */
   hUnzip = unzOpen( hb_fsNameConv( szZipFile, &pszFree ) );

   if( pszFree )
      hb_xfree( pszFree );

   if( hUnzip == NULL )
      return UNZ_ERRNO;

   pFileName = hb_fsFNameSplit( szZipFile );
   hFile     = hb_fsCreateTemp( pFileName->szPath, NULL, FC_NORMAL, szTempFile );
   hZip      = NULL;
   if( hFile != FS_ERROR )
   {
      hb_fsClose( hFile );
      hZip = zipOpen( szTempFile, APPEND_STATUS_CREATE );
   }
   hb_xfree( pFileName );

   if( hZip == NULL )
   {
      unzClose( hUnzip );
      return UNZ_ERRNO;
   }

   iResult = unzGetGlobalInfo( hUnzip, &ugi );
   if( iResult == UNZ_OK )
   {
      if( ugi.size_comment > 0 )
      {
         pszGlobalComment = ( char * ) hb_xgrab( ugi.size_comment + 1 );
         if( ( uLong ) unzGetGlobalComment( hUnzip, pszGlobalComment,
                                            ugi.size_comment ) != ugi.size_comment )
            iResult = UNZ_ERRNO;
         pszGlobalComment[ ugi.size_comment ] = '\0';
      }
      if( iResult == UNZ_OK )
         iResult = unzGoToFirstFile( hUnzip );
   }

   while( iResult == UNZ_OK )
   {
      iResult = unzGetCurrentFileInfo( hUnzip, &ufi, szCurrFile, HB_PATH_MAX - 1, NULL, 0, NULL, 0 );
      if( iResult != UNZ_OK )
         break;

      if( hb_strMatchFile( szCurrFile, szFileMask ) )
         iFilesDel++;
      else
      {
         if( ufi.size_file_extra )
            pExtraField = ( char * ) hb_xgrab( ufi.size_file_extra );
         if( ufi.size_file_comment )
            pszFileComment = ( char * ) hb_xgrab( ufi.size_file_comment + 1 );

         iResult = unzGetCurrentFileInfo( hUnzip, &ufi, NULL, 0,
                                          pExtraField, ufi.size_file_extra,
                                          pszFileComment, ufi.size_file_comment );
         if( pszFileComment )
            pszFileComment[ ufi.size_file_comment ] = '\0';
         if( iResult != UNZ_OK )
            break;

         iResult = unzOpenCurrentFile2( hUnzip, &method, &level, 1 );
         if( iResult != UNZ_OK )
            break;

         iExtraFieldLen = unzGetLocalExtrafield( hUnzip, NULL, 0 );
         if( iExtraFieldLen < 0 )
         {
            iResult = UNZ_ERRNO;
            break;
         }
         else if( iExtraFieldLen > 0 )
         {
            pLocalExtraField = hb_xgrab( iExtraFieldLen );
            if( unzGetLocalExtrafield( hUnzip, pLocalExtraField, iExtraFieldLen ) != iExtraFieldLen )
            {
               iResult = UNZ_ERRNO;
               break;
            }
         }

         memset( &zfi, 0, sizeof( zfi ) );
         memcpy( &zfi.tmz_date, &ufi.tmu_date, sizeof( tm_unz ) );
         zfi.dosDate     = ufi.dosDate;
         zfi.internal_fa = ufi.internal_fa;
         zfi.external_fa = ufi.external_fa;

         iResult = zipOpenNewFileInZip2( hZip, szCurrFile, &zfi, pLocalExtraField, iExtraFieldLen, pExtraField, ufi.size_file_extra, pszFileComment, method, level, 1 );
         if( iResult != UNZ_OK )
            break;

         if( ufi.compressed_size )
         {
            void * buffer = hb_xgrab( HB_Z_IOBUF_SIZE );
            uLong  ulLeft = ufi.compressed_size;

            while( ulLeft > 0 )
            {
               int iRead = HB_MIN( ulLeft, HB_Z_IOBUF_SIZE );
               iResult = unzReadCurrentFile( hUnzip, ( voidp ) buffer, iRead );
               if( iResult < 0 )
                  break;
               if( iResult != iRead )
               {
                  iResult = UNZ_ERRNO;
                  break;
               }
               iResult = zipWriteInFileInZip( hZip, ( voidp ) buffer, iRead );
               if( iResult != UNZ_OK )
                  break;
               ulLeft -= iRead;
            }
            hb_xfree( buffer );
            if( iResult != UNZ_OK )
               break;
         }

         iResult = zipCloseFileInZipRaw( hZip, ufi.uncompressed_size, ufi.crc );
         if( iResult != UNZ_OK )
            break;

         iResult = unzCloseCurrentFile( hUnzip );
         if( iResult != UNZ_OK )
            break;

         if( pExtraField )
         {
            hb_xfree( pExtraField );
            pExtraField = NULL;
         }
         if( pszFileComment )
         {
            hb_xfree( pszFileComment );
            pszFileComment = NULL;
         }
         if( pLocalExtraField )
         {
            hb_xfree( pLocalExtraField );
            pLocalExtraField = NULL;
         }
         iFilesLeft++;
      }
      iResult = unzGoToNextFile( hUnzip );
   }

   if( pExtraField )
      hb_xfree( pExtraField );
   if( pszFileComment )
      hb_xfree( pszFileComment );
   if( pLocalExtraField )
      hb_xfree( pLocalExtraField );

   if( iFilesDel == 0 )
      iResult = UNZ_ERRNO;
   else if( iResult == UNZ_END_OF_LIST_OF_FILE )
      iResult = UNZ_OK;

   if( iResult != UNZ_OK )
      zipClose( hZip, NULL );
   else
      iResult = zipClose( hZip, pszGlobalComment );
   unzClose( hUnzip );
   if( pszGlobalComment )
      hb_xfree( pszGlobalComment );

   if( iResult != UNZ_OK )
      hb_fsDelete( szTempFile );
   else
   {
      hb_fsDelete( szZipFile );

      if( iFilesLeft == 0 )
         hb_fsDelete( szTempFile );
      else if( ! hb_fsRename( szTempFile, szZipFile ) )
         iResult = UNZ_ERRNO;
   }

   return iResult;
}
Ejemplo n.º 3
0
///////////////////////////////////////////////////////////////
//
// CResourceChecker::ReplaceFilesInZIP
//
// Based on example at http://www.winimage.com/zLibDll/minizip.html
// by Ivan A. Krestinin
//
///////////////////////////////////////////////////////////////
int CResourceChecker::ReplaceFilesInZIP( const string& strOrigZip, const string& strTempZip, const vector < string >& pathInArchiveList, const vector < string >& m_upgradedFullPathList )
{
    // open source and destination file
    zlib_filefunc_def ffunc;
    #ifdef WIN32
    fill_win32_filefunc(&ffunc);
    #else
    fill_fopen_filefunc(&ffunc);
    #endif

    zipFile szip = unzOpen2(strOrigZip.c_str(), &ffunc);
    if (szip==NULL) { /*free(tmp_name);*/ return 0; }
    zipFile dzip = zipOpen2(strTempZip.c_str(), APPEND_STATUS_CREATE, NULL, &ffunc);
    if (dzip==NULL) { unzClose(szip); /*free(tmp_name);*/ return 0; }

    // get global commentary
    unz_global_info glob_info;
    if (unzGetGlobalInfo(szip, &glob_info) != UNZ_OK) { zipClose(dzip, NULL); unzClose(szip); /*free(tmp_name);*/ return 0; }

    char* glob_comment = NULL;
    if (glob_info.size_comment > 0)
    {
        glob_comment = (char*)malloc(glob_info.size_comment+1);
        if ((glob_comment==NULL)&&(glob_info.size_comment!=0)) { zipClose(dzip, NULL); unzClose(szip); /*free(tmp_name);*/ return 0; }

        if ((unsigned int)unzGetGlobalComment(szip, glob_comment, glob_info.size_comment+1) != glob_info.size_comment)  { zipClose(dzip, NULL); unzClose(szip); free(glob_comment); /*free(tmp_name);*/ return 0; }
    }

    // copying files
    int n_files = 0;

    int rv = unzGoToFirstFile(szip);
    while (rv == UNZ_OK)
    {
        // get zipped file info
        unz_file_info unzfi;
        char dos_fn[MAX_PATH];
        if (unzGetCurrentFileInfo(szip, &unzfi, dos_fn, MAX_PATH, NULL, 0, NULL, 0) != UNZ_OK) break;
        char fn[MAX_PATH];
        #ifdef WIN32
        OemToChar(dos_fn, fn);
        #endif

        // See if file should be replaced
        string fullPathReplacement;
        for ( unsigned long i = 0 ; i < pathInArchiveList.size () ; i++ )
            if ( stricmp ( fn, pathInArchiveList[i].c_str () ) == 0 )
                fullPathReplacement = m_upgradedFullPathList[i];

        // Replace file in zip
        if ( fullPathReplacement.length () )
        {
            void* buf = NULL;
            unsigned long ulLength = 0;

            // Get new file into a buffer
            if ( FILE* pFile = File::Fopen ( fullPathReplacement.c_str (), "rb" ) )
            {
                // Get the file size,
                fseek( pFile, 0, SEEK_END );
                ulLength = ftell( pFile );
                fseek( pFile, 0, SEEK_SET );

                // Load file into a buffer
                buf = malloc( ulLength );
                if ( fread ( buf, 1, ulLength, pFile ) != ulLength )
                {
                    free( buf );
                    buf = NULL;
                }

                // Clean up
                fclose ( pFile );
            }

            if( !buf )
                break;

            // open destination file
            zip_fileinfo zfi;
            memcpy (&zfi.tmz_date, &unzfi.tmu_date, sizeof(tm_unz));
            zfi.dosDate = unzfi.dosDate;
            zfi.internal_fa = unzfi.internal_fa;
            zfi.external_fa = unzfi.external_fa;

            char* extrafield = NULL;
            char* commentary = NULL;
            int size_local_extra = 0;
            void* local_extra = NULL;
            int unzfi_size_file_extra = 0;
            int method = Z_DEFLATED;
            int level = Z_DEFAULT_COMPRESSION;


            if (zipOpenNewFileInZip(dzip, dos_fn, &zfi, local_extra, size_local_extra, extrafield, unzfi_size_file_extra, commentary, method, level )!=UNZ_OK)
                {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            // write file
            if (zipWriteInFileInZip(dzip, buf, ulLength)!=UNZ_OK)
                {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            if (zipCloseFileInZip(dzip/*, unzfi.uncompressed_size, unzfi.crc*/)!=UNZ_OK)
                {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            free( buf );
        }

        // Copy file in zip
        if ( !fullPathReplacement.length () )
        {
            char* extrafield = (char*)malloc(unzfi.size_file_extra);
            if ((extrafield==NULL)&&(unzfi.size_file_extra!=0)) break;
            char* commentary = (char*)malloc(unzfi.size_file_comment);
            if ((commentary==NULL)&&(unzfi.size_file_comment!=0)) {free(extrafield); break;}

            if (unzGetCurrentFileInfo(szip, &unzfi, dos_fn, MAX_PATH, extrafield, unzfi.size_file_extra, commentary, unzfi.size_file_comment) != UNZ_OK) {free(extrafield); free(commentary); break;}

            // open file for RAW reading
            int method;
            int level;
            if (unzOpenCurrentFile2(szip, &method, &level, 1)!=UNZ_OK) {free(extrafield); free(commentary); break;}

            int size_local_extra = unzGetLocalExtrafield(szip, NULL, 0);
            if (size_local_extra<0) {free(extrafield); free(commentary); break;}
            void* local_extra = malloc(size_local_extra);
            if ((local_extra==NULL)&&(size_local_extra!=0)) {free(extrafield); free(commentary); break;}
            if (unzGetLocalExtrafield(szip, local_extra, size_local_extra)<0) {free(extrafield); free(commentary); free(local_extra); break;}

            // this malloc may fail if file very large
            void* buf = malloc(unzfi.compressed_size);
            if ((buf==NULL)&&(unzfi.compressed_size!=0)) {free(extrafield); free(commentary); free(local_extra); break;}

            // read file
            int sz = unzReadCurrentFile(szip, buf, unzfi.compressed_size);
            if ((unsigned int)sz != unzfi.compressed_size) {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            // open destination file
            zip_fileinfo zfi;
            memcpy (&zfi.tmz_date, &unzfi.tmu_date, sizeof(tm_unz));
            zfi.dosDate = unzfi.dosDate;
            zfi.internal_fa = unzfi.internal_fa;
            zfi.external_fa = unzfi.external_fa;

            if (zipOpenNewFileInZip2(dzip, dos_fn, &zfi, local_extra, size_local_extra, extrafield, unzfi.size_file_extra, commentary, method, level, 1)!=UNZ_OK) {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            // write file
            if (zipWriteInFileInZip(dzip, buf, unzfi.compressed_size)!=UNZ_OK) {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            if (zipCloseFileInZipRaw(dzip, unzfi.uncompressed_size, unzfi.crc)!=UNZ_OK) {free(extrafield); free(commentary); free(local_extra); free(buf); break;}

            if (unzCloseCurrentFile(szip)==UNZ_CRCERROR) {free(extrafield); free(commentary); free(local_extra); free(buf); break;}
            free(commentary);
            free(buf);
            free(extrafield);
            free(local_extra);

            n_files ++;
        }

        rv = unzGoToNextFile(szip);
    }

    zipClose(dzip, glob_comment);
    unzClose(szip);

    free(glob_comment);

    return rv==UNZ_END_OF_LIST_OF_FILE;
}
Ejemplo n.º 4
0
//entry manipulation
int XSYNCZIP_openEntryByName ( XDRM_CTRL_CONTEXT_PTR pCtx, EntryHandle *entryHandle, char *name )
{
   int retval =0;
   int interRetval = 0;

   int compSize =0;
   int uncompSize=0;
   unz64_s dummy;

   //extra infos, need these to calculate current entrys's offset from entire zip file
   int entryNameLen =0;
   int extraFieldLen = 0;
   int extraLen =0;

   entryHandle->isAbleToRandomAccess = 0;
   entryHandle->index = NULL;

   if ( name == NULL || entryHandle == NULL ) {
      return UNZ_PARAMERROR;
   }

   if ( UNZ_OK != ( interRetval = unzLocateFile ( pCtx->pZipFile,name,0/*CASESENSITIVITY*/ ) )  ) {
      retval =-1;
      entryHandle->name = NULL;
      entryHandle->idx = -1;
      entryHandle->crrPos = -1;
      entryHandle->endOffsetOnEntireFile=-1;
      entryHandle->isDir = NOT_IMPLEMENTED_YET;

   } else {

      // open current entry
	   dummy= *(pCtx->pZipFile);  //copy base infos
      dummy.pfile_in_zip_read= NULL;    // we want new zip read info so.

      if ( UNZ_OK == ( retval =  unzOpenCurrentFile ( &dummy ) ) ) {
         compSize = dummy.cur_file_info.compressed_size;
         uncompSize = dummy.cur_file_info.uncompressed_size;
         entryNameLen = dummy.cur_file_info.size_filename;
         extraFieldLen = unzGetLocalExtrafield ( &dummy,NULL,0 ); //pCtx->pZipFile->cur_file_info.external_fa;

         //total extra len = name len + extra field len
         extraLen = extraFieldLen + entryNameLen;

         entryHandle->offsetOnEntireFile =  dummy.byte_before_the_zipfile +
                                            dummy.cur_file_info_internal.offset_curfile +
                                            0x1e/*SIZEZIPLOCALHEADER*/+
                                            extraLen;

         entryHandle->idx= dummy.num_file;
         entryHandle->crrPos = 0;
         entryHandle->prePos = 0;
         entryHandle->compSize = compSize; //size of entry
         entryHandle->uncompSize = uncompSize; //size of entry
         entryHandle->endPos = entryHandle->crrPos + uncompSize;  //offset+size
         entryHandle->endOffsetOnEntireFile = entryHandle->offsetOnEntireFile + uncompSize;  //offsetoffsetOnEntireFile+size
         entryHandle->isDir = NOT_IMPLEMENTED_YET;
         entryHandle->name = ( char * ) calloc ( entryNameLen+1, sizeof ( char ) );
         strncpy ( entryHandle->name, name, entryNameLen );

         entryHandle->readInfo = dummy.pfile_in_zip_read;

         //do not close, because we'll keep its decompressing history
         //closeEntry will do this clean up
         //unzCloseCurrentFile ( pCtx->pZipFile );
      }

   }

   return retval;
}