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

Add a variant of FastZip.CreateZip with a leaveOpen parameter to control output stream disposal #455

Merged
merged 2 commits into from
Aug 7, 2020
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
15 changes: 15 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/FastZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,28 @@ public void CreateZip(string zipFileName, string sourceDirectory, bool recurse,
/// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
/// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
{
CreateZip(outputStream, sourceDirectory, recurse, fileFilter, directoryFilter, false);
}

/// <summary>
/// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
/// </summary>
/// <param name="outputStream">The stream to write archive data to.</param>
/// <param name="sourceDirectory">The directory to source files from.</param>
/// <param name="recurse">True to recurse directories, false for no recursion.</param>
/// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
/// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
/// <param name="leaveOpen">true to leave <paramref name="outputStream"/> open after the zip has been created, false to dispose it.</param>
public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter, bool leaveOpen)
{
NameTransform = new ZipNameTransform(sourceDirectory);
sourceDirectory_ = sourceDirectory;

using (outputStream_ = new ZipOutputStream(outputStream))
{
outputStream_.SetLevel((int)CompressionLevel);
outputStream_.IsStreamOwner = !leaveOpen;

if (password_ != null)
{
Expand Down
40 changes: 40 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,5 +625,45 @@ public void SetDirectoryModifiedDate()
Directory.Delete(targetDir, true);
}
}

/// <summary>
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/78
/// </summary>
/// <param name="leaveOpen">if true, the stream given to CreateZip should be left open, if false it should be disposed.</param>
[TestCase(true)]
[TestCase(false)]
[Category("Zip")]
[Category("CreatesTempFile")]
public void CreateZipShouldLeaveOutputStreamOpenIfRequested(bool leaveOpen)
{
const string tempFileName = "a(2).dat";

using (var tempFolder = new Utils.TempDir())
{
// Create test input file
string addFile = Path.Combine(tempFolder.Fullpath, tempFileName);
MakeTempFile(addFile, 16);

// Create the zip with fast zip
var target = new TrackedMemoryStream();
var fastZip = new FastZip();

fastZip.CreateZip(target, tempFolder.Fullpath, false, @"a\(2\)\.dat", null, leaveOpen: leaveOpen);

// Check that the output stream was disposed (or not) as expected
Assert.That(target.IsDisposed, Is.Not.EqualTo(leaveOpen), "IsDisposed should be the opposite of leaveOpen");

// Check that the file contents are correct in both cases
var archive = new MemoryStream(target.ToArray());
using (ZipFile zf = new ZipFile(archive))
{
Assert.AreEqual(1, zf.Count);
ZipEntry entry = zf[0];
Assert.AreEqual(tempFileName, entry.Name);
Assert.AreEqual(16, entry.Size);
Assert.IsTrue(zf.TestArchive(true));
}
}
}
}
}