Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.5.0+

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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Gzip,
  Chilkat.BinData,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  gzip: TGzip;
  bd: TBinData;
  json: TJsonObject;
  filename: string;
  comment: string;
  extraData: string;

begin
  success := False;

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

  gzip := TGzip.Create;
  bd := TBinData.Create;
  json := TJsonObject.Create;

  //  Load a Gzip file into BinData:
  success := bd.LoadFile('example.txt.gz');
  if (success = False) then
    begin
      WriteLn(bd.LastErrorText);
      Exit;
    end;

  //  Get the metadata information from the in-memory Gzip data:
  success := gzip.GetGzipInfoBd(bd,json);
  if (success = False) then
    begin
      WriteLn(gzip.LastErrorText);
      Exit;
    end;

  //  Output the JSON containing metadata:
  WriteLn('Gzip metadata JSON:');
  WriteLn(json.Emit());

  //  Access individual fields only if they exist:

  if (json.HasMember('filename') = True) then
    begin
      filename := json.StringOf('filename');
      WriteLn('Filename: ' + filename);
    end;

  if (json.HasMember('comment') = True) then
    begin
      comment := json.StringOf('comment');
      WriteLn('Comment: ' + comment);
    end;

  if (json.HasMember('extraData') = True) then
    begin
      extraData := json.StringOf('extraData');
      WriteLn('ExtraData (Base64): ' + extraData);
    end;


  gzip.Free;
  bd.Free;
  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.