Here is the code from the real project, adopted for the question, so some data is hardcoded:
static void Main(string[] args)
{
Console.WriteLine("Starting. " + Environment.Version);
using (var stream = new FileStream(@"stream_test.txt", FileMode.Open))
{
stream.Position = 0;
// .NET implements Deflate (RFC 1951) but not zlib (RFC 1950),
// so we have to skip the first two bytes.
stream.ReadByte();
stream.ReadByte();
var zipStream = new DeflateStream(stream, CompressionMode.Decompress, true);
// Hardcoded length from real project. In the previous .Net versions this is size of final result
long bytesToRead = (long)262 * 350;
var buffer = new byte[bytesToRead];
int bytesWereRead = zipStream.Read(buffer, 0, (int)bytesToRead);
if (bytesWereRead != bytesToRead)
{
throw new Exception("ZIP stream was not fully decompressed.");
}
Console.WriteLine("Ok");
Console.ReadKey();
}
}
Issue with the decompressing appears not on the each stream so the input file can be found on the GitHub with the project code. https://github.com/dimsa/Net6DeflateStreamIssue/tree/main/DeflateStreamTest
This code works fine on:
- .NET Framework (was tested on 2.0, 3.5, 4)
- .NET Standard 2.1 and
- .NET 5
.NET 6 fails. The decompressed data in the Net 6 has the incorrect length.
Is there any workaround, or should be used another compression libraries?