Skip to content

Commit 75adef5

Browse files
authored
PR #455: Add FastZip.CreateZip with a leaveOpen parameter
* 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
1 parent fd9e8a8 commit 75adef5

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/ICSharpCode.SharpZipLib/Zip/FastZip.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,28 @@ public void CreateZip(string zipFileName, string sourceDirectory, bool recurse,
361361
/// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
362362
/// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
363363
public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
364+
{
365+
CreateZip(outputStream, sourceDirectory, recurse, fileFilter, directoryFilter, false);
366+
}
367+
368+
/// <summary>
369+
/// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
370+
/// </summary>
371+
/// <param name="outputStream">The stream to write archive data to.</param>
372+
/// <param name="sourceDirectory">The directory to source files from.</param>
373+
/// <param name="recurse">True to recurse directories, false for no recursion.</param>
374+
/// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
375+
/// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
376+
/// <param name="leaveOpen">true to leave <paramref name="outputStream"/> open after the zip has been created, false to dispose it.</param>
377+
public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter, bool leaveOpen)
364378
{
365379
NameTransform = new ZipNameTransform(sourceDirectory);
366380
sourceDirectory_ = sourceDirectory;
367381

368382
using (outputStream_ = new ZipOutputStream(outputStream))
369383
{
370384
outputStream_.SetLevel((int)CompressionLevel);
385+
outputStream_.IsStreamOwner = !leaveOpen;
371386

372387
if (password_ != null)
373388
{

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,5 +625,45 @@ public void SetDirectoryModifiedDate()
625625
Directory.Delete(targetDir, true);
626626
}
627627
}
628+
629+
/// <summary>
630+
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/78
631+
/// </summary>
632+
/// <param name="leaveOpen">if true, the stream given to CreateZip should be left open, if false it should be disposed.</param>
633+
[TestCase(true)]
634+
[TestCase(false)]
635+
[Category("Zip")]
636+
[Category("CreatesTempFile")]
637+
public void CreateZipShouldLeaveOutputStreamOpenIfRequested(bool leaveOpen)
638+
{
639+
const string tempFileName = "a(2).dat";
640+
641+
using (var tempFolder = new Utils.TempDir())
642+
{
643+
// Create test input file
644+
string addFile = Path.Combine(tempFolder.Fullpath, tempFileName);
645+
MakeTempFile(addFile, 16);
646+
647+
// Create the zip with fast zip
648+
var target = new TrackedMemoryStream();
649+
var fastZip = new FastZip();
650+
651+
fastZip.CreateZip(target, tempFolder.Fullpath, false, @"a\(2\)\.dat", null, leaveOpen: leaveOpen);
652+
653+
// Check that the output stream was disposed (or not) as expected
654+
Assert.That(target.IsDisposed, Is.Not.EqualTo(leaveOpen), "IsDisposed should be the opposite of leaveOpen");
655+
656+
// Check that the file contents are correct in both cases
657+
var archive = new MemoryStream(target.ToArray());
658+
using (ZipFile zf = new ZipFile(archive))
659+
{
660+
Assert.AreEqual(1, zf.Count);
661+
ZipEntry entry = zf[0];
662+
Assert.AreEqual(tempFileName, entry.Name);
663+
Assert.AreEqual(16, entry.Size);
664+
Assert.IsTrue(zf.TestArchive(true));
665+
}
666+
}
667+
}
628668
}
629669
}

0 commit comments

Comments
 (0)