Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Gzip
oleobject loo_Json
string ls_InputFile
string ls_OutputFile

li_Success = 0

// 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.

loo_Gzip = create oleobject
li_rc = loo_Gzip.ConnectToNewObject("Chilkat.Gzip")
if li_rc < 0 then
    destroy loo_Gzip
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

// Set extra data using a hex string.
// This example represents 4 bytes: 00 01 02 03
li_Success = loo_Gzip.SetExtraData("00010203","hex")
if li_Success = 0 then
    Write-Debug loo_Gzip.LastErrorText
    destroy loo_Gzip
    destroy loo_Json
    return
end if

// Compress a file so the extra data is embedded in the Gzip header:
ls_InputFile = "example.txt"
ls_OutputFile = "example.txt.gz"

li_Success = loo_Gzip.CompressFile(ls_InputFile,ls_OutputFile)
if li_Success = 0 then
    Write-Debug loo_Gzip.LastErrorText
    destroy loo_Gzip
    destroy loo_Json
    return
end if

Write-Debug "Gzip file created with extra data."

// (Optional) Retrieve the metadata to verify:
li_Success = loo_Gzip.GetGzipInfo(ls_OutputFile,loo_Json)
if li_Success = 0 then
    Write-Debug loo_Gzip.LastErrorText
    destroy loo_Gzip
    destroy loo_Json
    return
end if

Write-Debug "Metadata JSON:"
Write-Debug loo_Json.Emit()


destroy loo_Gzip
destroy loo_Json