Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example demonstrates how to retrieve metadata from Gzip data
// stored in a BinData object.
let gzip = CkoGzip()!
let bd = CkoBinData()!
let json = CkoJsonObject()!
// Load a Gzip file into BinData:
success = bd.loadFile(path: "example.txt.gz")
if success == false {
print("\(bd.lastErrorText!)")
return
}
// Get the metadata information from the in-memory Gzip data:
success = gzip.getInfoBd(bd: bd, json: json)
if success == false {
print("\(gzip.lastErrorText!)")
return
}
// Output the JSON containing metadata:
print("Gzip metadata JSON:")
print("\(json.emit()!)")
// Access individual fields only if they exist:
if json.hasMember(jsonPath: "filename") == true {
var filename: String? = json.string(of: "filename")
print("Filename: \(filename!)")
}
if json.hasMember(jsonPath: "comment") == true {
var comment: String? = json.string(of: "comment")
print("Comment: \(comment!)")
}
if json.hasMember(jsonPath: "extraData") == true {
var extraData: String? = json.string(of: "extraData")
print("ExtraData (Base64): \(extraData!)")
}
}