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

Uncompress a Gzip File to a String

See more Gzip Examples

This example demonstrates how to use the UncompressFileToString method to decompress a Gzip (.gz) file that contains text and return the result as a string.

The method reads the compressed file, decompresses the data, and then converts the resulting bytes into a string using the specified character set (in this case, UTF-8).

It is important to specify the correct character set that matches the original encoding of the text. If the wrong character set is used, the resulting string may contain incorrect or unreadable characters.

This method is convenient when working with compressed text data that needs to be processed directly in memory without writing to a file.

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.Gzip;

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

procedure RunDemo;
var
  gzip: TGzip;
  gzPath: string;
  text: string;

begin
  //  This example demonstrates how to uncompress a Gzip (.gz) file
  //  that contains text and return the result as a string.

  gzip := TGzip.Create;

  //  The Gzip file to be uncompressed:
  gzPath := 'example.txt.gz';

  //  Uncompress the file and interpret the result as UTF-8 text:
  text := gzip.UncompressFileToString(gzPath,'utf-8');
  if (gzip.LastMethodSuccess = False) then
    begin
      WriteLn(gzip.LastErrorText);
      Exit;
    end;

  WriteLn('Uncompressed text:');
  WriteLn(text);


  gzip.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.