Exception while using TStreamReader with TZipFile
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:
- Read forward from beginning
(Offset = 0) and (Origin = soBeginning)
- Read forward from current position
(Offset >= 0) and (Origin = soCurrent)
- Read end
(Offset = 0) and (Origin = soEnd)
But TStreamReader
adjusts the read buffer position with AdjustEndOfBuffer
in its FillBuffer
method like this:
1 2 3 |
FStream.Position := FStream.Position - Rewind; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function TZDecompressionStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; [...] begin if (Offset = 0) and (Origin = soBeginning) then [...] else if ((Offset >= 0) and (Origin = soCurrent)) then [...] else if (Offset = 0) and (Origin = soEnd) then [...] else if (Origin = soEnd) or (Offset <> Position) then // Add this line to allow raise EZDecompressionError.Create(SZInvalid); // seeking to actual position result := FZStream.total_out; end; |
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.
4 Comments to Exception while using TStreamReader with TZipFile
Have you filed a report on QC?
May 13, 2014
I’m currently preparing a simple test case, but yes I will file a report then.
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.
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
Search
Recent Posts
- How to compile Lua 5.4.0 for Android as a dynamic library using Android Studio 4
- Please make inline vars usable for production – fix RSP-28892
- How to compile Lua 5.4.0 as a Mac OS X dynamic library
- How to compile Lua 5.4.0 for Linux as a shared library
- How to compile Lua 5.4.0 for Windows
- Daily Wage – a Spigot/Bukkit plugin that pays out a daily wage
- How to compile Lua 5.3.5 for Windows
- Better Collada exporter for Blender with custom properties
- MOS6502-delphi – a MOS 6502 CPU emulator for Delphi
- Pass a multidimensional array as a parameter (with a hidden caveat)
Categories
Tags
Archives
- May 2020
- March 2020
- June 2019
- March 2017
- August 2016
- July 2016
- June 2016
- January 2016
- September 2015
- February 2015
- January 2015
- October 2014
- September 2014
- August 2014
- May 2014
- March 2014
- February 2014
- November 2011
- June 2011
- February 2011
- March 2010
- September 2009
- August 2009
- July 2009
- May 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- February 2008
- June 2007
Delphi Feeds
- How To Live Track The ISS International Space Station December 20, 2024
- SmartInspect 4 Beta 1 Released December 20, 2024
- Mastering FastReport in Delphi: Dynamic File Reporting and Visualization December 19, 2024
- This was 2024 overview December 19, 2024
- Mon bilan GitHub pour l'année 2024 December 19, 2024
- Re-Architecting Legacy Applications: Top Software Transformation December 18, 2024
- The Horror of finding the right database! Part 3 December 18, 2024
- Using TeeChart in Vector Magnetics’ RivCross Software for Precise Directional Drilling December 18, 2024
- Upgrading C++Builder 12.2: Tip #7, bcc64x tips! December 17, 2024
- Enhancing Delphi Code Safety: Best practices, tools, and context December 17, 2024
May 13, 2014