Compare commits

...

3 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
e95e996d17 Remove unused variable from test
Address code review feedback: remove unused testFile variable

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
2025-12-08 09:26:28 +00:00
copilot-swe-agent[bot]
bfa0e4a808 Fix ArjFactory.IsArchive to not throw on non-archive files
- Wrap ArjMainHeader.Read call in try-catch block
- Return false on InvalidDataException instead of propagating
- Add tests for ArchiveFactory.IsArchive with non-archive files
- Fixes regression in 0.42.0 where IsArchive threw exceptions

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
2025-12-08 09:18:11 +00:00
copilot-swe-agent[bot]
3752462c70 Initial plan 2025-12-08 09:09:59 +00:00
2 changed files with 37 additions and 3 deletions

View File

@@ -28,12 +28,19 @@ namespace SharpCompress.Factories
int bufferSize = ReaderOptions.DefaultBufferSize
)
{
var arjHeader = new ArjMainHeader(new ArchiveEncoding());
if (arjHeader.Read(stream) == null)
try
{
var arjHeader = new ArjMainHeader(new ArchiveEncoding());
if (arjHeader.Read(stream) == null)
{
return false;
}
return true;
}
catch
{
return false;
}
return true;
}
public IReader OpenReader(Stream stream, ReaderOptions? options) =>

View File

@@ -654,4 +654,31 @@ public class ArchiveTests : ReaderTests
Assert.Equal(3, archive.Entries.Count());
}
}
[Fact]
public void ArchiveFactory_IsArchive_NonArchiveFile_ShouldReturnFalse()
{
// Test that ArchiveFactory.IsArchive returns false instead of throwing
// when called on a non-archive file (regression test for issue #1060)
using (var stream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 }))
{
var result = ArchiveFactory.IsArchive(stream, out var type);
Assert.False(result);
Assert.Null(type);
}
}
[Fact]
public void ArchiveFactory_IsArchive_ValidArchive_ShouldReturnTrue()
{
// Test that ArchiveFactory.IsArchive correctly identifies valid archives
var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.noEmptyDirs.zip");
using (var stream = File.OpenRead(testArchive))
{
var result = ArchiveFactory.IsArchive(stream, out var type);
Assert.True(result);
Assert.Equal(ArchiveType.Zip, type);
}
}
}