Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.5.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oGzip
LOCAL oJson
LOCAL cGzPath
LOCAL cFilename
LOCAL cComment
LOCAL cExtraData

nSuccess := 0

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

oGzip := CreateObject("Chilkat.Gzip")
oJson := CreateObject("Chilkat.JsonObject")

//  The Gzip file to examine:
cGzPath := "example.txt.gz"

//  Get the metadata information:
nSuccess := oGzip:GetGzipInfo(cGzPath, oJson)
IF (nSuccess == 0)
    ? oGzip:LastErrorText
    oGzip:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  Output the JSON containing metadata:
? "Gzip metadata JSON:"
? oJson:Emit()

//  Access individual fields only if they exist:

IF (oJson:HasMember("filename") == 1)
    cFilename := oJson:StringOf("filename")
    ? "Filename: " + cFilename
ENDIF

IF (oJson:HasMember("comment") == 1)
    cComment := oJson:StringOf("comment")
    ? "Comment: " + cComment
ENDIF

IF (oJson:HasMember("extraData") == 1)
    cExtraData := oJson:StringOf("extraData")
    ? "ExtraData (Base64): " + cExtraData
ENDIF

oGzip:destroy()
oJson:destroy()