Skip to content

Commit

Permalink
Merge pull request #337 from 4ybaka/issue-323-tar-archive-finalization
Browse files Browse the repository at this point in the history
Added ability to leave tar archive open after stream is closed
  • Loading branch information
adamhathcock authored Jan 14, 2018
2 parents d8c8dab + e701f52 commit f893c12
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected override void SaveTo(Stream stream, WriterOptions options,
IEnumerable<TarArchiveEntry> oldEntries,
IEnumerable<TarArchiveEntry> 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))
Expand Down
12 changes: 9 additions & 3 deletions src/SharpCompress/Writers/Tar/TarWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions src/SharpCompress/Writers/Tar/TarWriterOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using SharpCompress.Archives;
using SharpCompress.Common;

namespace SharpCompress.Writers.Tar
{
public class TarWriterOptions : WriterOptions
{
/// <summary>
/// Indicates if archive should be finalized (by 2 empty blocks) on close.
/// </summary>
public bool FinalizeArchiveOnClose { get; }

public TarWriterOptions(CompressionType compressionType, bool finalizeArchiveOnClose)
: base(compressionType)
{
FinalizeArchiveOnClose = finalizeArchiveOnClose;
}

internal TarWriterOptions(WriterOptions options) : this(options.CompressionType, true)
{
}
}
}
2 changes: 1 addition & 1 deletion src/SharpCompress/Writers/WriterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
{
Expand Down
21 changes: 20 additions & 1 deletion tests/SharpCompress.Test/Tar/TarWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SharpCompress.Common;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Writers.Tar;
using Xunit;

namespace SharpCompress.Test.Tar
Expand Down Expand Up @@ -34,5 +36,22 @@ public void Tar_Rar_Write()
{
Assert.Throws<InvalidFormatException>(() => 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);
}
}
}
}

0 comments on commit f893c12

Please sign in to comment.