From b3d0e14b78bdf4926ce84abba6939c01f671356b Mon Sep 17 00:00:00 2001 From: Paul Du Bois Date: Tue, 1 Sep 2015 16:08:25 -0700 Subject: [PATCH] Fix severe perf degradation reading Stored file in Mono When reading a stored file, ZipFile returns a PartialInputStream directly instead of wrapping it with a decompression layer. PartialInputStream handles sharing the underlying stream among multiple threads by calling Seek() before each read. Unlike Microsoft's reference implementation, Mono's FileStream flushes its internal read buffer on every Seek. --- src/Zip/ZipFile.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Zip/ZipFile.cs b/src/Zip/ZipFile.cs index 6a71cae5c..5be059daf 100644 --- a/src/Zip/ZipFile.cs +++ b/src/Zip/ZipFile.cs @@ -3778,8 +3778,11 @@ public override int Read(byte[] buffer, int offset, int count) return 0; } } - - baseStream_.Seek(readPos_, SeekOrigin.Begin); + // Protect against Stream implementations that throw away their buffer on every Seek + // (for example, Mono FileStream) + if (baseStream_.Position != readPos_) { + baseStream_.Seek(readPos_, SeekOrigin.Begin); + } int readCount = baseStream_.Read(buffer, offset, count); if (readCount > 0) { readPos_ += readCount;