Sample code for 30+ languages & platforms
PureBasic

Retrieve Metadata from a Gzip File

See more Gzip Examples

This example demonstrates how to use the GetGzipInfo method to retrieve metadata embedded within a Gzip file.

The method reads the Gzip header and extracts any available metadata, including the embedded filename, comment, and optional extra data. The extracted information 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. This avoids attempting to access values that may not be present.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ; This example demonstrates how to retrieve metadata embedded in a Gzip file.

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

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

    ; The Gzip file to examine:
    gzPath.s = "example.txt.gz"

    ; Get the metadata information:
    success = CkGzip::ckGetGzipInfo(gzip,gzPath,json)
    If success = 0
        Debug CkGzip::ckLastErrorText(gzip)
        CkGzip::ckDispose(gzip)
        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)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure