Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, BinData, Compression;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
compress: HCkCompression;
bdOut: HCkBinData;
part1: PWideChar;
part2: PWideChar;
part3: PWideChar;
bdIn: HCkBinData;
compressedBase64: PWideChar;
bdDecompressed: HCkBinData;
resultText: PWideChar;

begin
success := False;

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

compress := CkCompression_Create();
CkCompression_putAlgorithm(compress,'zlib');

// This will accumulate the compressed output.
bdOut := CkBinData_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
// ------------------------------------------------------------------

CkCompression_putFirstChunk(compress,True);
CkCompression_putLastChunk(compress,False);

bdIn := CkBinData_Create();
CkBinData_AppendString(bdIn,part1,'utf-8');

success := CkCompression_CompressBd2(compress,bdIn,bdOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkCompression__lastErrorText(compress));
    Exit;
  end;

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

CkCompression_putFirstChunk(compress,False);
CkCompression_putLastChunk(compress,False);

CkBinData_Clear(bdIn);
CkBinData_AppendString(bdIn,part2,'utf-8');

success := CkCompression_CompressBd2(compress,bdIn,bdOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkCompression__lastErrorText(compress));
    Exit;
  end;

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

CkCompression_putFirstChunk(compress,False);
CkCompression_putLastChunk(compress,True);

CkBinData_Clear(bdIn);
CkBinData_AppendString(bdIn,part3,'utf-8');

success := CkCompression_CompressBd2(compress,bdIn,bdOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkCompression__lastErrorText(compress));
    Exit;
  end;

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

Memo1.Lines.Add('Compressed data (base64):');
Memo1.Lines.Add(compressedBase64);

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

bdDecompressed := CkBinData_Create();

CkCompression_putFirstChunk(compress,True);
CkCompression_putLastChunk(compress,True);

success := CkCompression_DecompressBd2(compress,bdOut,bdDecompressed);
if (success = False) then
  begin
    Memo1.Lines.Add(CkCompression__lastErrorText(compress));
    Exit;
  end;

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

Memo1.Lines.Add('Decompressed text:');
Memo1.Lines.Add(resultText);

CkCompression_Dispose(compress);
CkBinData_Dispose(bdOut);
CkBinData_Dispose(bdIn);
CkBinData_Dispose(bdDecompressed);

end;