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

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 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.JsonObject,
  Chilkat.Gzip;

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

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

begin
  success := False;

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

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

  //  The Gzip file to examine:
  gzPath := 'example.txt.gz';

  //  Get the metadata information:
  success := gzip.GetGzipInfo(gzPath,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;
  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.