Decompression via ZlibStream for large message doesn't work
See original GitHub issueThe below test triggers the following exception in Decompress
step for a message bigger than 131072 letters on latest nuget versions. Messages with length less or equal to 131072 letters are decompressed fine. I’ve checked ‘0.29.0’, ‘0.28.0’, ‘0.27.0’:
Message:
SharpCompress.Compressors.Deflate.ZlibException : Bad state (incorrect data check)
Stack Trace:
InflateManager.Inflate(FlushType flush)
ZlibBaseStream.Read(Byte[] buffer, Int32 offset, Int32 count)
Stream.CopyTo(Stream destination, Int32 bufferSize)
CompressorsTests.Decompress(Stream input, Stream output) line 135
CompressorsTests.Zlib_should_read_the_previously_written_message() line 104
but it works fine for versions <= 0.26.0
[Fact]
public void Zlib_should_read_the_previously_written_message()
{
var message = new string('a', 131073); // 131073 causes the failure, but 131072 (-1) doesn't
var bytes = Encoding.ASCII.GetBytes(message);
using (var inputStream = new MemoryStream(bytes))
{
using (var compressedStream = new MemoryStream())
using (var byteBufferStream = new BufferedStream(inputStream)) // System.IO
{
Compress(byteBufferStream, compressedStream, compressionLevel: 1);
compressedStream.Position = 0;
using (var decompressedStream = new MemoryStream())
{
Decompress(compressedStream, decompressedStream);
byteBufferStream.Position = 0;
var result = Encoding.ASCII.GetString(GetBytes(byteBufferStream));
result.Should().Be(message);
}
}
}
}
public void Compress(Stream input, Stream output, int compressionLevel)
{
using (var zlibStream = new ZlibStream(new NonDisposingStream(output), CompressionMode.Compress, (CompressionLevel)compressionLevel))
{
zlibStream.FlushMode = FlushType.Sync;
input.CopyTo(zlibStream);
}
}
public void Decompress(Stream input, Stream output)
{
using (var zlibStream = new ZlibStream(new NonDisposingStream(input), CompressionMode.Decompress))
{
zlibStream.CopyTo(output);
}
}
byte[] GetBytes(BufferedStream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return bytes;
}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Decompressing a MemoryStream using Zlib
I am writing software that deals with a large collection of files that contain zlib compressed data in different sections of the file...
Read more >Re^12: Can't decompress zlib compression stream with ...
Firstly, the code doesn't explicitly handle the end of the compressed data stream at all. In this section of code you are using...
Read more >Compression and Decompression of Data Using Zlib in ...
When working with large systems or moving large amounts of data around, it is helpful to be able to compress/decompress the data.
Read more >Full restore zlib decompression error -3
Failed to download disk, Agen...."( cannot see the rest of the message ). Doesn't matter which restore point, it always fails, even went...
Read more >ZLibStream Class (System.IO.Compression)
Initializes a new instance of the ZLibStream class by using the specified stream, compression mode, and whether to leave the stream open. Properties....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Should be on nuget soon https://www.nuget.org/packages/SharpCompress/0.30.1
I’ve made a PR with the test and identified fix: https://github.com/adamhathcock/sharpcompress/pull/624
Eyeball it and I’ll merge it and push to nuget