stride3d/stride · error · InvalidOperationException

Stream cannot seek

Error message

Stream cannot seek

What it means

The ZipFile(Stream) constructor needs to seek backward to locate the end-of-central-directory record. A non-seekable stream (e.g. network or pipe stream) cannot be read as an archive, so the constructor throws immediately.

Solutions

  1. Copy the stream into a MemoryStream or a temporary FileStream first, then open the ZipFile over that
  2. For HTTP, use a stream that supports seeking (e.g. write response to disk)
  3. Check stream.CanSeek before constructing to give a clearer error

Example fix

// before
using var zip = new ZipFile(responseStream);
// after
using var ms = new MemoryStream();
responseStream.CopyTo(ms);
ms.Position = 0;
using var zip = new ZipFile(ms);
Defensive patterns

Strategy: validation

Validate before calling

if (!stream.CanSeek) { using var ms = new MemoryStream(); stream.CopyTo(ms); ms.Position = 0; return new ZipFile(ms); }

Type guard

static bool IsSeekable(Stream s) => s.CanSeek;

Try / catch

try { zip = new ZipFile(stream); }
catch (InvalidOperationException) { /* buffer to MemoryStream and retry */ }

Prevention

When it happens

Trigger: Constructing ZipFile from a raw NetworkStream, the body of an HTTP response stream read directly, a compressed/deflate stream, or a console/pipe stream.

Common situations: Downloading a zip directly into ZipFile without buffering, wrapping a GZipStream and passing it as the zip stream.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/7eda7ec41f2c0972. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.IO/System.IO.Compression.Zip/ZipFile.cs:82

        {
            this.FileName = filename;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ZipFile"/> class. 
        /// Method to open an existing storage from stream
        /// </summary>
        /// <param name="stream">
        /// Already opened stream with zip contents
        /// </param>
        /// <returns>
        /// A valid ZipFile object
        /// </returns>
        public ZipFile(Stream stream)
        {
            if (!stream.CanSeek)
            {
                throw new InvalidOperationException("Stream cannot seek");
            }

            this.zipFileStream = stream;

            if (!this.ReadFileInfo())
            {
                throw new Exception();
            }

            this.FileName = string.Empty;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the number of entries in the zip file.

View on GitHub (pinned to 96fad776d2)