未验证 提交 672ff9b9 编写于 作者: E etemi 提交者: GitHub

ZipArchive now disposes given stream only once (#87982)

* Fix #79695

Even though the IDisposable contract says that you should be able to call
Dispose multiple times, be forgiving for implementations that throw when
Dispose is called multiple times.

* Remove line

* Augmented test to explicitly test behavior according to leaveOpen parameter

---------

Co-authored-by: etemi <etemi>
上级 3da60b4b
......@@ -164,7 +164,7 @@ public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding?
if (mode == ZipArchiveMode.Create)
_archiveReader = null;
else
_archiveReader = new BinaryReader(_archiveStream);
_archiveReader = new BinaryReader(_archiveStream, Encoding.UTF8, leaveOpen: true);
_entries = new List<ZipArchiveEntry>();
_entriesCollection = new ReadOnlyCollection<ZipArchiveEntry>(_entries);
_entriesDictionary = new Dictionary<string, ZipArchiveEntry>();
......
......@@ -249,5 +249,39 @@ public static async Task IdentifyEncryptedEntries(string zipFile)
Assert.Equal(expectedEntries, entriesEncrypted);
}
[Theory]
[InlineData(true, 0)]
[InlineData(false, 1)]
public static async Task EnsureDisposeIsCalledAsExpectedOnTheUnderlyingStream(bool leaveOpen, int expectedDisposeCalls)
{
var disposeCallCountingStream = new DisposeCallCountingStream();
using (var tempStream = await StreamHelpers.CreateTempCopyStream(zfile("small.zip")))
{
tempStream.CopyTo(disposeCallCountingStream);
}
using (ZipArchive archive = new ZipArchive(disposeCallCountingStream, ZipArchiveMode.Read, leaveOpen))
{
// Iterate through entries to ensure read of zip file
foreach (ZipArchiveEntry entry in archive.Entries)
{
Assert.False(entry.IsEncrypted);
}
}
Assert.Equal(expectedDisposeCalls, disposeCallCountingStream.NumberOfDisposeCalls);
}
private class DisposeCallCountingStream : MemoryStream
{
public int NumberOfDisposeCalls { get; private set; }
protected override void Dispose(bool disposing)
{
NumberOfDisposeCalls++;
base.Dispose(disposing);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册