|
| 1 | +--- |
| 2 | +title: "ZipArchiveEntry names and comments respect UTF8 flag" |
| 3 | +description: Learn about the .NET 9 breaking change in core .NET libraries where ZipArchiveEntry names and comments now respect the UTF8 flag when decoding. |
| 4 | +ms.date: 09/04/2024 |
| 5 | +--- |
| 6 | +# ZipArchiveEntry names and comments respect UTF8 flag |
| 7 | + |
| 8 | +A <xref:System.IO.Compression.ZipArchive> can be created with an <xref:System.Text.Encoding> parameter, which is used to decode the names and comments of entries in the ZIP archive. .NET 7 introduced a regression where this encoding was used by default, with a fallback to the system default code page (UTF8 in .NET Core) if no encoding was supplied. This regression is corrected in .NET 9: if the entry's general purpose bit flags indicate that UTF8 should be used, that is respected. If the UTF8 bit flag is not set, the user-supplied encoding is used (with the existing fallback to the system default code page if none is supplied.) |
| 9 | + |
| 10 | +## Previous behavior |
| 11 | + |
| 12 | +In .NET 7 and .NET 8, if a `ZipArchive` was instantiated with a user-specified `entryNameEncoding` parameter, this encoding was always used when decoding the names and comments of entries in the ZIP archive. `entryNameEncoding` was used even if the entry had the bit set to signify that its name and comment were encoded in UTF8. |
| 13 | + |
| 14 | +## New behavior |
| 15 | + |
| 16 | +Starting in .NET 9, when a ZIP archive entry's name and comment are decoded, its UTF8 bit flag is respected. The user-supplied `entryNameEncoding` parameter is only used to decode the entry's name and comment if this bit flag is unset. |
| 17 | + |
| 18 | +## Version introduced |
| 19 | + |
| 20 | +.NET 9 RC 1 |
| 21 | + |
| 22 | +## Type of breaking change |
| 23 | + |
| 24 | +This change is a [behavioral change](../../categories.md#behavioral-change). |
| 25 | + |
| 26 | +## Reason for change |
| 27 | + |
| 28 | +This change corrects a regression in .NET 7 and .NET 8. It also returns <xref:System.IO.Compression.ZipArchive> to compliance with the ZIP file format specification, sections 4.4.4 and appendix D. |
| 29 | + |
| 30 | +## Recommended action |
| 31 | + |
| 32 | +If your code passes an encoding to the [ZipArchive constructor](xref:System.IO.Compression.ZipArchive.%23ctor(System.IO.Stream,System.IO.Compression.ZipArchiveMode,System.Boolean,System.Text.Encoding)), be aware that this encoding isn't respected in all situations. It will only be used if the entry's UTF8 bit is not set. |
| 33 | + |
| 34 | +If you're using <xref:System.IO.Compression.ZipArchive> to parse ZIP entries with names encoded in non-UTF8 format (but which have the UTF8 bit flag set), you will no longer be able to do so. The previous behavior was a bug. |
| 35 | + |
| 36 | +## Affected APIs |
| 37 | + |
| 38 | +- <xref:System.IO.Compression.ZipArchive.%23ctor(System.IO.Stream,System.IO.Compression.ZipArchiveMode,System.Boolean,System.Text.Encoding)> |
| 39 | +- <xref:System.IO.Compression.ZipFile.ExtractToDirectory(System.IO.Stream,System.String,System.Text.Encoding,System.Boolean)?displayProperty=fullName> |
| 40 | +- <xref:System.IO.Compression.ZipFile.ExtractToDirectory(System.String,System.String,System.Text.Encoding,System.Boolean)?displayProperty=fullName> |
| 41 | +- <xref:System.IO.Compression.ZipFile.ExtractToDirectory(System.String,System.String,System.Text.Encoding)?displayProperty=fullName> |
| 42 | +- <xref:System.IO.Compression.ZipFile.ExtractToDirectory(System.IO.Stream,System.String,System.Text.Encoding)?displayProperty=fullName> |
| 43 | +- <xref:System.IO.Compression.ZipFile.Open(System.String,System.IO.Compression.ZipArchiveMode,System.Text.Encoding)?displayProperty=fullName> |
0 commit comments