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:
{