Add compression magic number detection to System.Formats.Tar with helpful error messages#119996
Merged
iremyux merged 39 commits intodotnet:mainfrom Nov 24, 2025
Merged
Add compression magic number detection to System.Formats.Tar with helpful error messages#119996iremyux merged 39 commits intodotnet:mainfrom
iremyux merged 39 commits intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the System.Formats.Tar library by adding compression format detection at the beginning of tar archive reading. When users attempt to read a compressed tar file directly without decompression, the library now provides helpful error messages that identify the compression type and guide users on the appropriate solution.
- Adds magic number detection for common compression formats (GZIP, ZLIB, BZIP2, LZ4, XZ, etc.)
- Provides differentiated error messages for supported vs unsupported compression formats
- Integrates compression checking into both synchronous and asynchronous tar reading workflows
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| TarHeader.Read.cs | Adds compression detection logic and calls to check magic numbers during tar header reading |
| Strings.resx | Adds localized error message templates for supported and unsupported compression detection |
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
This was referenced Oct 6, 2025
…n supported/unsuported formats
This was referenced Oct 15, 2025
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Show resolved
Hide resolved
Open
3 tasks
Contributor
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs:1
- The new
ThrowIfCompressedArchivemethod lacks test coverage. Consider adding tests for each supported compression format (GZIP, BZIP2, XZ, Zstandard, ZIP, 7-Zip, ZLIB) to verify the detection logic and error messages work correctly. Tests should verify that each compression format's magic number is properly detected and throws the expectedInvalidDataExceptionwith the correct format name.
// Licensed to the .NET Foundation under one or more agreements.
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
This was referenced Nov 10, 2025
…der.Read.cs Co-authored-by: Copilot <[email protected]>
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
…der.Read.cs Co-authored-by: Copilot <[email protected]>
This was referenced Nov 13, 2025
rzikm
approved these changes
Nov 24, 2025
src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarHeader.Read.cs
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves error messages when users attempt to read compressed TAR archives. When TAR header parsing fails due to an invalid checksum field, the code now checks if the file might be a compressed archive (GZIP, BZIP2, XZ, ZIP, ZLIB, 7-Zip, or Zstandard) by examining compression magic numbers. If a compression format is detected, it throws a clear, localized error message indicating the specific compression format detected (e.g., "The file appears to be a GZIP archive. TAR format expected.").
Fixes #89056