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

Retrieve Metadata from Gzip Data in Memory (BinData)

This example demonstrates how to use the GetGzipInfoBd method to retrieve metadata from Gzip data stored in memory within a BinData object.

The Gzip data is first loaded into the BinData instance. The GetGzipInfoBd method then extracts any available metadata from the Gzip header, including the embedded filename, comment, and optional extra data.

The metadata 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.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example demonstrates how to retrieve metadata from Gzip data
-- stored in a BinData object.

local gzip = chilkat.Gzip.new()
local bd = chilkat.BinData.new()
local json = chilkat.JsonObject.new()

-- Load a Gzip file into BinData:
local success = bd:load_file("example.txt.gz")
if not success then
    print(bd.last_error_text)
    return
end

-- Get the metadata information from the in-memory Gzip data:
success = gzip:get_gzip_info_bd(bd, json)
if not success then
    print(gzip.last_error_text)
    return
end

-- Output the JSON containing metadata:
print("Gzip metadata JSON:")
print(json:emit())

-- Access individual fields only if they exist:

if json:has_member("filename") then
    local filename = json:string_of("filename")
    print("Filename: " .. filename)
end

if json:has_member("comment") then
    local comment = json:string_of("comment")
    print("Comment: " .. comment)
end

if json:has_member("extraData") then
    local extraData = json:string_of("extraData")
    print("ExtraData (Base64): " .. extraData)
end