[PR #1013] [CLOSED] Add XZ/Lzw compression detection and clear error messages for compressed TAR files #1434

Open
opened 2026-01-29 22:20:33 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1013
Author: @Copilot
Created: 11/11/2025
Status: Closed

Base: masterHead: copilot/fix-tar-xz-file-reading-issue


📝 Commits (3)

  • ea8fcb9 Initial plan
  • c8ca687 Add XZ/Lzw support to TarReader and detection for compressed TAR in TarArchive
  • e55af11 Add tests for compressed TAR detection and fix stream disposal

📊 Changes

3 files changed (+215 additions, -2 deletions)

View changed files

📝 src/SharpCompress/Archives/Tar/TarArchive.cs (+146 -2)
📝 src/SharpCompress/Readers/Tar/TarReader.cs (+26 -0)
📝 tests/SharpCompress.Test/Tar/TarArchiveTests.cs (+43 -0)

📄 Description

Opening tar.xz files with TarArchive.Open() failed with "Failed to read TAR header" because the library tried to read compressed data directly without decompression.

Changes

TarReader: Added XZ and Lzw compression detection

  • TarReader.Open() now detects and handles tar.xz and tar.Z files alongside existing GZip/BZip2/ZStandard/LZip support

TarArchive: Added compression detection with clear error messages

  • TarArchive.Open() now detects all compressed TAR formats and throws InvalidFormatException with actionable guidance
  • Compressed TAR streams are non-seekable after decompression, requiring forward-only TarReader API

Usage

For compressed TAR files, use TarReader:

using var stream = new FileStream("file.tar.xz", FileMode.Open, FileAccess.Read);
using var reader = TarReader.Open(stream);
while (reader.MoveToNextEntry())
{
    // Process entries
}

Attempting TarArchive.Open() on compressed files now produces:

InvalidFormatException: Compressed TAR archives (XZ) are not supported by TarArchive.Open(). 
Please use TarReader.Open() instead for forward-only reading of compressed TAR files.
Original prompt

This section details on the original issue you should resolve

<issue_title>Issue reading a specific tar.xz file</issue_title>
<issue_description>Trying to open the archive adressen.tar.xz with the following code did not seem to recognize this file:

using var stream = new FileStream("path/to/locally/stored");
var archive = TarArchive.Open(load);
var entry = archive.Entries.First();

It claims there are not entries. When I open it with 7Zip or the built-in archiving tooling of Windows, I can open the archive.

The used method (according to 7Zip) is LZMA2:26 CRC64. Am I doing something wrong? Or am I facing a (file size maybe, the backed file is just over 1 GB unpacked) issue?</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/1013 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 11/11/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `copilot/fix-tar-xz-file-reading-issue` --- ### 📝 Commits (3) - [`ea8fcb9`](https://github.com/adamhathcock/sharpcompress/commit/ea8fcb93701a3939ed908cf31c850ce4b90c38e6) Initial plan - [`c8ca687`](https://github.com/adamhathcock/sharpcompress/commit/c8ca687dc27f454e98ad7f80d47440e839fd83dc) Add XZ/Lzw support to TarReader and detection for compressed TAR in TarArchive - [`e55af11`](https://github.com/adamhathcock/sharpcompress/commit/e55af11800f34a080c222924c37cbab1ec74b345) Add tests for compressed TAR detection and fix stream disposal ### 📊 Changes **3 files changed** (+215 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Archives/Tar/TarArchive.cs` (+146 -2) 📝 `src/SharpCompress/Readers/Tar/TarReader.cs` (+26 -0) 📝 `tests/SharpCompress.Test/Tar/TarArchiveTests.cs` (+43 -0) </details> ### 📄 Description Opening tar.xz files with `TarArchive.Open()` failed with "Failed to read TAR header" because the library tried to read compressed data directly without decompression. ## Changes **TarReader**: Added XZ and Lzw compression detection - `TarReader.Open()` now detects and handles tar.xz and tar.Z files alongside existing GZip/BZip2/ZStandard/LZip support **TarArchive**: Added compression detection with clear error messages - `TarArchive.Open()` now detects all compressed TAR formats and throws `InvalidFormatException` with actionable guidance - Compressed TAR streams are non-seekable after decompression, requiring forward-only `TarReader` API ## Usage For compressed TAR files, use `TarReader`: ```csharp using var stream = new FileStream("file.tar.xz", FileMode.Open, FileAccess.Read); using var reader = TarReader.Open(stream); while (reader.MoveToNextEntry()) { // Process entries } ``` Attempting `TarArchive.Open()` on compressed files now produces: ``` InvalidFormatException: Compressed TAR archives (XZ) are not supported by TarArchive.Open(). Please use TarReader.Open() instead for forward-only reading of compressed TAR files. ``` <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Issue reading a specific tar.xz file</issue_title> > <issue_description>Trying to open the archive [adressen.tar.xz](https://github.com/LJPc-solutions/Nederlandse-adressen-en-postcodes/raw/refs/heads/main/adressen.tar.xz) with the following code did not seem to recognize this file: > > ``` C# > using var stream = new FileStream("path/to/locally/stored"); > var archive = TarArchive.Open(load); > var entry = archive.Entries.First(); > ``` > It claims there are not entries. When I open it with 7Zip or the built-in archiving tooling of Windows, I can open the archive. > > The used method (according to 7Zip) is `LZMA2:26 CRC64`. Am I doing something wrong? Or am I facing a (file size maybe, the backed file is just over 1 GB unpacked) issue?</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes adamhathcock/sharpcompress#1008 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:20:33 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1434