Exception while using TStreamReader with TZipFile

Tuesday, May 13th, 2014 | Dennis D. Spreen | Delphi Programming, Delphi XE6

If you’re using the TStreamReader with a TZDecompressionStream – the stream output by TZipFile.Read(const FileName: string; out Stream: TStream; out LocalHeader: TZipHeader) – you’re likely to experience the exception: EZDecompressionError with the message 'Invalid ZStream operation!'.

TZDecompressionStream allows only three seek methods:

  1. Read forward from beginning (Offset = 0) and (Origin = soBeginning)
  2. Read forward from current position (Offset >= 0) and (Origin = soCurrent)
  3. Read end (Offset = 0) and (Origin = soEnd)

But TStreamReader adjusts the read buffer position with AdjustEndOfBuffer in its FillBuffer method like this:

Even if Rewind is 0 the setting of the stream position invokes a seek method call with Offset > 0 and Origin = soBeginning thus lead to the above exception.

“Solution”
Allow TZDecompressionStream.Seek seeking to the current stream position (if not originating from soEnd):

Change the file System.ZLib.pas to:

This won’t work if your TStreamReader read buffer needs to be rewinded to be sure you have a valid encoding in there. If you’re using ANSI/ASCII files this won’t happen (Rewind is always 0), if you’re using anything else (like utf-8) you have two possibilities: decompress the whole file to a memory stream first or write your own intermediate stream class which allows rewinding to a certain position but doesn’t keep the whole stream in memory like TMemoryStream does.

Tags:

4 Comments to Exception while using TStreamReader with TZipFile

William Meyer
May 13, 2014

Have you filed a report on QC?

Dennis
May 13, 2014

I’m currently preparing a simple test case, but yes I will file a report then.

Jeroen
July 29, 2014

Hi Dennis,

Have you filed a bug yet? We have a lot of “forward only” streams as well and none can be used icw TStreamReader anymore.

Jeroen
July 29, 2014

As it is critical to me and I could not find a bug I filed http://qc.embarcadero.com/wc/qcmain.aspx?d=126530

Leave a comment

About Dennis D. Spreen

I'm an avid programmer working on a variety of platforms in a variety of languages with a wide technical interest.

Search

QR Code

Categories