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

Chunked Compression Using CompressBd2 with FirstChunk and LastChunk

See more Compression Examples

This example demonstrates how to compress data in multiple segments using the CompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.

The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.

This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.

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.Compression;

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

procedure RunDemo;
var
  success: Boolean;
  compress: TCompression;
  bdOut: TBinData;
  part1: string;
  part2: string;
  part3: string;
  bdIn: TBinData;
  compressedBase64: string;
  bdDecompressed: TBinData;
  resultText: string;

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';

  //  This will accumulate the compressed output.
  bdOut := TBinData.Create;

  //  ------------------------------------------------------------------
  //  Simulate input arriving in chunks.
  //  ------------------------------------------------------------------

  part1 := 'The quick brown fox ';
  part2 := 'jumps over the lazy dog. ';
  part3 := 'This text is split into chunks.';

  //  ------------------------------------------------------------------
  //  Compress the first chunk
  //  ------------------------------------------------------------------

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

  bdIn := TBinData.Create;
  bdIn.AppendString(part1,'utf-8');

  success := compress.CompressBd2(bdIn,bdOut);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  //  ------------------------------------------------------------------
  //  Compress a middle chunk
  //  ------------------------------------------------------------------

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

  bdIn.Clear();
  bdIn.AppendString(part2,'utf-8');

  success := compress.CompressBd2(bdIn,bdOut);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  //  ------------------------------------------------------------------
  //  Compress the final chunk
  //  ------------------------------------------------------------------

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

  bdIn.Clear();
  bdIn.AppendString(part3,'utf-8');

  success := compress.CompressBd2(bdIn,bdOut);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  //  Get the final compressed result as base64 for display
  compressedBase64 := bdOut.GetEncoded('base64');

  WriteLn('Compressed data (base64):');
  WriteLn(compressedBase64);

  //  ------------------------------------------------------------------
  //  Decompress to verify correctness
  //  ------------------------------------------------------------------

  bdDecompressed := TBinData.Create;

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

  success := compress.DecompressBd2(bdOut,bdDecompressed);
  if (success = False) then
    begin
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  //  Convert decompressed bytes back to a string
  resultText := bdDecompressed.GetString('utf-8');

  WriteLn('Decompressed text:');
  WriteLn(resultText);


  compress.Free;
  bdOut.Free;
  bdIn.Free;
  bdDecompressed.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.