mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-04 05:25:00 +00:00
Added ability to leave tar archive open after stream is closed
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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:
|
||||
|
||||
23
src/SharpCompress/Writers/Tar/TarWriterOptions.cs
Executable file
23
src/SharpCompress/Writers/Tar/TarWriterOptions.cs
Executable 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ namespace SharpCompress.Writers
|
||||
}
|
||||
case ArchiveType.Tar:
|
||||
{
|
||||
return new TarWriter(stream, writerOptions);
|
||||
return new TarWriter(stream, new TarWriterOptions(writerOptions));
|
||||
}
|
||||
default:
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user