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

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

Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/1013

State: closed
Merged: No


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.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/1013 **State:** closed **Merged:** No --- 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).
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#1436