question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decompression via ZlibStream for large message doesn't work

See original GitHub issue

The 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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
adamhathcockcommented, Nov 22, 2021
2reactions
adamhathcockcommented, Nov 22, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found