Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change FastZip.ExtractZip to use the new ZipFile constructor to set isStreamOwner, rather than setting the property after construction #311

Merged
merged 2 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/FastZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,13 @@ public void ExtractZip(Stream inputStream, string targetDirectory,
directoryFilter_ = new NameFilter(directoryFilter);
restoreDateTimeOnExtract_ = restoreDateTime;

using (zipFile_ = new ZipFile(inputStream))
using (zipFile_ = new ZipFile(inputStream, !isStreamOwner))
{
if (password_ != null)
{
zipFile_.Password = password_;
}
zipFile_.IsStreamOwner = isStreamOwner;

System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator();
while (continueRunning_ && enumerator.MoveNext())
{
Expand Down
56 changes: 56 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,5 +447,61 @@ void CreateTestFile(string archiveFile, string contentPath)
Directory.Delete(tempPath, true);
}
}

/// <summary>
/// Check that the input stream is not closed on error when isStreamOwner is false
/// </summary>
[Test]
public void StreamNotClosedOnError()
{
// test paths
string tempFilePath = GetTempFilePath();
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");

var tempFolderPath = Path.Combine(tempFilePath, Path.GetRandomFileName());
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should not exist");

// memory that isn't a valid zip
var ms = new TrackedMemoryStream(new byte[32]);
Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially");

// Try to extract
var fastZip = new FastZip();
fastZip.CreateEmptyDirectories = true;

Assert.Throws<ZipException>(() => fastZip.ExtractZip(ms, tempFolderPath, FastZip.Overwrite.Always, null, "a", "b", false, false), "Should throw when extracting an invalid file");
Assert.IsFalse(ms.IsClosed, "inputStream stream should NOT be closed when isStreamOwner is false");

// test folder should not have been created on error
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist");
}

/// <summary>
/// Check that the input stream is closed on error when isStreamOwner is true
/// </summary>
[Test]
public void StreamClosedOnError()
{
// test paths
string tempFilePath = GetTempFilePath();
Assert.IsNotNull(tempFilePath, "No permission to execute this test?");

var tempFolderPath = Path.Combine(tempFilePath, Path.GetRandomFileName());
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should not exist");

// memory that isn't a valid zip
var ms = new TrackedMemoryStream(new byte[32]);
Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially");

// Try to extract
var fastZip = new FastZip();
fastZip.CreateEmptyDirectories = true;

Assert.Throws<ZipException>(() => fastZip.ExtractZip(ms, tempFolderPath, FastZip.Overwrite.Always, null, "a", "b", false, true), "Should throw when extracting an invalid file");
Assert.IsTrue(ms.IsClosed, "inputStream stream should be closed when isStreamOwner is true");

// test folder should not have been created on error
Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist");
}
}
}