(JavaScript) 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. Note: This example requires Chilkat v11.5.0 or greater.
var success = false;
// This example demonstrates how to retrieve metadata from Gzip data
// stored in a BinData object.
var gzip = new CkGzip();
var bd = new CkBinData();
var json = new CkJsonObject();
// Load a Gzip file into BinData:
success = bd.LoadFile("example.txt.gz");
if (success == false) {
console.log(bd.LastErrorText);
return;
}
// Get the metadata information from the in-memory Gzip data:
success = gzip.GetGzipInfoBd(bd,json);
if (success == false) {
console.log(gzip.LastErrorText);
return;
}
// Output the JSON containing metadata:
console.log("Gzip metadata JSON:");
console.log(json.Emit());
// Access individual fields only if they exist:
if (json.HasMember("filename") == true) {
var filename = json.StringOf("filename");
console.log("Filename: " + filename);
}
if (json.HasMember("comment") == true) {
var comment = json.StringOf("comment");
console.log("Comment: " + comment);
}
if (json.HasMember("extraData") == true) {
var extraData = json.StringOf("extraData");
console.log("ExtraData (Base64): " + extraData);
}
|