Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example demonstrates how to retrieve metadata embedded in a Gzip file.
let gzip = CkoGzip()!
let json = CkoJsonObject()!
// The Gzip file to examine:
var gzPath: String? = "example.txt.gz"
// Get the metadata information:
success = gzip.getInfo(filePath: gzPath, 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!)")
}
}