From 9092ecf33128925f6e66f3a7b91641de435a45cb Mon Sep 17 00:00:00 2001 From: Dmitry Nesterov <4ybaka@gmail.com> Date: Thu, 4 Jan 2018 22:57:32 +0100 Subject: [PATCH 1/3] Added ability to leave tar archive open after stream is closed --- src/SharpCompress/Writers/Tar/TarWriter.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index db7bc97ac..c4036b1be 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -11,9 +11,13 @@ namespace SharpCompress.Writers.Tar { public class TarWriter : AbstractWriter { - public TarWriter(Stream destination, WriterOptions options) + private readonly bool finalizeArchiveOnClose; + + public TarWriter(Stream destination, WriterOptions options, bool finalizeArchiveOnClose = true) : base(ArchiveType.Tar, options) { + this.finalizeArchiveOnClose = finalizeArchiveOnClose; + if (!destination.CanWrite) { throw new ArgumentException("Tars require writable streams."); @@ -97,8 +101,10 @@ protected override void Dispose(bool isDisposing) { if (isDisposing) { - PadTo512(0, true); - PadTo512(0, true); + if (finalizeArchiveOnClose){ + PadTo512(0, true); + PadTo512(0, true); + } switch (OutputStream) { case BZip2Stream b: From 8f7ea420b332c426cc6cedeea7f9a28fa5707746 Mon Sep 17 00:00:00 2001 From: Dmitry Nesterov <4ybaka@gmail.com> Date: Sat, 13 Jan 2018 00:41:35 +0100 Subject: [PATCH 2/3] Revert "Added ability to leave tar archive open after stream is closed" This reverts commit 9092ecf33128925f6e66f3a7b91641de435a45cb. --- src/SharpCompress/Writers/Tar/TarWriter.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index c4036b1be..db7bc97ac 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -11,13 +11,9 @@ namespace SharpCompress.Writers.Tar { public class TarWriter : AbstractWriter { - private readonly bool finalizeArchiveOnClose; - - public TarWriter(Stream destination, WriterOptions options, bool finalizeArchiveOnClose = true) + public TarWriter(Stream destination, WriterOptions options) : base(ArchiveType.Tar, options) { - this.finalizeArchiveOnClose = finalizeArchiveOnClose; - if (!destination.CanWrite) { throw new ArgumentException("Tars require writable streams."); @@ -101,10 +97,8 @@ protected override void Dispose(bool isDisposing) { if (isDisposing) { - if (finalizeArchiveOnClose){ - PadTo512(0, true); - PadTo512(0, true); - } + PadTo512(0, true); + PadTo512(0, true); switch (OutputStream) { case BZip2Stream b: From f85fd1f6a4754ffd7ecc3a823683a1ac71cb9016 Mon Sep 17 00:00:00 2001 From: Dmitry Nesterov <4ybaka@gmail.com> Date: Sat, 13 Jan 2018 00:44:42 +0100 Subject: [PATCH 3/3] Added ability to leave tar archive open after stream is closed --- src/SharpCompress/Archives/Tar/TarArchive.cs | 2 +- src/SharpCompress/Writers/Tar/TarWriter.cs | 12 +++++++--- .../Writers/Tar/TarWriterOptions.cs | 23 +++++++++++++++++++ src/SharpCompress/Writers/WriterFactory.cs | 2 +- .../SharpCompress.Test/Tar/TarWriterTests.cs | 21 ++++++++++++++++- 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100755 src/SharpCompress/Writers/Tar/TarWriterOptions.cs diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index 35d7fd152..961c1f6e8 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -182,7 +182,7 @@ protected override void SaveTo(Stream stream, WriterOptions options, IEnumerable oldEntries, IEnumerable newEntries) { - using (var writer = new TarWriter(stream, options)) + using (var writer = new TarWriter(stream, new TarWriterOptions(options))) { foreach (var entry in oldEntries.Concat(newEntries) .Where(x => !x.IsDirectory)) diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index db7bc97ac..659c60e14 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -11,9 +11,13 @@ namespace SharpCompress.Writers.Tar { public class TarWriter : AbstractWriter { - public TarWriter(Stream destination, WriterOptions options) + private bool finalizeArchiveOnClose; + + public TarWriter(Stream destination, TarWriterOptions options) : base(ArchiveType.Tar, options) { + finalizeArchiveOnClose = options.FinalizeArchiveOnClose; + if (!destination.CanWrite) { throw new ArgumentException("Tars require writable streams."); @@ -97,8 +101,10 @@ protected override void Dispose(bool isDisposing) { if (isDisposing) { - PadTo512(0, true); - PadTo512(0, true); + if (finalizeArchiveOnClose) { + PadTo512(0, true); + PadTo512(0, true); + } switch (OutputStream) { case BZip2Stream b: diff --git a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs new file mode 100755 index 000000000..9174b23f3 --- /dev/null +++ b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs @@ -0,0 +1,23 @@ +using SharpCompress.Archives; +using SharpCompress.Common; + +namespace SharpCompress.Writers.Tar +{ + public class TarWriterOptions : WriterOptions + { + /// + /// Indicates if archive should be finalized (by 2 empty blocks) on close. + /// + public bool FinalizeArchiveOnClose { get; } + + public TarWriterOptions(CompressionType compressionType, bool finalizeArchiveOnClose) + : base(compressionType) + { + FinalizeArchiveOnClose = finalizeArchiveOnClose; + } + + internal TarWriterOptions(WriterOptions options) : this(options.CompressionType, true) + { + } + } +} \ No newline at end of file diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index b623ee63a..1f1c01a50 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -27,7 +27,7 @@ public static IWriter Open(Stream stream, ArchiveType archiveType, WriterOptions } case ArchiveType.Tar: { - return new TarWriter(stream, writerOptions); + return new TarWriter(stream, new TarWriterOptions(writerOptions)); } default: { diff --git a/tests/SharpCompress.Test/Tar/TarWriterTests.cs b/tests/SharpCompress.Test/Tar/TarWriterTests.cs index faeabd221..4b0b21685 100644 --- a/tests/SharpCompress.Test/Tar/TarWriterTests.cs +++ b/tests/SharpCompress.Test/Tar/TarWriterTests.cs @@ -1,4 +1,6 @@ -using SharpCompress.Common; +using System.IO; +using SharpCompress.Common; +using SharpCompress.Writers.Tar; using Xunit; namespace SharpCompress.Test.Tar @@ -34,5 +36,22 @@ public void Tar_Rar_Write() { Assert.Throws(() => Write(CompressionType.Rar, "Zip.ppmd.noEmptyDirs.zip", "Zip.ppmd.noEmptyDirs.zip")); } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Tar_Finalize_Archive(bool finalizeArchive) + { + using (MemoryStream stream = new MemoryStream()) + using (Stream content = File.OpenRead(Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg"))) { + using (TarWriter writer = new TarWriter(stream, new TarWriterOptions(CompressionType.None, finalizeArchive))) { + writer.Write("doesn't matter", content, null); + } + + var paddedContentWithHeader = content.Length / 512 * 512 + 512 + 512; + var expectedStreamLength = finalizeArchive ? paddedContentWithHeader + 512 * 2 : paddedContentWithHeader; + Assert.Equal(expectedStreamLength, stream.Length); + } + } } }