Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Compress and Decompress a String
See more Compression Examples
Demonstrates how to compress and decompress a string.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.Compression,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sb: TStringBuilder;
i: Integer;
compress: TCompression;
compressedBytes: TBytes;
compressedBase64: string;
decompressedString: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
sb := TStringBuilder.Create;
for i := 1 to 20 do
begin
sb.Append('This is the original uncompressed string.' + #13#10);
end;
compress := TCompression.Create;
compress.Algorithm := 'deflate';
// Indicate that the utf-8 byte representation of the string should be compressed.
compress.Charset := 'utf-8';
compressedBytes := compress.CompressString(sb.GetAsString());
// If the compressed data is desired in string format, then get the base64 representation of the bytes.
compress.EncodingMode := 'base64';
compressedBase64 := compress.CompressStringENC(sb.GetAsString());
WriteLn('Compressed Bytes as Base64: ' + compressedBase64);
// Now decompress...
decompressedString := compress.DecompressString(compressedBytes);
WriteLn('The original string after decompressing from binary compressed data:');
WriteLn(decompressedString);
// To decompress from Base64...
compress.EncodingMode := 'base64';
decompressedString := compress.DecompressStringENC(compressedBase64);
WriteLn('The original string after decompressing from Base64:');
WriteLn(decompressedString);
sb.Free;
compress.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.