Jump to content
Sign in to follow this  
Zinthose

gZip UDF

Recommended Posts

Background

I wrote this as the first piece of my WikiBot Script to permit gZiped http data when communicating with the server. I hated the idea of writing the data to the harddrive just to run 7zip to decompress the data I had in memory. I found zlib but it is a pain to implement. I then discovered that gZip has an Standard IO mode that I can pipe the data directly to the program using the console streams. SWEET!

And thus my gZip UDF is born.

gZip

You can aquire gZip from www.gzip.org or directly at: gzip124xN.zip. Simply extract the gZip.exe to the same folder as the source file.

Note

Although this is fast, I do not recommend this for use in an http server as it is not fast enough.

gZip Example.au3

#include 'ButtonConstants.au3'
#include 'EditConstants.au3'
#include 'GUIConstantsEx.au3'
#include 'StaticConstants.au3'
#include 'WindowsConstants.au3'
#include 'gZip.au3'

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
    $frmExample = GUICreate("Zinthose's gZip UDF Example", 633, 450, 193, 125)
    GUISetOnEvent($GUI_EVENT_CLOSE, "frmExampleClose")
    $lblFluff = GUICtrlCreateLabel("gZip UDF Example", 0, 0, 542, 70)
    GUICtrlSetFont(-1, 42, 800, 0, "Arial")
    GUICtrlSetColor(-1, 0x008000)
    GUICtrlSetResizing(-1, $GUI_DOCKLEFT+$GUI_DOCKTOP+$GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
    $cmdCompress = GUICtrlCreateButton("&Compress", 552, 6, 75, 25, 0)
    GUICtrlSetResizing(-1, $GUI_DOCKRIGHT+$GUI_DOCKTOP+$GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
    GUICtrlSetOnEvent(-1, "cmdCompressClick")
    GUICtrlSetTip(-1, "Click to compress the data .")
    $cmdDecompress = GUICtrlCreateButton("&Decompress", 552, 36, 75, 25, 0)
    GUICtrlSetResizing(-1, $GUI_DOCKRIGHT+$GUI_DOCKTOP+$GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
    GUICtrlSetOnEvent(-1, "cmdDecompressClick")
    $txtData = GUICtrlCreateEdit("", 0, 70, 632, 379)
    GUICtrlSetData(-1, _ExampleCompressed())
    GUICtrlSetResizing(-1, $GUI_DOCKLEFT+$GUI_DOCKRIGHT+$GUI_DOCKBOTTOM+$GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
    GUISetState(@SW_SHOW)
    Dim $frmExample_AccelTable[1][2] = [["^d", $cmdDecompress]]
    GUISetAccelerators($frmExample_AccelTable)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func frmExampleClose()
    Exit
EndFunc

Func cmdCompressClick()
    Local $Data
    $Data = _gzDeflate(GUICtrlRead($txtData))
    If @error Then Return MsgBox(4096, "Compress Error", $gzERROR[@error])
    GUICtrlSetData($txtData, $Data)
EndFunc

Func cmdDecompressClick()
    Local $Data
    $Data = _gzInflate(GUICtrlRead($txtData))
    
    If @error Then Return MsgBox(4096, "Decompress Error", $gzERROR[@error])
    GUICtrlSetData($txtData, BinaryToString($Data))
EndFunc

Func _ExampleCompressed()
    Return _
        "0x1F8B080000000000020B9557CB72E3BA11DDAB4AFF8068331BDDF980789589931B57DD79247655D620D914119300" & _
        "838714FD7DCE6980B2C7B15375ABB4A00874A3D17DFAF4A1F9EE7B31650DDE58B3B8C1BBD394CD10C5C6EBD15C2637" & _
        "8B7930581E24CA8037629F8DF5031FB063BFFB7E966816EBAFB0FF77B1CE675DEE4B74A124730E7359C484D18C219E" & _
        "42CEE2CD1CA2C0F29FCDB90FC340D71E1EE7ABF1765D9D3F1D4D2A78EFF1264F38DBF4167EACC96D79BFFB53A2DB14" & _
        "E8DE8B3989CFD81C37F3F6606C36CBD5F4935D3A443A84103FEF77874F4F2E55DBB34B2E87783C2094A52040BDE721" & _
        "7F688E3BD7A85C3AEA5D7DC033B62EB8D7E7C37E87D0A6A3195CCACEF78CE9C14459441DB86C2E3619E7792BD3CD4C" & _
        "E7BDF4BAC83BC19DD87E3249561B6D16335CE9BA1A5F62282C8FCBC99CA690722D1C3D8D73BDD85FEC49A21E797169" & _
        "92411711580C973B734679746DB2032EAFBE72309D2EEF777F8D61E165BB109E919C127BB1496A92B9C3FCB23DA096" & _
        "EA78660CBF89C7C561DE5E468B6A312FD10ECE020F8B752824C004F7DC60FD49E6844223FB9BF137FC992525A3B5A6" & _
        "7F01B034A39A505F6F92DCFC0C5709F117003766DCC8C492F2CC2C21524DDE5AE20A6801835CDFEF9EA6E8E619C9C0" & _
        "81BF98F1F67C7179322342B428556F50FA182202E3D16694399B4E108ADCED778F01E7030B3E5C8ECC190CE6B9D650" & _
        "6C6E87237713409C8F4871CA212005B2D6E5E37B9833806C6CE67CB4ECC5F700F748A39970F87D9677BC3B0E05EA3E" & _
        "06EB8F28A9F60EAC818AD99CA25C70811850A878873BE1CC7A162EECE10285E7122FF5E8D83A0925360F681B84F6D5" & _
        "0E7641926281CB6B28515BDF9DC5B3BE0FC62DEB5C93FAA5E40A5EDB334082D6A6170260B8296C8D0D4F9503E20B03" & _
        "D40D2359E7F58E8D23CCC74D7C6466F01A85EA2D80AE2703F22424D670A0B703D0A2787C306145F8603D2059436ED9" & _
        "BDB7F159AFA51CF54E8699E07B91151D9F1B8486CD6615891A26B379834C65BB8B32AEAE8E08A7DEF73E944EC1A404" & _
        "BDF0107D482C098ECB166DA5E0C519ECFD50D75F60BC651C7D24BEDDBAF82E0674558D5E57096E0DF164CF42E75937" & _
        "BCF461208F5C42DCC24D2B37A83B2E636EA49544AA7B008BDAE67F38343C3EBCECA8A75A6FA49F708912F1835D67FB" & _
        "E7EAEAAD83AFB0FA987E99EF2FB46DF9965BD57389BEC20A8DBB219D0C00060166BAB6CC66E3303C9159362CDCC68E" & _
        "B6EF85559C4319E87542E835BD1C2B8F85C1BD6E88545AB856214EFB1A6EC5E4C5F901848ADE0603C91FF7BBDF2433" & _
        "9C244013BB8D43D8E69A66FBD2C6BC3EEC1338EB6AE43FDA51CDB89110A26A2C85C18E53DB607ECF10C0506ED27C3B" & _
        "2DC77B44F11D3DB0F5C338974A07264D3A3419A7464B4E6D7A609C5DACA762BB6EDAEF1EFC06992CEB2ACC6D02B988" & _
        "8E6E70047954DDB6A61EEC55E7FCB5C6F92D5400637062F4844E5C52EE5B2C1A73923B86AD4AC697CC33829E0156C2" & _
        "21573C4DAD0BB6405D3D712694B16BB603640F80D97376DA2E00FEFFC3C63FDA72534D5D6124A3F9016001FFFFE2DF" & _
        "FF6FD9A8CDE60F00FC4496D542491790C9CE21BA4E4EC5E9982378310031B8FA6BC5795A7405E9FDA272090CCEC6D5" & _
        "5390766F06E9432CCB96DC3E140F25A699A32269E87D9AA809B00371632854084D01F6EA69627D88CA50542D11E340" & _
        "38B1A6CC099EE8B58688E3D709F541F94ED12DADC37B470CD6225F6CA33733C6A60ABE5179C2820702CA4FC236950D" & _
        "FD572D91CA43BC44669AEE51AB4FC9FC00C08287DCA8F6248ABF17E45537FDA3067EF876D3148AE7AF055A01A2CFC6" & _
        "B3A824D09403D755268DE1329342D94D5473485A841CC2C05967DDA13344334641667DBAA0D0B3CB19DA63114B3AC1" & _
        "00692FC00272D68A7515C9944B17CA5A4FC84E32AF209D2852C7AC4A0DD89EF9772A8BB20C9EA1F0C8EF57A93A1202" & _
        "12655228A76AAA58A9F0E36D7EC6DF175761DE69F7DCC463EACBBCE6CABB2FE87D6B4EC9AE07316D5A0604F036A7DB" & _
        "80A938C09CC9AA1B30E1D8E1ED3CE4AF77F5AC631D1D3A50DA48A698AFAC4FA13C6A1C4AD62A9A5F6D309CC3F0134A" & _
        "5E519BCFCA0EDA492394DFA4ECAC8C659AAC47352A3D60A4FEB4DEE849068440C6DC54013511AE5659FED5C74150EB" & _
        "3102D103242B7B0D02FCB24D027E21BC12DE4669156E415A6405D19B91A7A12ADE31FF7CB85180D47AB64E7B93EBC7" & _
        "8CD6236E6D7E33B6EB4037DD9502943D158C5DB5B9D636C80FAA25A8B85F0F2B6D361082DE3419558EA90E7B5069FF" & _
        "DC18A57EBFFDD9EA078436B0AADAE2274C49A4CC9274AADE2F7E01E7B911E5BB07557381D09F715D2185A5361F5EBF" & _
        "616198AE5A787F4A5A710C687E4574951D36F53DB878121D10DCFD3724B462045F1368B62920F29F0CBF8FE693A691" & _
        "58D8D2F9E967EC2A5DB4D9F9C2BA4D36BCCBBA8F50E0FAE14C6123A235C1E7479A5CA06A4C3AB93D53E57554B0A895" & _
        "FB6BBFE1E126D771F4AD31C14B67747A72FE59A5CB03E281487CE6009779243BCD750D49D5C00A03D31849D4756DE3" & _
        "50B22606233FCB15556DAAF24B7C5B275D1F6F1478C457A6D2381E6C6902E27D175F855F7AB8631F83D5435FA3D5FC" & _
        "173E157C3967100000"
EndFunc

gZip.au3

#Include-once

;###################################
;##   Global Error Lookup Table   ##
;###################################
Global Const $gzERROR[5] = [ _
    "SUCCESS", _
    "Error extracting gzip", _
    "Invalid datastream supplied", _
    "Error executing gZip", _
    "gZip datastream is corrupt" _
]

;#####################################
;##   Install gZip to Temp Folder   ##
Func _gzStartUp();####################
    FileInstall("gzip.exe", @TempDir & "\gzip.exe", 1)
    If @error Then Return SetError(1, @error)
EndFunc

;#################################################
;##   Remove the gZip exe from the temp folder   #
Func _gzShutdown();###############################
    FileDelete(@TempDir & "\gzip.exe")
EndFunc

;####################################
;##   gZip Decompressor function   ##
Func _gzInflate($Data);##############
    Local Const $Params = "-cdf"
    Local $Result
    
    ;## Validate Data format and convert occordingly
        __gzMakeBinary($Data)
        If @error Then Return SetError(@error, @extended, $Result)
    
    ;## Check for gZip File Header
        If BinaryMid($Data, 1, 2) <> "0x1F8B" Then Return SetError(2)
        
    ;## Execute
        $Result = __gzExecute($Data, $Params)
        If @error Then Return SetError(@error, @extended, $Result)
    
    Return $Result
EndFunc

;##################################
;##   gZip Compressor function   ##
Func _gzDeflate($Data);############
    Local Const $Params = "-c9f"
    Local $Result

    ;## Validate Data format and convert occordingly
        __gzMakeBinary($Data)
        If @error Then Return SetError(@error, @extended, $Result)
    
    ;## Execute
        $Result = __gzExecute($Data, $Params)
        If @error Then Return SetError(@error, @extended, $Result)
    
    Return $Result
EndFunc

;######################
;##   INTERNAL USE   ##
;##   Execute gZip   ##
Func __gzExecute(ByRef $Data, $Params)
    Local Const $cmdFormat = '%s /c "%s" %s'
    Local Const $gZip = @TempDir & "\gzip.exe"
    Local $Result = ""
    Local $isTemp = False
    Local $pid, $Command
    
    $Command = StringFormat($cmdFormat, @ComSpec, $gZip, $Params)

    ;## Verify gZip.exe is available
        If Not FileExists($gZip) Then
            _gzStartUp()
            If @error Then Return SetError(@error, @extended, "")
            $isTemp = True
        EndIf

    ;## Open gZip in Standard io mode
        $pid = Run($Command, @TempDir, @SW_HIDE, 0x3)
        If @error Then
            If $isTemp Then _gzShutdown()
            Return SetError(3, @error, "")
        EndIf
    
    ;## Send data to gZip stdIn stream and close the stream
        StdinWrite($pid, $Data)
        StdinWrite($pid)
    
    ;## Read the results from gZip's stdOut stream
        While True      
            StdoutRead($pid, True)
            If @error Then ExitLoop
            If @extended = 0 Then ContinueLoop

            $Result = StdoutRead($pid, False, True)
            ExitLoop            
        WEnd
    
;~      ConsoleWrite("> $Result = " & $Result & @CRLF)
    
    ;## Ensure the gZip process is closed
        StdioClose($pid)
        ProcessClose($pid)
    
    ;## Clean up if this is a temporary instance
        If $isTemp Then _gzShutdown()
    
    ;## Validate Return
        If StringStripWS($Result, 3) = "" Then Return SetError(4, 0, "")
;~          TODO: Add check for gZip Errors
    
    Return $Result
EndFunc

;###############################
;##        INTERNAL USE       ##
;##   Convert data to binary  ##
Func __gzMakeBinary(ByRef $Data)
    If IsString($Data) And StringLeft($Data, 2) = "0x" Then
        $Data = Binary($Data)
        If Not IsBinary($Data) Then Return SetError(2)
    EndIf
    
    If Not IsBinary($Data) Then $Data = StringToBinary($Data)
EndFunc

Enjoy :o

Updated Both scripts... Added error look up table and corrected issue when compressing binary data. The return value of the _gzDeflate is now a binary. If the intended value is a string then simply BinaryToString() it.

Sorry, I forgot to attach the UDF. :D

Edited by Zinthose

Share this post


Link to post
Share on other sites

nice example ^_^

thanks for sharing


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

Is it just me or is there no gZip.au3 attached. >_<

Also it seems that the end of 'gZip Example.au3' is corrupted, as after the _ExampleCompressed EndFunc there is random data.

Share this post


Link to post
Share on other sites

Is it just me or is there no gZip.au3 attached. :D

Also it seems that the end of 'gZip Example.au3' is corrupted, as after the _ExampleCompressed EndFunc there is random data.

Sign, could someone upload the udf? thanks!

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Edit:

Im getting gzip: stdin is a a multi-part gzip file -- get newer version of gzip Error.

But im using newest gzip version. How can I find out what gzip version was used to compress the data?

Edit: obviousely this UDF is broken. if I use gzip.exe to convert manually it works. But then the annoying console pops up all the time :mellow:

Edited by goldenix

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...