From db98e5f39ba7dd4eae5c9de04628fccafd10eed3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Oct 2025 13:34:53 +0000 Subject: [PATCH] Fix ArchiveFactory.Open to avoid double-wrapping SharpCompressStream Use SharpCompressStream.Create instead of constructor to properly handle streams that are already wrapped. This prevents potential buffering issues when opening ZIP files, particularly on Linux systems. Added tests to verify both raw FileStream and pre-wrapped stream scenarios. Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> --- src/SharpCompress/Archives/ArchiveFactory.cs | 2 +- tests/SharpCompress.Test/ArchiveTests.cs | 32 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 870092f1..94368ece 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -20,7 +20,7 @@ public static class ArchiveFactory public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) { readerOptions ??= new ReaderOptions(); - stream = new SharpCompressStream(stream, bufferSize: readerOptions.BufferSize); + stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); return FindFactory(stream).Open(stream, readerOptions); } diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 2849a71f..2f47e121 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -622,4 +622,36 @@ public class ArchiveTests : ReaderTests VerifyFiles(); } } + + [Fact] + public void ArchiveFactory_Open_WithPreWrappedStream() + { + // Test that ArchiveFactory.Open works correctly with a stream that's already wrapped + // This addresses the issue where ZIP files fail to open on Linux + var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.noEmptyDirs.zip"); + + // Open with a pre-wrapped stream + using (var fileStream = File.OpenRead(testArchive)) + using (var wrappedStream = SharpCompressStream.Create(fileStream, bufferSize: 32768)) + using (var archive = ArchiveFactory.Open(wrappedStream)) + { + Assert.Equal(ArchiveType.Zip, archive.Type); + Assert.Equal(3, archive.Entries.Count()); + } + } + + [Fact] + public void ArchiveFactory_Open_WithRawFileStream() + { + // Test that ArchiveFactory.Open works correctly with a raw FileStream + // This is the common use case reported in the issue + var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.noEmptyDirs.zip"); + + using (var stream = File.OpenRead(testArchive)) + using (var archive = ArchiveFactory.Open(stream)) + { + Assert.Equal(ArchiveType.Zip, archive.Type); + Assert.Equal(3, archive.Entries.Count()); + } + } }