✅BinaryReader reads garbage when reading from compressed stream
This is a weird one - I think. So I have a rather complex data type that I write to and load from disk. I'm using a
ZipArchive
to get some data compression.
Writing basically looks like this:
and reading like this:
The code above works fine as long as I have NoCompression
specified. I can successfully write and then read the file back. However, as soon as I change the compression level to anything else (like Optimal
), I get a random error in the middle of reading with reader
where it tries to read the length of an array and it reads it as a billion or something (where it should be like 100). I've gone through it with the debugger and all data up to that point is read correctly. I also don't think this is a new code path it runs into the first time. The same code has been called multiple times already before running into the error.
(more details in thread)1 Reply
I've also gone ahead and picked the same object and serialized it to disk twice. The first time I used
NoCompression
, the second time I used Optimal
compression. Then I took both zip files and extracted them with 7zip and compared their content. They are both identical down to the byte.
Another interesting part is if I buffer the stream on deserialization to a MemoryStream
it suddenly works even with compression. If I change the reader code from above to
it works and I can load the data successfully.
Any idea what's going on here?
I'll try to create a minimal example, but it might take a while since not even all of my objects produce this error...
Alright I found the issue and it was of course not a problem with either BinaryReader
nor ZipArchive
. At one point I read the data from the reader into a span and I didn't check that the amount read is actually the whole span. I changed
to
and now it works...