stride3d/stride · error · InvalidOperationException

You cannot modify this stream.

Error message

You cannot modify this stream.

What it means

ZipStream is a read-only stream over a zip entry; Flush() is overridden to always throw InvalidOperationException because flushing would imply modifying the underlying archive. Calling Flush on a ZipStream is always a programming error.

Solutions

  1. Remove the Flush() call on ZipStream
  2. Flush or dispose the wrapper writer without flushing the base stream (e.g. leave open and dispose carefully)
  3. Only flush streams known to be writable
  4. Guard flush calls behind a CanWrite check

Example fix

// before
using (var writer = new StreamWriter(zipStream))
{
    writer.WriteLine(text);
    zipStream.Flush(); // throws
}
// after
using (var reader = new StreamReader(zipStream))
{
    var text = reader.ReadToEnd(); // read-only usage, no flush needed
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!stream.CanWrite) { /* skip flush */ }

Type guard

static bool IsWritableStream(Stream s) => s.CanWrite;

Try / catch

try { stream.Flush(); } catch (InvalidOperationException) { /* ZipStream is read-only; safe to ignore */ }

Prevention

When it happens

Trigger: Calling stream.Flush() (directly or via StreamWriter/CryptoStream wrappers that flush on Dispose) on a ZipStream obtained from ZipFile.OpenFile/ReadFile.

Common situations: Wrapping ZipStream in a StreamWriter whose Dispose flushes the stream, or copying defensive Flush calls from code written for writable streams.

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/5fbd81b6f5d32acf. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.IO/System.IO.Compression.Zip/ZipStream.cs:141

        /// </summary>
        public override void Close()
        {
            base.Close();

            if (this.zipFileEntry.Method == Compression.Deflate)
            {
                this.innerStream.Dispose();
            }
        }

        /// <summary>
        /// The flush.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public override void Flush()
        {
            throw new InvalidOperationException("You cannot modify this stream.");
        }

        /// <summary>
        /// The read.
        /// </summary>
        /// <param name="buffer">
        /// The buffer.
        /// </param>
        /// <param name="offset">
        /// The offset.
        /// </param>
        /// <param name="count">
        /// The count.
        /// </param>
        /// <returns>
        /// The read.
        /// </returns>
        public override int Read(byte[] buffer, int offset, int count)

View on GitHub (pinned to 96fad776d2)