Unicode C
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
#include <C_CkGzipW.h>
#include <C_CkBinDataW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkGzipW gzip;
HCkBinDataW bd;
HCkJsonObjectW json;
const wchar_t *filename;
const wchar_t *comment;
const wchar_t *extraData;
success = FALSE;
// This example demonstrates how to retrieve metadata from Gzip data
// stored in a BinData object.
gzip = CkGzipW_Create();
bd = CkBinDataW_Create();
json = CkJsonObjectW_Create();
// Load a Gzip file into BinData:
success = CkBinDataW_LoadFile(bd,L"example.txt.gz");
if (success == FALSE) {
wprintf(L"%s\n",CkBinDataW_lastErrorText(bd));
CkGzipW_Dispose(gzip);
CkBinDataW_Dispose(bd);
CkJsonObjectW_Dispose(json);
return;
}
// Get the metadata information from the in-memory Gzip data:
success = CkGzipW_GetGzipInfoBd(gzip,bd,json);
if (success == FALSE) {
wprintf(L"%s\n",CkGzipW_lastErrorText(gzip));
CkGzipW_Dispose(gzip);
CkBinDataW_Dispose(bd);
CkJsonObjectW_Dispose(json);
return;
}
// Output the JSON containing metadata:
wprintf(L"Gzip metadata JSON:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(json));
// Access individual fields only if they exist:
if (CkJsonObjectW_HasMember(json,L"filename") == TRUE) {
filename = CkJsonObjectW_stringOf(json,L"filename");
wprintf(L"Filename: %s\n",filename);
}
if (CkJsonObjectW_HasMember(json,L"comment") == TRUE) {
comment = CkJsonObjectW_stringOf(json,L"comment");
wprintf(L"Comment: %s\n",comment);
}
if (CkJsonObjectW_HasMember(json,L"extraData") == TRUE) {
extraData = CkJsonObjectW_stringOf(json,L"extraData");
wprintf(L"ExtraData (Base64): %s\n",extraData);
}
CkGzipW_Dispose(gzip);
CkBinDataW_Dispose(bd);
CkJsonObjectW_Dispose(json);
}