Sample code for 30+ languages & platforms
Unicode C++

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

Unicode C++
#include <CkGzipW.h>
#include <CkBinDataW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example demonstrates how to retrieve metadata from Gzip data
    //  stored in a BinData object.

    CkGzipW gzip;
    CkBinDataW bd;
    CkJsonObjectW json;

    //  Load a Gzip file into BinData:
    success = bd.LoadFile(L"example.txt.gz");
    if (success == false) {
        wprintf(L"%s\n",bd.lastErrorText());
        return;
    }

    //  Get the metadata information from the in-memory Gzip data:
    success = gzip.GetGzipInfoBd(bd,json);
    if (success == false) {
        wprintf(L"%s\n",gzip.lastErrorText());
        return;
    }

    //  Output the JSON containing metadata:
    wprintf(L"Gzip metadata JSON:\n");
    wprintf(L"%s\n",json.emit());

    //  Access individual fields only if they exist:

    if (json.HasMember(L"filename") == true) {
        const wchar_t *filename = json.stringOf(L"filename");
        wprintf(L"Filename: %s\n",filename);
    }

    if (json.HasMember(L"comment") == true) {
        const wchar_t *comment = json.stringOf(L"comment");
        wprintf(L"Comment: %s\n",comment);
    }

    if (json.HasMember(L"extraData") == true) {
        const wchar_t *extraData = json.stringOf(L"extraData");
        wprintf(L"ExtraData (Base64): %s\n",extraData);
    }
    }