Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Gzip Compress In Memory and Base64 Encode

See more Gzip Examples

Demonstrates how to Gzip compress in-memory data and then encode the compressed data to base64.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.BinData,
  Chilkat.Gzip;

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

procedure RunDemo;
var
  success: Boolean;
  gzip: TGzip;
  fileData: TBinData;
  strBase64: string;
  strBase64MultiLine: string;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  gzip := TGzip.Create;

  //  This example will load a file into the fileData object.
  //  Your application might load fileData from other sources..
  fileData := TBinData.Create;
  success := fileData.LoadFile('qa_data/xml/hamlet.xml');
  if (success <> True) then
    begin
      WriteLn('Failed to load file.');
      Exit;
    end;

  //  In-place compress the contents of fileData
  success := gzip.CompressBd(fileData);
  if (success <> True) then
    begin
      WriteLn(gzip.LastErrorText);
      Exit;
    end;

  //  Get the base64 encoded compressed data (in a single line).
  strBase64 := fileData.GetEncoded('base64');
  WriteLn(strBase64);
  WriteLn('--------');

  //  To get the base64 in multiple lines, as it might appear in MIME,
  //  use "base64-mime".
  strBase64MultiLine := fileData.GetEncoded('base64-mime');
  WriteLn(strBase64MultiLine);


  gzip.Free;
  fileData.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.