[PR #997] Fix ArchiveFactory.Open double-wrapping causing "Cannot determine compressed stream type" on Linux #1418

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

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

State: closed
Merged: Yes


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.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/997 **State:** closed **Merged:** Yes --- `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: ```csharp 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 ```csharp // 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 ``` <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *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, > > ```cs > 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) > > <comments> > <comment_new><author>@adamhathcock</author><body> > Probably need more detail here.</body></comment_new> > <comment_new><author>@adamhathcock</author><body> > 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</body></comment_new> > </comments> > </details> - Fixes adamhathcock/sharpcompress#874 <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/adamhathcock/sharpcompress/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.
claunia added the pull-request label 2026-01-29 22:20:28 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1418