简体   繁体   中英

Delphi XE 8 - how to decompress a gzip file?

I'm using Delphi XE 8 and trying to decompress a gzip file. I've copied the following code straight from the Embarcadero website as an example, but I get "EZDecompressionError with message 'data error'.

procedure DecompressGzip(inFileName : string);
var
  LInput, LOutput: TFileStream;
  LUnZip: TZDecompressionStream;

begin
  { Create the Input, Output, and Decompressed streams. }
  LInput := TFileStream.Create(InFileName, fmOpenRead);
  LOutput := TFileStream.Create(ChangeFileExt(InFileName, 'txt'), fmCreate);
  LUnZip := TZDecompressionStream.Create(LInput);

  { Decompress data. }
  LOutput.CopyFrom(LUnZip, 0);
  { Free the streams. }
  LUnZip.Free;
  LInput.Free;
  LOutput.Free;
end;

An example file I'm trying to decompress is located here: http://ftp.nhc.noaa.gov/atcf/aid_public/

Your code is correct, but you've forgot to enable zlib to detect gzip header (by default, the only data format recognized is zlib format). You have to call TDecompressionStream.Create(source: TStream; WindowBits: Integer) overloaded constructor and specify how deep zlib should look into stream for gzip header:

procedure TForm2.FormCreate(Sender: TObject);
var
  FileStream: TFileStream;
  DecompressionStream: TDecompressionStream;
  Strings: TStringList;
begin
  FileStream := TFileStream.Create('aal012015.dat.gz', fmOpenRead);
{
     windowBits can also be greater than 15 for optional gzip decoding.  Add
   32 to windowBits to enable zlib and gzip decoding with automatic header
   detection, or add 16 to decode only the gzip format (the zlib format will
   return a Z_DATA_ERROR).
}
  DecompressionStream := TDecompressionStream.Create(FileStream, 15 + 16);  // 31 bit wide window = gzip only mode

  Strings := TStringList.Create;
  Strings.LoadFromStream(DecompressionStream);

  ShowMessage(Strings[0]);

  { .... }
end;

For further reference look into zlib manual , also this question might be useful.

You are attempting to treat the data as though it were zlib compressed. However that is not compatible with gzip compressed data. Although both formats use the same internal compression algorithm, they have different headers.

To decompress gzip I refer you to this question: How to decode gzip data? Remy's answer there explains how you can use the TIdCompressorZLib from Indy to decompress gzip data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM