Unable to decompress a simple gzip file [SOLVED] lazarus / 21-General ---------------------------------------------------------------------- QuinnMartin · 2024-07-08 09:31 I have been struggling through most of this evening trying to figure out how to decompress a simple gzip file.  I am stuck. I know the source file is valid because it decompresses fine in gzip and when I run gzip -v -t (test) it reports that it's OK.  Below is the code I ended up with, but it triggers exceptions saying there's a data error.  I have attached the .gz test file (I get failures with other .gz files too). I really prefer to use native Lazarus code (instead of zlib, as I understand that gets into DLLs). Here is the code I ended up with.  Maybe I am not approaching this right, idk. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  zstream, paszlib; procedure DecompressGZipFile(const SourceFileName, TargetDirectory: String);const  BufferSize = 16384;var  SourceFile: TFileStream;  TargetFile: TFileStream;  DecompressionStream: TDecompressionStream;  Buffer: array[0..BufferSize-1] of Byte;  BytesRead: Integer;  TargetFileName: String;begin  SourceFile := nil;  TargetFile := nil;  DecompressionStream := nil;   try    SourceFile := TFileStream.Create(SourceFileName, fmOpenRead or fmShareDenyWrite);   TargetFileName := ExtractFileName(SourceFileName);    if LowerCase(ExtractFileExt(TargetFileName)) = '.gz' then      TargetFileName := ChangeFileExt(TargetFileName, '');    TargetFileName := IncludeTrailingPathDelimiter(TargetDirectory) + TargetFileName;   TargetFile := TFileStream.Create(TargetFileName, fmCreate);     DecompressionStream := TDecompressionStream.Create(SourceFile);     repeat      BytesRead := DecompressionStream.Read(Buffer, BufferSize);      if BytesRead > 0 then        TargetFile.WriteBuffer(Buffer, BytesRead);    until BytesRead = 0;    finally    DecompressionStream.Free;    SourceFile.Free;    TargetFile.Free;  end;end; ---------------------------------------------------------------------- marcov · 2024-07-08 11:34 Use TGZipDecompressionStream  instead of TDecompressionStream  ? ---------------------------------------------------------------------- QuinnMartin · 2024-07-09 00:03 I did not see where to use TGZipDecompressionStream. However I did suddenly solve it using zstream (code is below).  This is decompressing the files correctly. I hope this helps someone.  I also welcome suggestions from the experts for improving it. Very odd that paszlib wouldn't work correctly with it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  zstream;      function DecompressGZipFile(const SourceFileName, TargetDirectory: String): Boolean;var  Decompressor: TGZFileStream;  TargetFile: TFileStream;  Buffer: array[0..16383] of Byte;  BytesRead: Integer;  TargetFileName: String;begin  Result := False;  Decompressor := nil;  TargetFile := nil;   try    TargetFileName := ExtractFileName(SourceFileName);    if LowerCase(ExtractFileExt(TargetFileName)) = '.gz' then      TargetFileName := ChangeFileExt(TargetFileName, '');    TargetFileName := IncludeTrailingPathDelimiter(TargetDirectory) + TargetFileName;   Decompressor := TGZFileStream.Create(SourceFileName, gzOpenRead);    TargetFile := TFileStream.Create(TargetFileName, fmCreate);     repeat      BytesRead := Decompressor.Read(Buffer, SizeOf(Buffer));    if BytesRead > 0 then        TargetFile.WriteBuffer(Buffer, BytesRead);    until BytesRead = 0;     Result := True;  except    on E: Exception do    begin      // Handle or log the error as needed    end;  end;   FreeAndNil(Decompressor);  FreeAndNil(TargetFile);end;