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

Compress String Feed to Base64

See more Compression Examples

This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed data in 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.StringBuilder,
  Chilkat.BinData,
  Chilkat.Compression;

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

procedure RunDemo;
var
  success: Boolean;
  sbCompressedBase64: TStringBuilder;
  compress: TCompression;
  bdCompressed: TBinData;
  sbUncompressedChunk: TStringBuilder;
  i: Integer;
  originalText: string;

begin
  success := False;

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

  sbCompressedBase64 := TStringBuilder.Create;

  compress := TCompression.Create;
  compress.Algorithm := 'deflate';
  compress.Charset := 'utf-8';
  compress.EncodingMode := 'base64';

  compress.FirstChunk := True;
  compress.LastChunk := False;

  bdCompressed := TBinData.Create;
  sbUncompressedChunk := TStringBuilder.Create;

  for i := 0 to 24 do
    begin
      if (i = 24) then
        begin
          compress.LastChunk := True;
        end;

      sbUncompressedChunk.Clear();
      sbUncompressedChunk.AppendInt(i);
      sbUncompressedChunk.Append(': This is a line of data to be compressed...' + #13#10);

      compress.CompressSb(sbUncompressedChunk,bdCompressed);

      compress.FirstChunk := False;
    end;

  WriteLn('The base64 encoded compressed text:');
  WriteLn(bdCompressed.GetEncoded('base64'));

  //  Decompress in one call:
  originalText := compress.DecompressStringENC(bdCompressed.GetEncoded('base64'));
  WriteLn(originalText);


  sbCompressedBase64.Free;
  compress.Free;
  bdCompressed.Free;
  sbUncompressedChunk.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.