Sample code for 30+ languages & platforms
C++

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 C++ Downloads

C++
#include <CkGzip.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example demonstrates how to retrieve metadata embedded in a Gzip file.

    CkGzip gzip;
    CkJsonObject json;

    //  The Gzip file to examine:
    const char *gzPath = "example.txt.gz";

    //  Get the metadata information:
    success = gzip.GetGzipInfo(gzPath,json);
    if (success == false) {
        std::cout << gzip.lastErrorText() << "\r\n";
        return;
    }

    //  Output the JSON containing metadata:
    std::cout << "Gzip metadata JSON:" << "\r\n";
    std::cout << json.emit() << "\r\n";

    //  Access individual fields only if they exist:

    if (json.HasMember("filename") == true) {
        const char *filename = json.stringOf("filename");
        std::cout << "Filename: " << filename << "\r\n";
    }

    if (json.HasMember("comment") == true) {
        const char *comment = json.stringOf("comment");
        std::cout << "Comment: " << comment << "\r\n";
    }

    if (json.HasMember("extraData") == true) {
        const char *extraData = json.stringOf("extraData");
        std::cout << "ExtraData (Base64): " << extraData << "\r\n";
    }
    }