Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Compressing and Decompressing Files Using Streaming (CompressFile / DecompressFile)

See more Compression Examples

This example demonstrates how to compress a file to a binary format and then restore it using the Chilkat.Compression class. The CompressFile method reads the source file, compresses it using the specified algorithm, and writes the result to a destination file. The DecompressFile method performs the reverse operation, restoring the original file from the compressed data.

Both operations are performed internally in streaming mode, allowing files of any size to be processed efficiently without loading the entire file into memory. The example also includes a simple verification step by comparing file sizes to confirm that the decompressed output matches the original input.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.FileAccess;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  compress: TCompression;
  inputFile: string;
  compressedFile: string;
  decompressedFile: string;
  fac: TFileAccess;
  originalSize: Integer;
  restoredSize: Integer;

begin
  success := False;

  //  This example assumes the Chilkat API has already been unlocked.
  //  See Global Unlock Sample for sample code.

  compress := TCompression.Create;

  //  Use the zlib algorithm (recommended for general use)
  compress.Algorithm := 'zlib';

  //  ------------------------------------------------------------------
  //  Compress a file
  //  ------------------------------------------------------------------

  inputFile := 'c:/temp/example.txt';
  compressedFile := 'c:/temp/example.txt.zlib';

  success := compress.CompressFile(inputFile,compressedFile);
  if (success = False) then
    begin
      WriteLn('Compression failed:');
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  WriteLn('File compressed successfully:');
  WriteLn('  Input:      ' + inputFile);
  WriteLn('  Compressed: ' + compressedFile);

  //  ------------------------------------------------------------------
  //  Decompress the file back to its original form
  //  ------------------------------------------------------------------

  decompressedFile := 'c:/temp/example_restored.txt';

  success := compress.DecompressFile(compressedFile,decompressedFile);
  if (success = False) then
    begin
      WriteLn('Decompression failed:');
      WriteLn(compress.LastErrorText);
      Exit;
    end;

  WriteLn('File decompressed successfully:');
  WriteLn('  Output: ' + decompressedFile);

  //  ------------------------------------------------------------------
  //  Optional: Verify file sizes (basic sanity check)
  //  ------------------------------------------------------------------

  fac := TFileAccess.Create;

  originalSize := fac.FileSize(inputFile);
  restoredSize := fac.FileSize(decompressedFile);

  WriteLn('Original file size:   ' + originalSize);
  WriteLn('Restored file size:   ' + restoredSize);

  if (originalSize = restoredSize) then
    begin
      WriteLn('Sizes match (basic verification successful).');
    end
  else
    begin
      WriteLn('Warning: File sizes differ.');
    end;


  compress.Free;
  fac.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.