Lua Requires Chilkat v11.5.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example demonstrates how to retrieve metadata embedded in a Gzip file.
local gzip = chilkat.Gzip.new()
local json = chilkat.JsonObject.new()
-- The Gzip file to examine:
local gzPath = "example.txt.gz"
-- Get the metadata information:
local success = gzip:get_gzip_info(gzPath, 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