1

Closed

Save to the input stream should not be allowed.

description

This code should throw an exception:
 
        var inputStream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        using (ZipFile zip = ZipFile.Read(inputStream))
        {
            zip.AddEntry("three.txt", "small entry: 3");
            zip.Save(inputStream);
        }
Closed Jun 14, 2011 at 3:32 AM by Cheeso
fixed in changeset 79082. First binary with this fix: v1.9.1.6. This is a documentation update, only.

comments

Cheeso wrote Jan 20, 2010 at 5:28 PM

Thinking about this further, it is difficult to provide a general-case check for this. IF the input and output streams are both FileStreams, then comparing the SafeFileHandle values should tell whether it is the same stream. On the other hand, it's possible there are 2 open streams on the same file using different handles, and such a check would not catch that.

More generally, suppose the stream is a MemoryStream backed only by a byte array. In this case, it is very easy to see that you may have 2 MemoryStreams wrapping a single array. There's no way to check for this.

So I think the correct solution here is to simply document that it is not supported to save to the input stream.