Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Compression with ZLib as Resource and LoadLibrary

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

  • Compression with ZLib as Resource and LoadLibrary

    I've been meaning to do this for quite a while and finally got around to it. Using ZLib.dll without having to distribute it as a separate file by embedding it as RCDATA iin the .exe.

    '
    Code:
    #COMPILE EXE
    #DIM ALL
    #RESOURCE RCDATA zl "Zlib.dll"
    
    #INCLUDE "Win32API.inc"
    
    FUNCTION PBMAIN () AS LONG
        LOCAL strT AS STRING
        LOCAL ff AS LONG
        LOCAL lOrigSize AS LONG
        LOCAL strFile AS STRING
        strFile = "TestFile"
        IF ISFILE("Comp_" & strFile) THEN KILL "Comp_" & strFile
        IF ISFILE("Decomp_" & strFile) THEN KILL "Decomp_" & strFile
    
        IF NOT ZLibInit THEN
            ? "Problem extracting compresssor"
            EXIT FUNCTION
        END IF
    
        OPEN  strFile FOR BINARY AS #ff
        GET$ #ff, LOF(#ff), strT
        CLOSE #ff
        lOrigSize = LEN(strT)
    
        strT = compress(strT)
        ? strFile & " Original size: "& FORMAT$(lOrigSize,"0,") & "  Compressed: " & FORMAT$(LEN(strT),"0,")
    
        OPEN "Comp_" & strFile  FOR BINARY AS #ff
        PUT$ #ff,strT
        CLOSE #ff
        strT = uncompress(strT)
        ? "Decompressed size " & FORMAT$(LEN(StrT),"0,")
        OPEN "Decomp_" & strFile FOR BINARY AS #ff
        PUT$ #ff,strT
        CLOSE #ff
        ? "Done"
        ZLibCLose
    END FUNCTION
    
    '====== Begin ZLib functions ==========================
    DECLARE FUNCTION ZlibCompress (compr AS ANY, comprLen AS DWORD, buf AS ANY, BYVAL buflen AS DWORD)AS LONG
    DECLARE FUNCTION ZlibUncompress (uncompr AS ANY, uncomprLen AS DWORD, compr AS ANY, BYVAL lcompr AS DWORD)AS LONG
    
    FUNCTION ZlibInit() AS LONG
        GLOBAL hLib,hComp,hDeComp AS DWORD
        GLOBAL DLLLoc AS STRINGZ * %MAX_PATH
        LOCAL ff AS LONG
        GetTempPath(%MAX_PATH, DLLLoc)
        DLLLoc += "\mylib.dll"
        ff = FREEFILE
        IF ISFILE(DLLLoc) THEN KILL DLLLoc
        OPEN DLLLoc FOR BINARY AS #ff
        PUT$ #ff, RESOURCE$(RCDATA,"zl")
        CLOSE #ff
        hLib = LoadLibrary(DLLLoc)
        IF hLib = 0 THEN
            FUNCTION = 0
            EXIT FUNCTION
        END IF
        hComp = GetProcAddress(hLib, "compress")
        hDeComp = GetProcAddress(hLib, "uncompress")
        FUNCTION = -1
    END FUNCTION
    
    FUNCTION ZLibClose() AS LONG
        FreeLibrary hLib
        KILL DLLLoc
    END FUNCTION
    
    FUNCTION compress (rsTextIn AS STRING) AS STRING
       LOCAL wsTextOut AS STRING, llDo AS LONG,lspIn,lspOut AS STRING PTR
       lspIn = VARPTR(rsTextIn):lspOut = VARPTR(wsTextOut)
       @lspOut = SPACE$(LEN(@lspIn) * 1.2 + 12)
       llDo = LEN(@lspOut)
       CALL DWORD hComp USING ZLibCOmpress(BYVAL STRPTR(@lspOut), llDo, BYVAL STRPTR(@lspIn), LEN(@lspIn))
       @lspOut = MKDWD$(LEN(@lspIn)) & LEFT$(@lspOut, llDo)
       FUNCTION = @lspOut
    END FUNCTION
    
    FUNCTION uncompress(rsTextIn AS STRING) AS STRING
       LOCAL wsTextOut AS STRING, lspIn,lspOut AS STRING PTR
       lspIn = VARPTR(rsTextIn)
       lspOut = VARPTR(wsTextOut)
       @lspOut = SPACE$(CVDWD(@lspIn))
       CALL DWORD hDeComp USING ZLibUnCOmpress( BYVAL STRPTR(@lspOut),LEN(@lspOut),BYVAL STRPTR(@lspIn)+4,LEN(@lspIn)-4)
       FUNCTION [email protected]
    END FUNCTION
    '================= End ZLib functions ======================
    '
Working...
X