Sample code for 30+ languages & platforms
DataFlex

Add Custom Extra Data to a Gzip File

This example demonstrates how to use the SetExtraData method to include custom binary data in the Gzip header using a hex-encoded string.

The hex string represents the raw bytes to embed in the Gzip metadata. When a compression method is called, this data is included in the Gzip header.

The example also shows how to retrieve the metadata using GetGzipInfo to verify that the extra data was successfully embedded. The retrieved value is returned as a Base64-encoded string.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoGzip
    Variant vJson
    Handle hoJson
    String sInputFile
    String sOutputFile
    String sTemp1

    Move False To iSuccess

    // This example demonstrates how to include custom extra data
    // in the Gzip header when compressing.

    // The extra data is provided as a hex-encoded string.

    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

    // Set extra data using a hex string.
    // This example represents 4 bytes: 00 01 02 03
    Get ComSetExtraData Of hoGzip "00010203" "hex" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoGzip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Compress a file so the extra data is embedded in the Gzip header:
    Move "example.txt" To sInputFile
    Move "example.txt.gz" To sOutputFile

    Get ComCompressFile Of hoGzip sInputFile sOutputFile To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoGzip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Gzip file created with extra data."

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

    Showln "Metadata JSON:"
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1


End_Procedure