[PR #1066] [CLOSED] Fix ArjFactory.IsArchive throwing on non-archive files #1490

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

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1066
Author: @Copilot
Created: 12/8/2025
Status: Closed

Base: masterHead: copilot/fix-archivefactory-issue


📝 Commits (3)

  • 3752462 Initial plan
  • bfa0e4a Fix ArjFactory.IsArchive to not throw on non-archive files
  • e95e996 Remove unused variable from test

📊 Changes

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

View changed files

📝 src/SharpCompress/Factories/ArjFactory.cs (+10 -3)
📝 tests/SharpCompress.Test/ArchiveTests.cs (+27 -0)

📄 Description

Fix ArchiveFactory.IsArchive Regression for ARJ Files

Changes

  • Wrap ArjFactory.IsArchive logic in try-catch block to handle InvalidDataException
  • Add test cases for ArchiveFactory.IsArchive with non-archive and valid archive files
  • Run tests to verify fix (36 tests passed: 24 IsArchive + 12 ARJ)
  • Based on release branch (0.42.0)

Summary

Fixed regression in version 0.42.0 where ArchiveFactory.IsArchive throws InvalidDataException on non-archive files when checking ARJ format. The fix wraps the ArjMainHeader.Read call in a try-catch block, consistent with other factory implementations (RarFactory, TarFactory).

Root Cause

The ArjMainHeader.Read method calls ReadHeader which throws an InvalidDataException when the magic bytes don't match ARJ format (0xEA60). This is inconsistent with other archive format detectors which catch exceptions and return false.

Testing

  • All 24 IsArchive tests pass
  • All 12 ARJ reader tests pass
  • Based on release branch (tag 0.42.0)
  • Fix commit: 11b1616d

Resolves the issue where calling ArchiveFactory.IsArchive on simple non-archive files throws InvalidDataException.

Original prompt

This section details on the original issue you should resolve

<issue_title>Regression: ArchiveFactory.IsArchive throws on non archive files in 0.42.0</issue_title>
<issue_description>Version: 0.42.0

Calling ArchiveFactory.IsArchive on simple non-archive file throws InvalidDataException.
Works fine on 0.41.0

System.IO.InvalidDataException: Not an ARJ file (wrong magic bytes)
   at SharpCompress.Common.Arj.Headers.ArjHeader.ReadHeader(Stream stream) in /_/src/SharpCompress/Common/Arj/Headers/ArjHeader.cs:line 46
   at SharpCompress.Common.Arj.Headers.ArjMainHeader.Read(Stream stream) in /_/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs:line 43
   at SharpCompress.Factories.ArjFactory.IsArchive(Stream stream, String password, Int32 bufferSize) in /_/src/SharpCompress/Factories/ArjFactory.cs:line 32
   at SharpCompress.Archives.ArchiveFactory.IsArchive(Stream stream, Nullable`1& type, Int32 bufferSize) in /_/src/SharpCompress/Archives/ArchiveFactory.cs:line 198
   at SharpCompress.Archives.ArchiveFactory.IsArchive(String filePath, Nullable`1& type, Int32 bufferSize) in /_/src/SharpCompress/Archives/ArchiveFactory.cs:line 177
```</issue_description>

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

<comments>
</comments>


💬 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/1066 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 12/8/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `copilot/fix-archivefactory-issue` --- ### 📝 Commits (3) - [`3752462`](https://github.com/adamhathcock/sharpcompress/commit/3752462c708916053a0f0fd6bea940ec5fa1efc1) Initial plan - [`bfa0e4a`](https://github.com/adamhathcock/sharpcompress/commit/bfa0e4a80844a38e174627970c4bc60e3507b595) Fix ArjFactory.IsArchive to not throw on non-archive files - [`e95e996`](https://github.com/adamhathcock/sharpcompress/commit/e95e996d175ee39ddc8bdc6a65238a014db0b43e) Remove unused variable from test ### 📊 Changes **2 files changed** (+37 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Factories/ArjFactory.cs` (+10 -3) 📝 `tests/SharpCompress.Test/ArchiveTests.cs` (+27 -0) </details> ### 📄 Description ## Fix ArchiveFactory.IsArchive Regression for ARJ Files ### Changes - [x] Wrap `ArjFactory.IsArchive` logic in try-catch block to handle `InvalidDataException` - [x] Add test cases for `ArchiveFactory.IsArchive` with non-archive and valid archive files - [x] Run tests to verify fix (36 tests passed: 24 IsArchive + 12 ARJ) - [x] **Based on release branch (0.42.0)** ### Summary Fixed regression in version 0.42.0 where `ArchiveFactory.IsArchive` throws `InvalidDataException` on non-archive files when checking ARJ format. The fix wraps the `ArjMainHeader.Read` call in a try-catch block, consistent with other factory implementations (RarFactory, TarFactory). ### Root Cause The `ArjMainHeader.Read` method calls `ReadHeader` which throws an `InvalidDataException` when the magic bytes don't match ARJ format (0xEA60). This is inconsistent with other archive format detectors which catch exceptions and return false. ### Testing - ✅ All 24 IsArchive tests pass - ✅ All 12 ARJ reader tests pass - ✅ Based on release branch (tag 0.42.0) - ✅ Fix commit: 11b1616d Resolves the issue where calling `ArchiveFactory.IsArchive` on simple non-archive files throws `InvalidDataException`. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Regression: ArchiveFactory.IsArchive throws on non archive files in 0.42.0</issue_title> > <issue_description>Version: 0.42.0 > > Calling ArchiveFactory.IsArchive on simple non-archive file throws InvalidDataException. > Works fine on 0.41.0 > > ``` > System.IO.InvalidDataException: Not an ARJ file (wrong magic bytes) > at SharpCompress.Common.Arj.Headers.ArjHeader.ReadHeader(Stream stream) in /_/src/SharpCompress/Common/Arj/Headers/ArjHeader.cs:line 46 > at SharpCompress.Common.Arj.Headers.ArjMainHeader.Read(Stream stream) in /_/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs:line 43 > at SharpCompress.Factories.ArjFactory.IsArchive(Stream stream, String password, Int32 bufferSize) in /_/src/SharpCompress/Factories/ArjFactory.cs:line 32 > at SharpCompress.Archives.ArchiveFactory.IsArchive(Stream stream, Nullable`1& type, Int32 bufferSize) in /_/src/SharpCompress/Archives/ArchiveFactory.cs:line 198 > at SharpCompress.Archives.ArchiveFactory.IsArchive(String filePath, Nullable`1& type, Int32 bufferSize) in /_/src/SharpCompress/Archives/ArchiveFactory.cs:line 177 > ```</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes adamhathcock/sharpcompress#1064 <!-- 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:50 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1490