Skip to content

GZipStream doesn't write anything to the target stream when there was no write operation #66449

@sschultze

Description

@sschultze

Description

If you create a GZipStream CompressionMode.Compress and don't call the Write method (because there is no data), GZipStream writes nothing to the target stream.

Reproduction Steps

using (var ms = new MemoryStream())
{
    using (var gzipStream = new GZipStream(ms, CompressionMode.Compress, leaveOpen: true))
    {
        // Write nothing.
        // Workaround: gzipStream.Write(new byte[0]);
    }

    Console.WriteLine($"Length: {ms.Length} bytes");
    foreach (var b in ms.ToArray())
    {
        Console.Write(b.ToString("x2"));
    }
    Console.WriteLine();
}

Expected behavior

GZipStream writes at least the GZIP signature (0x1f 0x8b) and some other required stuff to the target stream.

In the reproduction steps, if you enable to workaround, the output seems to be correct:

Length: 20 bytes
1f8b080000000000001303000000000000000000

Actual behavior

Nothing is written to the target stream (0 bytes).

Regression?

I don't know.

Known Workarounds

Call the Write method with an empty byte array (see workaround in reproduction steps).

Configuration

  • .NET 6.0.3
  • x64
  • macOS Monterey 12.0.1

Other information

Node.js' gzipSync function works just fine with an empty buffer:

require('zlib').gzipSync(Buffer.from('', 'hex'))
<Buffer 1f 8b 08 00 00 00 00 00 00 13 03 00 00 00 00 00 00 00 00 00>

Metadata

Metadata

Assignees

Labels

area-System.IO.Compressionhelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions