Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

Compress Text Feed to Binary

See more Compression Examples

This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed binary data.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
bdCompressed: TChilkatBinData;
compress: TChilkatCompression;
sbUncompressedChunk: TChilkatStringBuilder;
i: Integer;
bdDecompressed: TChilkatBinData;
originalText: WideString;

begin
success := 0;

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

bdCompressed := TChilkatBinData.Create(Self);

compress := TChilkatCompression.Create(Self);
compress.Algorithm := 'deflate';
compress.Charset := 'utf-8';

sbUncompressedChunk := TChilkatStringBuilder.Create(Self);

compress.FirstChunk := 1;
compress.LastChunk := 0;

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

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

    compress.CompressSb(sbUncompressedChunk.ControlInterface,bdCompressed.ControlInterface);

    compress.FirstChunk := 0;
  end;

//  Show the compressed data in hex format:
Memo1.Lines.Add('The hex encoded compressed text:');
Memo1.Lines.Add(bdCompressed.GetEncoded('hex'));

//  Now decompress in one call.  It is important to set both FirstChunk and LastChunk = 1
bdDecompressed := TChilkatBinData.Create(Self);
compress.FirstChunk := 1;
compress.LastChunk := 1;
success := compress.DecompressBd2(bdCompressed.ControlInterface,bdDecompressed.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(compress.LastErrorText);
    Exit;
  end;

originalText := bdDecompressed.GetString('utf-8');
Memo1.Lines.Add(originalText);