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

Decompress Gzip Data from BinData Directly to a File

See more Gzip Examples

This example demonstrates how to use the UncompressBdToFile method to decompress Gzip data stored in a BinData object and write the uncompressed output directly to a file.

The compressed .gz data is first loaded into memory using a BinData object. The UncompressBdToFile method then decompresses the data and writes the result directly to the specified file, without modifying the contents of the BinData object.

This approach is useful when you want to extract compressed data to disk without performing an in-place transformation or managing intermediate buffers.

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.BinData,
  Chilkat.Gzip;

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

procedure RunDemo;
var
  success: Boolean;
  gzip: TGzip;
  bd: TBinData;

begin
  success := False;

  //  This example demonstrates how to decompress Gzip data stored in a BinData object
  //  and write the uncompressed output directly to a file.

  gzip := TGzip.Create;
  bd := TBinData.Create;

  //  Load a .gz file into BinData:
  success := bd.LoadFile('example.txt.gz');
  if (success = False) then
    begin
      WriteLn(bd.LastErrorText);
      Exit;
    end;

  WriteLn('Loaded Gzip data into memory.');

  //  Uncompress the Gzip data and write directly to a file:
  success := gzip.UncompressBdToFile(bd,'example.txt');
  if (success = False) then
    begin
      WriteLn(gzip.LastErrorText);
      Exit;
    end;

  WriteLn('File successfully uncompressed to example.txt');


  gzip.Free;
  bd.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.