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

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

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

begin
success := 0;

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

sbCompressedBase64 := TChilkatStringBuilder.Create(Self);

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

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

bdCompressed := TChilkatBinData.Create(Self);
sbUncompressedChunk := TChilkatStringBuilder.Create(Self);

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;

Memo1.Lines.Add('The base64 encoded compressed text:');
Memo1.Lines.Add(bdCompressed.GetEncoded('base64'));

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