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

Transition from BeginCompressStringENC to FirstChunk/LastChunk

Provides instructions for replacing deprecated BeginCompressStringENC method calls with using FirstChunk/LastChunk properties.

Chilkat Delphi ActiveX Downloads

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

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

sbIndex := TChilkatStringBuilder.Create(Self);

//  ----------------------------------------------------------------------------------
//  This is the deprecated way of compressing in chunks using Begin/More/End methods..
//  ----------------------------------------------------------------------------------

for i := 0 to 24 do
  begin
    //  Note: It is possible (and normal) for a BeginCompress* or MoreCompress* method to return
    //  an empty string (or 0 bytes).  When this happens, the input data is not lost.  It will be flushed
    //  in a subsequent call.
    sbIndex.Clear();
    sbIndex.AppendInt(i);
    if (i = 0) then
      begin
        sbCompressedBase64.Append(compress.BeginCompressStringENC(sbIndex.GetAsString()));
      end
    else
      begin
        sbCompressedBase64.Append(compress.MoreCompressStringENC(sbIndex.GetAsString()));
      end;
    sbCompressedBase64.Append(compress.MoreCompressStringENC(': This is a line of data to be compressed...' + #13#10));
  end;

//  Flush any remaining output.
sbCompressedBase64.Append(compress.EndCompressStringENC());

Memo1.Lines.Add('The base64 encoded compressed text:');
Memo1.Lines.Add(sbCompressedBase64.GetAsString());

//  Decompress in one call:
originalText := compress.DecompressStringENC(sbCompressedBase64.GetAsString());
Memo1.Lines.Add(originalText);

//  ----------------------------------------------------------------------------------
//  This is new way of compressing in chunks using FirstChunk and LastChunk properties
//  ----------------------------------------------------------------------------------

sbCompressedBase64.Clear();

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