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
- Remove the Flush() call on ZipStream
- Flush or dispose the wrapper writer without flushing the base stream (e.g. leave open and dispose carefully)
- Only flush streams known to be writable
- 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
- Treat ZipStream as strictly read-only
- Avoid wrapping it in writers that flush on dispose
- Check Stream.CanWrite before flush operations
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
- Stream cannot seek
- Stream cannot be written
- ZIP archive are read-only.
- Central directory currently does not exist
- Not a valid zip entry.
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)