Skip to content

Commit f68abec

Browse files
Numpsypiksel
authored andcommitted
Merge PR# 311: Set isStreamOwner from ZipFile constructor in FastZip.ExtractZip
* Change FastZip.ExtractZip to use the new ZipFile constructor to set isStreamOwner, rather than setting the property after construction. * Add unit tests for testing that FastZip.Extract handles stream disposal a bit better when handling a corrupt zip file
1 parent f854d8d commit f68abec

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/ICSharpCode.SharpZipLib/Zip/FastZip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,13 @@ public void ExtractZip(Stream inputStream, string targetDirectory,
464464
directoryFilter_ = new NameFilter(directoryFilter);
465465
restoreDateTimeOnExtract_ = restoreDateTime;
466466

467-
using (zipFile_ = new ZipFile(inputStream))
467+
using (zipFile_ = new ZipFile(inputStream, !isStreamOwner))
468468
{
469469
if (password_ != null)
470470
{
471471
zipFile_.Password = password_;
472472
}
473-
zipFile_.IsStreamOwner = isStreamOwner;
473+
474474
System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
475475
while (continueRunning_ && enumerator.MoveNext())
476476
{

test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,5 +447,61 @@ void CreateTestFile(string archiveFile, string contentPath)
447447
Directory.Delete(tempPath, true);
448448
}
449449
}
450+
451+
/// <summary>
452+
/// Check that the input stream is not closed on error when isStreamOwner is false
453+
/// </summary>
454+
[Test]
455+
public void StreamNotClosedOnError()
456+
{
457+
// test paths
458+
string tempFilePath = GetTempFilePath();
459+
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
460+
461+
var tempFolderPath = Path.Combine(tempFilePath, Path.GetRandomFileName());
462+
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should not exist");
463+
464+
// memory that isn't a valid zip
465+
var ms = new TrackedMemoryStream(new byte[32]);
466+
Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially");
467+
468+
// Try to extract
469+
var fastZip = new FastZip();
470+
fastZip.CreateEmptyDirectories = true;
471+
472+
Assert.Throws<ZipException>(() => fastZip.ExtractZip(ms, tempFolderPath, FastZip.Overwrite.Always, null, "a", "b", false, false), "Should throw when extracting an invalid file");
473+
Assert.IsFalse(ms.IsClosed, "inputStream stream should NOT be closed when isStreamOwner is false");
474+
475+
// test folder should not have been created on error
476+
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist");
477+
}
478+
479+
/// <summary>
480+
/// Check that the input stream is closed on error when isStreamOwner is true
481+
/// </summary>
482+
[Test]
483+
public void StreamClosedOnError()
484+
{
485+
// test paths
486+
string tempFilePath = GetTempFilePath();
487+
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");
488+
489+
var tempFolderPath = Path.Combine(tempFilePath, Path.GetRandomFileName());
490+
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should not exist");
491+
492+
// memory that isn't a valid zip
493+
var ms = new TrackedMemoryStream(new byte[32]);
494+
Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially");
495+
496+
// Try to extract
497+
var fastZip = new FastZip();
498+
fastZip.CreateEmptyDirectories = true;
499+
500+
Assert.Throws<ZipException>(() => fastZip.ExtractZip(ms, tempFolderPath, FastZip.Overwrite.Always, null, "a", "b", false, true), "Should throw when extracting an invalid file");
501+
Assert.IsTrue(ms.IsClosed, "inputStream stream should be closed when isStreamOwner is true");
502+
503+
// test folder should not have been created on error
504+
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist");
505+
}
450506
}
451507
}

0 commit comments

Comments
 (0)