Added ability to leave tar archive open after stream is closed

This commit is contained in:
Dmitry Nesterov
2018-01-13 00:44:42 +01:00
parent 8f7ea420b3
commit f85fd1f6a4
5 changed files with 54 additions and 6 deletions

View File

@@ -182,7 +182,7 @@ namespace SharpCompress.Archives.Tar
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))

View File

@@ -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 @@ namespace SharpCompress.Writers.Tar
{
if (isDisposing)
{
PadTo512(0, true);
PadTo512(0, true);
if (finalizeArchiveOnClose) {
PadTo512(0, true);
PadTo512(0, true);
}
switch (OutputStream)
{
case BZip2Stream b:

View File

@@ -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)
{
}
}
}

View File

@@ -27,7 +27,7 @@ namespace SharpCompress.Writers
}
case ArchiveType.Tar:
{
return new TarWriter(stream, writerOptions);
return new TarWriter(stream, new TarWriterOptions(writerOptions));
}
default:
{

View File

@@ -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 @@ namespace SharpCompress.Test.Tar
{
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);
}
}
}
}