Skip to content

DeflateStream output differs between .NET Framework and .NET Core #30539

@tmat

Description

@tmat

The compression result differs between .NET Framework and .NET Core. Is it expected? Are there any knobs that can be used to remove the differences?

The following program prints different output:

var text = "\r\nusing System;\r\n\r\nclass C\r\n{\r\n    public static void Main()\r\n    {\r\n        Console.WriteLine();\r\n    }\r\n}\r\n";
var compressedStream = new MemoryStream();

using (var deflater = new DeflateStream(compressedStream, CompressionLevel.Optimal, leaveOpen: true))
using (var writer = new StreamWriter(deflater, Encoding.UTF8, bufferSize: 1024, leaveOpen: true))
{
    writer.Write(text);
}

compressedStream.Position = 0;
Console.WriteLine(BitConverter.ToString(compressedStream.ToArray()));

Full Framework:

7B-BF-7B-FF-FB-DD-FB-79-B9-4A-8B-33-F3-D2-15-82-2B-8B-4B-52-73-AD-79-B9-78-B9-92-73-12-
8B-8B-15-9C-79-B9-AA-79-B9-14-80-A0-A0-34-29-27-33-59-A1-B8-24-B1-04-48-95-E5-67-A6-28-
F8-26-66-E6-69-68-42-A4-A1-AA-40-C0-39-3F-AF-38-3F-27-55-2F-BC-28-B3-24-D5-27-33-2F-55-
43-D3-1A-22-5B-CB-CB-05-44-00

Core:

7A-BF-7B-FF-FB-DD-FB-79-B9-4A-8B-33-F3-D2-15-82-2B-8B-4B-52-73-AD-79-B9-78-B9-92-73-12-
8B-8B-15-9C-79-B9-AA-79-B9-14-80-A0-A0-34-29-27-33-59-A1-B8-24-B1-04-48-95-E5-67-A6-28-
F8-26-66-E6-69-68-42-A4-A1-AA-40-C0-39-3F-AF-38-3F-27-55-2F-BC-28-B3-24-D5-27-33-2F-55-
43-13-68-22-48-A6-96-97-0B-88-00-00-00-00-FF-FF-03-00

Both blobs decompress back to the same value. But their compressed lengths are different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IO.CompressionquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions