Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
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.StringBuilder,
Chilkat.Compression;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
bdCompressed: TBinData;
compress: TCompression;
sbUncompressedChunk: TStringBuilder;
i: Integer;
bdDecompressed: TBinData;
originalText: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
bdCompressed := TBinData.Create;
compress := TCompression.Create;
compress.Algorithm := 'deflate';
compress.Charset := 'utf-8';
sbUncompressedChunk := TStringBuilder.Create;
compress.FirstChunk := True;
compress.LastChunk := False;
for i := 0 to 24 do
begin
if (i = 24) then
begin
compress.LastChunk := True;
end;
sbUncompressedChunk.Clear();
sbUncompressedChunk.AppendInt(i);
sbUncompressedChunk.Append(': This is a line of data to be compressed...' + #13#10);
compress.CompressSb(sbUncompressedChunk,bdCompressed);
compress.FirstChunk := False;
end;
// Show the compressed data in hex format:
WriteLn('The hex encoded compressed text:');
WriteLn(bdCompressed.GetEncoded('hex'));
// Now decompress in one call. It is important to set both FirstChunk and LastChunk = True
bdDecompressed := TBinData.Create;
compress.FirstChunk := True;
compress.LastChunk := True;
success := compress.DecompressBd2(bdCompressed,bdDecompressed);
if (success = False) then
begin
WriteLn(compress.LastErrorText);
Exit;
end;
originalText := bdDecompressed.GetString('utf-8');
WriteLn(originalText);
bdCompressed.Free;
compress.Free;
sbUncompressedChunk.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.