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

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

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/997
Author: @Copilot
Created: 10/29/2025
Status: Merged
Merged: 10/31/2025
Merged by: @adamhathcock

Base: masterHead: copilot/fix-ziparchive-linux-issue


📝 Commits (5)

  • ad5c655 Initial plan
  • df59c5c Plan: Fix potential ZipArchive.IsZipFile failure on Linux
  • db98e5f Fix ArchiveFactory.Open to avoid double-wrapping SharpCompressStream
  • ea77666 Final verification: All tests pass, no security issues
  • ee84d97 Merge remote-tracking branch 'origin/master' into copilot/fix-ziparchive-linux-issue

📊 Changes

2 files changed (+33 additions, -1 deletions)

View changed files

📝 src/SharpCompress/Archives/ArchiveFactory.cs (+1 -1)
📝 tests/SharpCompress.Test/ArchiveTests.cs (+32 -0)

📄 Description

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.


🔄 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/997 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 10/29/2025 **Status:** ✅ Merged **Merged:** 10/31/2025 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `copilot/fix-ziparchive-linux-issue` --- ### 📝 Commits (5) - [`ad5c655`](https://github.com/adamhathcock/sharpcompress/commit/ad5c655c4587240d02b50f084226a254fd2c06a7) Initial plan - [`df59c5c`](https://github.com/adamhathcock/sharpcompress/commit/df59c5cb9d3aa7e0ff1efdf88ae47327b3dcb6fa) Plan: Fix potential ZipArchive.IsZipFile failure on Linux - [`db98e5f`](https://github.com/adamhathcock/sharpcompress/commit/db98e5f39ba7dd4eae5c9de04628fccafd10eed3) Fix ArchiveFactory.Open to avoid double-wrapping SharpCompressStream - [`ea77666`](https://github.com/adamhathcock/sharpcompress/commit/ea77666b4a42b974b21d388cf6e1dd55c1bc0c56) Final verification: All tests pass, no security issues - [`ee84d97`](https://github.com/adamhathcock/sharpcompress/commit/ee84d971b232b319be3014154a9128e6bff0128d) Merge remote-tracking branch 'origin/master' into copilot/fix-ziparchive-linux-issue ### 📊 Changes **2 files changed** (+33 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Archives/ArchiveFactory.cs` (+1 -1) 📝 `tests/SharpCompress.Test/ArchiveTests.cs` (+32 -0) </details> ### 📄 Description `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. --- <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:27 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1415