Skip to content

Commit

Permalink
PR #455: Add FastZip.CreateZip with a leaveOpen parameter
Browse files Browse the repository at this point in the history
* Add a variant of FastZip.CreateZip with a leaveOpen parameter to control output stream disposal
* Add unit test for FastZip.CreateZip leaving the stream open or disposed as required
  • Loading branch information
Numpsy authored Aug 7, 2020
1 parent fd9e8a8 commit 75adef5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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));
}
}
}
}
}

0 comments on commit 75adef5

Please sign in to comment.