Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Gzip;

...

procedure TForm1.Button1Click(Sender: TObject);
var
gzip: HCkGzip;
gzPath: PWideChar;
text: PWideChar;

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

gzip := CkGzip_Create();

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

// Uncompress the file and interpret the result as UTF-8 text:
text := CkGzip__uncompressFileToString(gzip,gzPath,'utf-8');
if (CkGzip_getLastMethodSuccess(gzip) = False) then
  begin
    Memo1.Lines.Add(CkGzip__lastErrorText(gzip));
    Exit;
  end;

Memo1.Lines.Add('Uncompressed text:');
Memo1.Lines.Add(text);

CkGzip_Dispose(gzip);

end;