Announcement

Collapse
No announcement yet.

ZLib and a Progress Bar

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ZLib and a Progress Bar

    Anyone got an example of using Zlib to uncompress
    a Zip Archive with multiple files in the archive.

    And is it possible to get progress info for a progress bar
    with zLib ?

    (i know aZip can do it but I hsve to use zlib)



    ------------------
    Kind Regards
    Mike

  • #2
    Mike,

    Try the following code:

    Code:
        DIM szFile AS ASCIIZ * 260, fH AS DWORD, enumZip AS DWORD, MainStr AS STRING
        DIM retZip AS LONG, global_info AS unz_global_info_s, unz_file_info AS unz_file_info_s
        
        fH = unzOpen(BYCOPY szFile)
         IF fH < 1 THEN FUNCTION = 0: EXIT FUNCTION
        CALL unzGetGlobalInfo(fH, global_info)
    
         FOR enumZip = 1 TO global_info.number_entry
             IF enumZip = 1 THEN CALL unzGoToFirstFile(fH)
             IF enumZip > 1 THEN CALL unzGoToNextFile(fH)
             
             retZip = unzGetCurrentFileInfo(fh, unz_file_info, szFileName, SIZEOF(szFileName), BYVAL 0, 0, BYVAL 0, 0)
             retZip = unzOpenCurrentFile(fH)
    
             MainStr = SPACE$(unz_file_info.uncompressed_size)
             retZip = unzReadCurrentFile(fH, BYVAL STRPTR(MainStr), unz_file_info.uncompressed_size)
    
                'here MainStr contains the contents of the extracted and inflated file..
    
             CALL unzCloseCurrentFile(fH)
    
         NEXT enumZip
    
    
        CALL unzClose(fH)
    Note: The PBZLIB.INC file i have didn't contain the unzClose() function. So add the following function to your Include for it:

    Code:
      FUNCTION _
        unzClose LIB "zlib.dll" ALIAS "unzClose" ( _
          BYVAL fh              AS DWORD _
        ) AS LONG
    Without it, you'll get File Write Access errors, and memory leaks etc etc

    To implement a progress bar, i'm not 100% sure about individual files, but for ZIPs with many files - you could
    make it tick up a progress bar for each file in the For Next loop. There is probably a better way though.


    - Nathan



    [This message has been edited by Nathan Evans (edited August 12, 2001).]

    Comment


    • #3
      Thx Nathan,

      You said you were missing the UnzClose() function then posted the
      zipOpen() function. I assume this was a mistake?

      I have a function:
      DECLARE FUNCTION unzClose LIB "zlib.dll" ALIAS "unzClose" ( BYVAL fh AS DWORD ) AS LONG
      from the previous threads is that what you are referring to?

      So is there any way to get the actual unzip progress from the ZLib
      library?

      My program comes across some large zipped files that take a little
      time and i would like to show the user something other than a
      static progress bar while that is happening.

      I do have 8 or more files, but the effect is of a stepping progress
      bar in 25% increments with pauses in between. i was hoping for
      something smoother.

      ------------------
      Kind Regards
      Mike

      [This message has been edited by Mike Trader (edited August 12, 2001).]

      Comment


      • #4
        Mike, I do it with PBcc.

        Code:
          SUB unzipit
            IF RTRIM$(pth$)="*ABORT*" THEN EXIT SUB
            unzipped=1 'set global so it won't repeat
            DIM S1 AS STRING, S1Len AS LONG, RetVal AS LONG, _
              ff AS LONG, global_info   AS unz_global_info_s, _
              fh AS DWORD
            DIM unz_file_info AS unz_file_info_s, _
              szFileName    AS ASCIIZ * 256, sBuf AS STRING, _
              sBufLen       AS DWORD, _
              sTemp         AS STRING, _
              zipfi         AS zip_fileinfo_s
            DIM szZipFile     AS ASCIIZ * 128
            DIM outFile       AS ASCIIZ * 256
            DIM outfile2      AS ASCIIZ * 256
            DIM ZippedFiles   AS LONG
            DIM LL            AS LONG
            LOCAL currentdir$,xxx$,pcnt&
            LOCAL lType&, lXPos&, lYPos&,lNormalize&
            LOCAL C%
            LOCAL lresult&
            LOCAL test$
            currentdir$=CURDIR$
            IF RIGHT$(currentdir$,1)="\" THEN currentdir$=LEFT$(currentdir$,LEN(currentdir$)-1)
            szZipFile = currentdir$+"\winwu.zip" 'COMMAND$
        
            IF szZipFile = "" THEN
              EXIT SUB 'FUNCTION
            END IF
            IF LEN(DIR$(szZipFile)) = 0 THEN
              PRINT "No Zip File"
              WHILE INKEY$="":WEND
              abort%=1
              EXIT SUB 
            END IF
            RetVal  = unzOpen(szZipFile)
            IF RetVal THEN
              fh = Retval
              RetVal = unzGetGlobalInfo(fh,global_info)
              IF ISFALSE RetVal THEN
        
              ELSE
                 PRINT "Fail #" + FORMAT$(RetVal)
                 WHILE INKEY$="":WEND
              END IF
              RetVal = unzGoToFirstFile(fh)
              FOR ZippedFiles = 1 TO global_info.number_entry
                IF ISFALSE RetVal THEN
                   RetVal = unzGetCurrentFileInfo (fh, _
                     unz_file_info, _
                     szFileName, _
                     SIZEOF(szFileName), _
                     BYVAL 0,_
                     0,_
                     BYVAL 0,_
                     0)
                   IF ISFALSE RetVal THEN
                      sBuf = STRING$(unz_file_info.uncompressed_size,0)
                      RetVal = unzOpenCurrentFile(fh)
                      IF ISFALSE(RetVal) THEN
                        RetVal = unzReadCurrentFile(fh,BYVAL STRPTR(sBuf),LEN(sBuf))
                        unzCloseCurrentFile fh
                        IF RetVal <> 0 THEN
                           ff = FREEFILE
                           LL=INSTR(szFileName,"/")
                           IF LL THEN
                              outFile=MID$(szFileName,1,LL)+"\"+MID$(szFileName,LL+1)
                           ELSE
                              outFile=szFileName
                           END IF
                           IF pth$<>"" THEN
                              outfile2=pth$+"\"+outfile
                              outfile=outfile2
                           END IF
                           OPEN TRIM$(outFile) FOR BINARY AS ff
                           PUT #ff,1,sBuf
                           CLOSE #ff
                        END IF
                      ELSE
                        EXIT FOR
                      END IF
                   ELSE
                      EXIT FOR
                   END IF
                END IF
                RetVal = unzGoToNextFile(fh)
        '*******insert progressbar here
                xxx$=szFileName
                pcnt&=ZippedFiles/global_info.number_entry*100
                ProgressBoxShow lType&, _
                   pcnt&, _ 'lPercent&, _
                   lXPos&, _
                   lYPos&, _
                   xxx$, _     '"Copying Programs to "+pth$+". . .", _ 'sText$, _
                   "Expanding program files", _       'sTitle$, _
                   lNormalize&
        '****end progressbar to here
        
              NEXT ZippedFiles
            END IF
        
          END SUB
        ------------------
        Client Writeup for the CPA

        buffs.proboards2.com

        Links Page

        Comment


        • #5
          Mistake entirely Updated the post. Sorry


          I'm not aware of any functions in zlib that return the extraction progress per each file.


          - Nathan

          Comment


          • #6
            Thx Fred,

            I was hoping there was some feedback from the unzipping engine to
            smoothly increment the progress bar like in aunzip32.dll, but i
            guess not

            This is how Im doing it right now:

            Code:
            #COMPILE EXE "UnZipEx.exe"
            #INCLUDE "PBZLIB.inc"
            
            
            '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ 
            FUNCTION PBMAIN () AS LONG
            
            
            LOCAL StrLen AS LONG, RetVal AS LONG, ff AS LONG, ZippedFile AS LONG
            LOCAL hFile  AS DWORD
            LOCAL aStr   AS STRING, UzFileStr AS STRING, TempStr AS STRING
            LOCAL zZipFile AS ASCIIZ*260, zFileName AS ASCIIZ*260 'or %MAX_PATH if you #INCLUDE "WIN32API.INC" 
            LOCAL global_info AS unz_global_info_s, unz_file_info AS unz_file_info_s, zipfi AS zip_fileinfo_s
            
                zZipFile = "xxxxx.zip" ' change this for a zip file you have
            
            
                hFile = unzOpen(zZipFile)
                IF hFile < 1 THEN MSGBOX zZipFile,,"Bad Zip File" : EXIT FUNCTION
            
            
                RetVal = unzGetGlobalInfo(hFile, global_info)
                IF RetVal THEN MSGBOX "Zip Archive bad, Fail #" + FORMAT$(RetVal) : EXIT FUNCTION
                MSGBOX "Num Files "+FORMAT$(global_info.number_entry)+CHR$(13)+"Comment Lgth "+FORMAT$(global_info.size_comment),,zZipFile
            
            
                RetVal = unzGoToFirstFile(hFile) ' get first file handle
                FOR ZippedFile = 1 TO global_info.number_entry 
                        IF RetVal THEN RetVal = unzGoToNextFile(hFile) : MSGBOX "Can't get a Handle",,zZipFile : ITERATE ' Try next file
                        RetVal = unzGetCurrentFileInfo (hFile, unz_file_info, zFileName, SIZEOF(zFileName), BYVAL 0, 0, BYVAL 0, 0)
            
            
                        IF RetVal THEN MSGBOX "Cannot get File Info",,zFileName : ITERATE
                       'MSGBOX "Compressed size " + FORMAT$(unz_file_info.compressed_size  )+ CHR$(13) + _
                       '       "Extracted  size " + FORMAT$(unz_file_info.uncompressed_size),,zFileName
                        RetVal = unzOpenCurrentFile(hFile)
            
            
                        IF RetVal THEN MSGBOX "File is empty",,zFileName + FORMAT$(RetVal) : ITERATE
                        UzFileStr = SPACE$(unz_file_info.uncompressed_size) ' make a long enough string to hold the file
                        RetVal    = unzReadCurrentFile(hFile, BYVAL STRPTR(UzFileStr), unz_file_info.uncompressed_size)
                        CALL unzCloseCurrentFile(hFile)
            
            
                        IF RetVal = 0 THEN MSGBOX "Uncompressed length = 0",,zFileName : ITERATE
                       'MSGBOX "Size = " + FORMAT$(RetVal/1000) + "k",,"Saving:  " + zFileName ' size of file
                        ff = FREEFILE
                        OPEN zFileName FOR BINARY AS ff
                            PUT$ #ff, UzFileStr
                        CLOSE #ff
                        RetVal = unzGoToNextFile(hFile) ' get next file handle
            
            
                       'DIALOG SEND hDlgProgBar, %PROGBARID, ROUND(ZippedFile/global_info.number_entry *100 ,0), 0 ' update progress bar
            
            
                NEXT ZippedFile
            
            
                CALL unzClose(hFile)
                MSGBOX zZipFile,,"Archive Closed"
            
            
            END FUNCTION
            
            
            '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            Why does this forum engine remove one line of spacing?
            I have to double the spacing lines each time i post code



            [This message has been edited by Mike Trader (edited August 12, 2001).]

            Comment


            • #7
              Mike, to include a "blank" line in a CODE block, ensure the line has at least one space character on it.


              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment

              Working...
              X