Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 29, 2025

ArchiveFactory.Open(stream) fails on Linux with "Cannot determine compressed stream type" when the stream is already wrapped or when buffering state needs careful management.

Root Cause

ArchiveFactory.Open unconditionally creates a new SharpCompressStream wrapper:

stream = new SharpCompressStream(stream, bufferSize: readerOptions.BufferSize);

This causes double-wrapping when the stream is already a SharpCompressStream, leading to inconsistent buffering state that manifests differently across platforms.

Changes

  • ArchiveFactory.cs: Use SharpCompressStream.Create instead of constructor

    • Reuses existing wrapper if properties match
    • Only creates new wrapper when necessary
    • Prevents double-wrapping and buffering inconsistencies
  • Tests: Added coverage for both pre-wrapped and raw FileStream scenarios

// After fix - handles both cases correctly
using var stream = File.OpenRead(zipFilePath);
var archive = ArchiveFactory.Open(stream);  // Works on Linux

using var wrapped = SharpCompressStream.Create(stream, bufferSize: 32768);
var archive2 = ArchiveFactory.Open(wrapped);  // Also works, no double-wrap
Original prompt

This section details on the original issue you should resolve

<issue_title>ZipArchive.IsZipFile fails on Linux: Cannot determine compressed stream type</issue_title>
<issue_description>Hi,

Stream stream = File.OpenRead(zipFilePath);
var archive = ArchiveFactory.Open(stream);

Fails on Linux for zip files created on Windows using SharpCompress with:
System.InvalidOperationException : Cannot determine compressed stream type. Supported Archive Formats: Zip, Rar, 7Zip, GZip, Tar

Same code runs fine on Windows though.

DotNet 8, SharpCompress 0.38.0</issue_description>

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

@adamhathcock Probably need more detail here. @adamhathcock More code detail and archive type might point me in the right direction. ARM is a hint which I'll look at but in theory that should be fine. I'm using ARM MacOS

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits October 29, 2025 13:26
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
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>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ZipArchive.IsZipFile failing on Linux for specific zip files Fix ArchiveFactory.Open double-wrapping causing "Cannot determine compressed stream type" on Linux Oct 29, 2025
Copilot AI requested a review from adamhathcock October 29, 2025 13:39
@adamhathcock adamhathcock marked this pull request as ready for review October 29, 2025 14:27
Copilot AI review requested due to automatic review settings October 29, 2025 14:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue where ZIP files fail to open on Linux by preventing double-wrapping of streams when ArchiveFactory.Open is called with a stream that's already wrapped in SharpCompressStream. The fix changes ArchiveFactory.Open to use the SharpCompressStream.Create static factory method instead of directly constructing a new instance, which checks if the stream is already wrapped and reuses it if appropriate.

  • Changed ArchiveFactory.Open to use SharpCompressStream.Create instead of direct constructor call
  • Added two test cases to verify the fix handles both pre-wrapped and raw file streams correctly
  • Updated package lock file resolving Microsoft.NET.ILLink.Tasks from version 8.0.17 to 8.0.0

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/SharpCompress/Archives/ArchiveFactory.cs Changed stream wrapping to use factory method that prevents double-wrapping
tests/SharpCompress.Test/ArchiveTests.cs Added tests for both pre-wrapped and raw file stream scenarios
src/SharpCompress/packages.lock.json Downgraded Microsoft.NET.ILLink.Tasks package version

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

{
// 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");
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to 'System.IO.Path.Combine'.

Copilot uses AI. Check for mistakes.
{
// 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");
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to 'System.IO.Path.Combine'.

Copilot uses AI. Check for mistakes.
…ive-linux-issue

# Conflicts:
#	src/SharpCompress/packages.lock.json
@adamhathcock adamhathcock merged commit 75bc850 into master Oct 31, 2025
7 of 8 checks passed
@adamhathcock adamhathcock deleted the copilot/fix-ziparchive-linux-issue branch October 31, 2025 11:25
@Pyroluk
Copy link

Pyroluk commented Nov 28, 2025

While testing the new release, it turned out the issue was simply a missing Git LFS installation in the CI test pipeline.
GitLab didn’t indicate anywhere that git pull with LFS had failed.

However, the problem in this library is still the misleading error message when the stream is actually empty:
"Cannot determine compressed stream type. Supported Archive Formats: Zip, Rar, 7Zip, GZip, Tar"

Maybe we can add a check for an empty stream and throw a more specific error message in that case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZipArchive.IsZipFile fails on Linux: Cannot determine compressed stream type

3 participants