Sample code for 30+ languages & platforms
AutoIt

Retrieve Metadata from Gzip Data in Memory (BinData)

This example demonstrates how to use the GetGzipInfoBd method to retrieve metadata from Gzip data stored in memory within a BinData object.

The Gzip data is first loaded into the BinData instance. The GetGzipInfoBd method then extracts any available metadata from the Gzip header, including the embedded filename, comment, and optional extra data.

The metadata is returned in a JsonObject. Because these fields are optional, the example checks for the existence of each JSON member using HasMember before retrieving its value.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example demonstrates how to retrieve metadata from Gzip data
; stored in a BinData object.

$oGzip = ObjCreate("Chilkat.Gzip")
$oBd = ObjCreate("Chilkat.BinData")
$oJson = ObjCreate("Chilkat.JsonObject")

; Load a Gzip file into BinData:
$bSuccess = $oBd.LoadFile("example.txt.gz")
If ($bSuccess = False) Then
    ConsoleWrite($oBd.LastErrorText & @CRLF)
    Exit
EndIf

; Get the metadata information from the in-memory Gzip data:
$bSuccess = $oGzip.GetGzipInfoBd($oBd,$oJson)
If ($bSuccess = False) Then
    ConsoleWrite($oGzip.LastErrorText & @CRLF)
    Exit
EndIf

; Output the JSON containing metadata:
ConsoleWrite("Gzip metadata JSON:" & @CRLF)
ConsoleWrite($oJson.Emit() & @CRLF)

; Access individual fields only if they exist:

If ($oJson.HasMember("filename") = True) Then
Local $sFilename = $oJson.StringOf("filename")
    ConsoleWrite("Filename: " & $sFilename & @CRLF)
EndIf

If ($oJson.HasMember("comment") = True) Then
Local $sComment = $oJson.StringOf("comment")
    ConsoleWrite("Comment: " & $sComment & @CRLF)
EndIf

If ($oJson.HasMember("extraData") = True) Then
Local $sExtraData = $oJson.StringOf("extraData")
    ConsoleWrite("ExtraData (Base64): " & $sExtraData & @CRLF)
EndIf