Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoGzip
    Variant vJson
    Handle hoJson
    String sGzPath
    String sFilename
    String sComment
    String sExtraData
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatGzip)) To hoGzip
    If (Not(IsComObjectCreated(hoGzip))) Begin
        Send CreateComObject of hoGzip
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End

    // The Gzip file to examine:
    Move "example.txt.gz" To sGzPath

    // Get the metadata information:
    Get pvComObject of hoJson to vJson
    Get ComGetGzipInfo Of hoGzip sGzPath vJson To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoGzip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Output the JSON containing metadata:
    Showln "Gzip metadata JSON:"
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // Access individual fields only if they exist:

    Get ComHasMember Of hoJson "filename" To bTemp1
    If (bTemp1 = True) Begin
        Get ComStringOf Of hoJson "filename" To sFilename
        Showln "Filename: " sFilename
    End

    Get ComHasMember Of hoJson "comment" To bTemp1
    If (bTemp1 = True) Begin
        Get ComStringOf Of hoJson "comment" To sComment
        Showln "Comment: " sComment
    End

    Get ComHasMember Of hoJson "extraData" To bTemp1
    If (bTemp1 = True) Begin
        Get ComStringOf Of hoJson "extraData" To sExtraData
        Showln "ExtraData (Base64): " sExtraData
    End



End_Procedure