Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkGzip.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    gzip.i = CkGzip::ckCreate()
    If gzip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load a Gzip file into BinData:
    success = CkBinData::ckLoadFile(bd,"example.txt.gz")
    If success = 0
        Debug CkBinData::ckLastErrorText(bd)
        CkGzip::ckDispose(gzip)
        CkBinData::ckDispose(bd)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; Get the metadata information from the in-memory Gzip data:
    success = CkGzip::ckGetGzipInfoBd(gzip,bd,json)
    If success = 0
        Debug CkGzip::ckLastErrorText(gzip)
        CkGzip::ckDispose(gzip)
        CkBinData::ckDispose(bd)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; Output the JSON containing metadata:
    Debug "Gzip metadata JSON:"
    Debug CkJsonObject::ckEmit(json)

    ; Access individual fields only if they exist:

    If CkJsonObject::ckHasMember(json,"filename") = 1
        filename.s = CkJsonObject::ckStringOf(json,"filename")
        Debug "Filename: " + filename
    EndIf

    If CkJsonObject::ckHasMember(json,"comment") = 1
        comment.s = CkJsonObject::ckStringOf(json,"comment")
        Debug "Comment: " + comment
    EndIf

    If CkJsonObject::ckHasMember(json,"extraData") = 1
        extraData.s = CkJsonObject::ckStringOf(json,"extraData")
        Debug "ExtraData (Base64): " + extraData
    EndIf



    CkGzip::ckDispose(gzip)
    CkBinData::ckDispose(bd)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure