2025-07-20 17:35:22 +01:00
|
|
|
using System.IO;
|
2026-04-20 07:58:29 +01:00
|
|
|
using System.Linq;
|
2020-07-26 14:36:07 +01:00
|
|
|
using System.Text;
|
2026-04-20 07:58:29 +01:00
|
|
|
using SharpCompress.Archives.Tar;
|
2018-01-13 00:44:42 +01:00
|
|
|
using SharpCompress.Common;
|
2026-04-20 07:58:29 +01:00
|
|
|
using SharpCompress.Common.Tar.Headers;
|
2018-01-13 00:44:42 +01:00
|
|
|
using SharpCompress.Writers.Tar;
|
2015-12-30 11:19:42 +00:00
|
|
|
using Xunit;
|
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
namespace SharpCompress.Test.Tar;
|
|
|
|
|
|
|
|
|
|
public class TarWriterTests : WriterTests
|
2015-12-30 11:19:42 +00:00
|
|
|
{
|
2025-07-20 17:35:22 +01:00
|
|
|
static TarWriterTests()
|
|
|
|
|
{
|
|
|
|
|
#if !NETFRAMEWORK
|
|
|
|
|
//fix issue where these tests could not be ran in isolation
|
|
|
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 13:14:08 +00:00
|
|
|
public TarWriterTests()
|
|
|
|
|
: base(ArchiveType.Tar) => UseExtensionInsteadOfNameToVerify = true;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Tar_Writer() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.None,
|
|
|
|
|
"Tar.noEmptyDirs.tar",
|
|
|
|
|
"Tar.noEmptyDirs.tar",
|
|
|
|
|
Encoding.GetEncoding(866)
|
|
|
|
|
);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Tar_BZip2_Writer() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.BZip2,
|
|
|
|
|
"Tar.noEmptyDirs.tar.bz2",
|
|
|
|
|
"Tar.noEmptyDirs.tar.bz2",
|
|
|
|
|
Encoding.GetEncoding(866)
|
|
|
|
|
);
|
2017-05-31 16:51:24 +01:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Tar_LZip_Writer() =>
|
2022-12-20 15:06:44 +00:00
|
|
|
Write(
|
|
|
|
|
CompressionType.LZip,
|
|
|
|
|
"Tar.noEmptyDirs.tar.lz",
|
|
|
|
|
"Tar.noEmptyDirs.tar.lz",
|
|
|
|
|
Encoding.GetEncoding(866)
|
|
|
|
|
);
|
2018-01-13 00:44:42 +01:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Fact]
|
2022-12-20 15:20:49 +00:00
|
|
|
public void Tar_Rar_Write() =>
|
2025-04-28 16:18:01 +01:00
|
|
|
Assert.Throws<InvalidFormatException>(() =>
|
|
|
|
|
Write(CompressionType.Rar, "Zip.ppmd.noEmptyDirs.zip", "Zip.ppmd.noEmptyDirs.zip")
|
2022-12-20 15:06:44 +00:00
|
|
|
);
|
2018-01-13 00:44:42 +01:00
|
|
|
|
2026-04-21 08:25:16 +01:00
|
|
|
[Theory]
|
|
|
|
|
[InlineData(CompressionType.Xz)]
|
|
|
|
|
[InlineData(CompressionType.ZStandard)]
|
|
|
|
|
[InlineData(CompressionType.Lzw)]
|
|
|
|
|
public void Tar_UnsupportedWrapperCompression_Write(CompressionType compressionType) =>
|
|
|
|
|
Assert.Throws<InvalidFormatException>(() =>
|
|
|
|
|
Write(compressionType, "Zip.ppmd.noEmptyDirs.zip", "Zip.ppmd.noEmptyDirs.zip")
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
[Theory]
|
|
|
|
|
[InlineData(true)]
|
|
|
|
|
[InlineData(false)]
|
|
|
|
|
public void Tar_Finalize_Archive(bool finalizeArchive)
|
|
|
|
|
{
|
|
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
using Stream content = File.OpenRead(Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg"));
|
|
|
|
|
using (
|
|
|
|
|
var writer = new TarWriter(
|
|
|
|
|
stream,
|
|
|
|
|
new TarWriterOptions(CompressionType.None, finalizeArchive)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
writer.Write("doesn't matter", content, null);
|
2018-01-13 00:44:42 +01:00
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
|
|
|
|
|
var paddedContentWithHeader = (content.Length / 512 * 512) + 512 + 512;
|
|
|
|
|
var expectedStreamLength = finalizeArchive
|
|
|
|
|
? paddedContentWithHeader + (512 * 2)
|
|
|
|
|
: paddedContentWithHeader;
|
|
|
|
|
Assert.Equal(expectedStreamLength, stream.Length);
|
2015-12-30 11:19:42 +00:00
|
|
|
}
|
2026-04-20 07:58:29 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Tar_Ustar_HeaderFormat_WritesShortPath()
|
|
|
|
|
{
|
|
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
var options = new TarWriterOptions(CompressionType.None, true, TarHeaderWriteFormat.USTAR);
|
|
|
|
|
using (var writer = new TarWriter(stream, options))
|
|
|
|
|
using (var content = new MemoryStream(Encoding.UTF8.GetBytes("hello")))
|
|
|
|
|
{
|
|
|
|
|
writer.Write("dir/file.txt", content, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
using var archive = TarArchive.OpenArchive(stream);
|
|
|
|
|
Assert.Single(archive.Entries);
|
|
|
|
|
Assert.Equal("dir/file.txt", archive.Entries.Single().Key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Tar_Ustar_HeaderFormat_ThrowsForLongPath()
|
|
|
|
|
{
|
|
|
|
|
var longName = new string('a', 160) + ".txt";
|
|
|
|
|
|
|
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
var options = new TarWriterOptions(CompressionType.None, true, TarHeaderWriteFormat.USTAR);
|
|
|
|
|
using var writer = new TarWriter(stream, options);
|
|
|
|
|
using var content = new MemoryStream(Encoding.UTF8.GetBytes("hello"));
|
|
|
|
|
|
|
|
|
|
Assert.Throws<InvalidFormatException>(() => writer.Write(longName, content, null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Tar_GnuLongLink_HeaderFormat_WritesLongPath()
|
|
|
|
|
{
|
|
|
|
|
var longName = new string('a', 160) + ".txt";
|
|
|
|
|
|
|
|
|
|
using var stream = new MemoryStream();
|
|
|
|
|
var options = new TarWriterOptions(
|
|
|
|
|
CompressionType.None,
|
|
|
|
|
true,
|
|
|
|
|
TarHeaderWriteFormat.GNU_TAR_LONG_LINK
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
using (var writer = new TarWriter(stream, options))
|
|
|
|
|
using (var content = new MemoryStream(Encoding.UTF8.GetBytes("hello")))
|
|
|
|
|
{
|
|
|
|
|
writer.Write(longName, content, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
using var archive = TarArchive.OpenArchive(stream);
|
|
|
|
|
Assert.Single(archive.Entries);
|
|
|
|
|
Assert.Equal(longName, archive.Entries.Single().Key);
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
}
|