Skip to content

Commit 7de8a4c

Browse files
authored
Document breaking change: DeflateStream and GZipStream now encode empty payloads (#51002)
1 parent 7e74286 commit 7de8a4c

4 files changed

Lines changed: 86 additions & 7 deletions

File tree

docs/core/compatibility/11.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ If you're migrating an app to .NET 11, the breaking changes listed here might af
1818

1919
| Title | Type of change |
2020
|-------------------------------------------------------------------|-------------------|
21+
| [DeflateStream and GZipStream write headers and footers for empty payload](core-libraries/11/deflatestream-gzipstream-empty-payload.md) | Behavioral change |
2122
| [Environment.TickCount made consistent with Windows timeout behavior](core-libraries/11/environment-tickcount-windows-behavior.md) | Behavioral change |
2223
| [MemoryStream maximum capacity updated and exception behavior changed](core-libraries/11/memorystream-max-capacity.md) | Behavioral change |
2324
| [TAR-reading APIs verify header checksums when reading](core-libraries/11/tar-checksum-validation.md) | Behavioral change |
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: "Breaking change: DeflateStream and GZipStream write headers and footers for empty payload"
3+
description: "Learn about the breaking change in .NET 11 where DeflateStream and GZipStream always write headers and footers to the output, even for empty payloads."
4+
ms.date: 01/09/2026
5+
ai-usage: ai-assisted
6+
---
7+
8+
# DeflateStream and GZipStream write headers and footers for empty payload
9+
10+
<xref:System.IO.Compression.DeflateStream> and <xref:System.IO.Compression.GZipStream> now always write format headers and footers to the output stream, even when no data is written. This ensures the output is a valid compressed stream according to the Deflate and GZip specifications.
11+
12+
## Version introduced
13+
14+
.NET 11 Preview 1
15+
16+
## Previous behavior
17+
18+
Previously, `DeflateStream` and `GZipStream` didn't produce any output if no data was written to the stream. This resulted in an empty output stream when compressing an empty input.
19+
20+
```csharp
21+
using var output = new MemoryStream();
22+
using (var deflate = new DeflateStream(output, CompressionMode.Compress))
23+
{
24+
// No data written.
25+
}
26+
Console.WriteLine(output.Length); // 0
27+
```
28+
29+
## New behavior
30+
31+
Starting in .NET 11, `DeflateStream` and `GZipStream` always write the appropriate headers and footers to the output stream, even when no data is written. This ensures the output contains valid compressed data according to the format specifications.
32+
33+
```csharp
34+
using var output = new MemoryStream();
35+
using (var deflate = new DeflateStream(output, CompressionMode.Compress))
36+
{
37+
// No data written.
38+
}
39+
Console.WriteLine(output.Length); // 2 (for Deflate) or 20 (for GZip).
40+
```
41+
42+
## Type of breaking change
43+
44+
This change is a [behavioral change](../../categories.md#behavioral-change).
45+
46+
## Reason for change
47+
48+
Deflate and GZip compression formats include delimiters for the start and end of compressed data. The correct way to compress an empty payload with these formats includes only those delimiters. Some tools don't correctly react to empty content and always expect the compressed content to contain at least the appropriate header and footer. This change ensures compatibility with such tools and produces properly formatted compressed streams according to the specifications.
49+
50+
## Recommended action
51+
52+
If the previous behavior is desired, special-case empty content to not compress it via `DeflateStream` or `GZipStream`.
53+
54+
```csharp
55+
byte[] dataToCompress = GetData();
56+
57+
if (dataToCompress.Length == 0)
58+
{
59+
// Don't compress empty data.
60+
return Array.Empty<byte>();
61+
}
62+
63+
using var output = new MemoryStream();
64+
using (var deflate = new DeflateStream(output, CompressionMode.Compress))
65+
{
66+
deflate.Write(dataToCompress);
67+
}
68+
return output.ToArray();
69+
```
70+
71+
If you need to distinguish between empty and nonempty compressed streams, track whether any data was written or check the original uncompressed data length before compression.
72+
73+
## Affected APIs
74+
75+
- <xref:System.IO.Compression.DeflateStream?displayProperty=fullName>
76+
- <xref:System.IO.Compression.GZipStream?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ items:
1010
href: 11.md
1111
- name: Core .NET libraries
1212
items:
13+
- name: DeflateStream and GZipStream write headers and footers for empty payload
14+
href: core-libraries/11/deflatestream-gzipstream-empty-payload.md
1315
- name: Environment.TickCount made consistent with Windows timeout behavior
1416
href: core-libraries/11/environment-tickcount-windows-behavior.md
1517
- name: MemoryStream maximum capacity updated and exception behavior changed

docs/standard/io/how-to-compress-and-extract-files.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ title: "How to: Compress and extract files"
33
description: Compress & extract files using System.IO.Compression. See examples using ZipFile, ZipArchive, ZipArchiveEntry, DeflateStream, & GZipStream.
44
ms.date: "08/10/2022"
55
ms.custom: devdivchpfy22
6-
dev_langs:
6+
dev_langs:
77
- "csharp"
88
- "vb"
9-
helpviewer_keywords:
9+
helpviewer_keywords:
1010
- "I/O [.NET], compression"
1111
- "compression"
1212
- "compress files"
@@ -69,9 +69,9 @@ You can also use the <xref:System.IO.Compression.GZipStream> and <xref:System.IO
6969

7070
## See also
7171

72-
- <xref:System.IO.Compression.ZipArchive>
73-
- <xref:System.IO.Compression.ZipFile>
74-
- <xref:System.IO.Compression.ZipArchiveEntry>
75-
- <xref:System.IO.Compression.DeflateStream>
76-
- <xref:System.IO.Compression.GZipStream>
72+
- <xref:System.IO.Compression.ZipArchive>
73+
- <xref:System.IO.Compression.ZipFile>
74+
- <xref:System.IO.Compression.ZipArchiveEntry>
75+
- <xref:System.IO.Compression.DeflateStream>
76+
- <xref:System.IO.Compression.GZipStream>
7777
- [File and stream I/O](index.md)

0 commit comments

Comments
 (0)