Sample code for 30+ languages & platforms
Lazarus Pascal

Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples

This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).


Key Points

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

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.StringBuilder,
  Chilkat.BinData,
  Chilkat.Compression;

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

procedure RunDemo;
var
  success: Boolean;
  compress: TCompression;
  sb: TStringBuilder;
  bdCompressed: TBinData;
  compressedBase64: string;
  bdChunkedOut: TBinData;
  sbPart: TStringBuilder;
  chunkedBase64: string;
  sbDecompressed: TStringBuilder;

begin
  success := False;

  //  This example assumes the Chilkat API has already been unlocked.
  //  See Global Unlock Sample for sample code.

  compress := TCompression.Create;
  compress.Algorithm := 'zlib';

  //  ================================================================
  //  1) Single-call compression (entire data in one call)
  //  ================================================================

  sb := TStringBuilder.Create;
  sb.Append('The quick brown fox jumps over the lazy dog. ');
  sb.Append('This is a simple example using CompressSb.');

  bdCompressed := TBinData.Create;

  //  When both FirstChunk and LastChunk are true (the defaults),
  //  the entire compression happens in a single call.
  compress.FirstChunk := True;
  compress.LastChunk := True;

  success := compress.CompressSb(sb,bdCompressed);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  compressedBase64 := bdCompressed.GetEncoded('base64');

  WriteLn('Single-call compressed (base64):');
  WriteLn(compressedBase64);

  //  ================================================================
  //  2) Chunked compression using FirstChunk / LastChunk
  //  ================================================================

  bdChunkedOut := TBinData.Create;

  //  First chunk
  compress.FirstChunk := True;
  compress.LastChunk := False;

  sbPart := TStringBuilder.Create;
  sbPart.Append('The quick brown fox ');

  success := compress.CompressSb(sbPart,bdChunkedOut);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  //  Middle chunk
  compress.FirstChunk := False;
  compress.LastChunk := False;

  sbPart.Clear();
  sbPart.Append('jumps over the lazy dog. ');

  success := compress.CompressSb(sbPart,bdChunkedOut);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  //  Final chunk
  compress.FirstChunk := False;
  compress.LastChunk := True;

  sbPart.Clear();
  sbPart.Append('This is a chunked CompressSb example.');

  success := compress.CompressSb(sbPart,bdChunkedOut);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  chunkedBase64 := bdChunkedOut.GetEncoded('base64');

  WriteLn('Chunked compressed (base64):');
  WriteLn(chunkedBase64);

  //  ================================================================
  //  3) Decompress to verify correctness
  //  ================================================================

  sbDecompressed := TStringBuilder.Create;

  //  Decompress in a single call (entire data already assembled)
  compress.FirstChunk := True;
  compress.LastChunk := True;

  success := compress.DecompressSb(bdChunkedOut,sbDecompressed);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  WriteLn('Decompressed text:');
  WriteLn(sbDecompressed.GetAsString());


  compress.Free;
  sb.Free;
  bdCompressed.Free;
  bdChunkedOut.Free;
  sbPart.Free;
  sbDecompressed.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.