Skip to content
4 changes: 4 additions & 0 deletions src/libraries/System.Formats.Tar/ref/System.Formats.Tar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ public enum TarEntryType : byte
public static partial class TarFile
{
public static void CreateFromDirectory(string sourceDirectoryName, System.IO.Stream destination, bool includeBaseDirectory) { }
public static void CreateFromDirectory(string sourceDirectoryName, System.IO.Stream destination, bool includeBaseDirectory, System.Formats.Tar.TarEntryFormat format) { }
public static void CreateFromDirectory(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory) { }
public static void CreateFromDirectory(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory, System.Formats.Tar.TarEntryFormat format) { }
public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, System.IO.Stream destination, bool includeBaseDirectory, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, System.IO.Stream destination, bool includeBaseDirectory, System.Formats.Tar.TarEntryFormat format, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task CreateFromDirectoryAsync(string sourceDirectoryName, string destinationFileName, bool includeBaseDirectory, System.Formats.Tar.TarEntryFormat format, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static void ExtractToDirectory(System.IO.Stream source, string destinationDirectoryName, bool overwriteFiles) { }
public static void ExtractToDirectory(string sourceFileName, string destinationDirectoryName, bool overwriteFiles) { }
public static System.Threading.Tasks.Task ExtractToDirectoryAsync(System.IO.Stream source, string destinationDirectoryName, bool overwriteFiles, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
64 changes: 50 additions & 14 deletions src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarFile.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
Expand Down Expand Up @@ -253,5 +253,41 @@ public void SkipRecursionIntoBaseDirectorySymlink()

Assert.Null(reader.GetNextEntry());
}

[Theory]
[MemberData(nameof(GetTarEntryFormats))]
public void CreateFromDirectory_WithFormat(TarEntryFormat format)
{
using TempDirectory source = new TempDirectory();
using TempDirectory destination = new TempDirectory();

string fileName = "file.txt";
File.Create(Path.Join(source.Path, fileName)).Dispose();

string destinationArchiveFileName = Path.Join(destination.Path, "output.tar");
TarFile.CreateFromDirectory(source.Path, destinationArchiveFileName, includeBaseDirectory: false, format);

using FileStream fileStream = File.OpenRead(destinationArchiveFileName);
using TarReader reader = new TarReader(fileStream);

TarEntry entry = reader.GetNextEntry();
Assert.NotNull(entry);
Assert.Equal(format, entry.Format);
Assert.Equal(fileName, entry.Name);

Assert.Null(reader.GetNextEntry());
}

[Theory]
[MemberData(nameof(GetInvalidTarEntryFormats))]
public void CreateFromDirectory_InvalidFormat_Throws(TarEntryFormat format)
{
using TempDirectory source = new TempDirectory();
using TempDirectory destination = new TempDirectory();
string destinationArchiveFileName = Path.Join(destination.Path, "output.tar");

Assert.Throws<ArgumentOutOfRangeException>("format", () =>
TarFile.CreateFromDirectory(source.Path, destinationArchiveFileName, includeBaseDirectory: false, format));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,38 @@ public void NonExistentDirectory_Throws()
using MemoryStream archive = new MemoryStream();
Assert.Throws<DirectoryNotFoundException>(() => TarFile.CreateFromDirectory(sourceDirectoryName: dirPath, destination: archive, includeBaseDirectory: false));
}

[Theory]
[MemberData(nameof(GetTarEntryFormats))]
public void CreateFromDirectory_WithFormat(TarEntryFormat format)
{
using TempDirectory source = new TempDirectory();
string fileName = "file.txt";
File.Create(Path.Join(source.Path, fileName)).Dispose();

using MemoryStream archive = new MemoryStream();
TarFile.CreateFromDirectory(source.Path, archive, includeBaseDirectory: false, format);

archive.Position = 0;
using TarReader reader = new TarReader(archive);

TarEntry entry = reader.GetNextEntry();
Assert.NotNull(entry);
Assert.Equal(format, entry.Format);
Assert.Equal(fileName, entry.Name);

Assert.Null(reader.GetNextEntry());
}

[Theory]
[MemberData(nameof(GetInvalidTarEntryFormats))]
public void CreateFromDirectory_InvalidFormat_Throws(TarEntryFormat format)
{
using TempDirectory source = new TempDirectory();
using MemoryStream archive = new MemoryStream();

Assert.Throws<ArgumentOutOfRangeException>("format", () =>
TarFile.CreateFromDirectory(source.Path, archive, includeBaseDirectory: false, format));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
Expand Down Expand Up @@ -297,5 +297,45 @@ public async Task SkipRecursionIntoBaseDirectorySymlinkAsync()

Assert.Null(await reader.GetNextEntryAsync()); // subDirectory should not be found
}

[Theory]
[MemberData(nameof(GetTarEntryFormats))]
public async Task CreateFromDirectoryAsync_WithFormat(TarEntryFormat format)
{
using (TempDirectory source = new TempDirectory())
using (TempDirectory destination = new TempDirectory())
{
string fileName = "file.txt";
File.Create(Path.Join(source.Path, fileName)).Dispose();

string destinationArchiveFileName = Path.Join(destination.Path, "output.tar");
await TarFile.CreateFromDirectoryAsync(source.Path, destinationArchiveFileName, includeBaseDirectory: false, format);

await using (FileStream fileStream = File.OpenRead(destinationArchiveFileName))
await using (TarReader reader = new TarReader(fileStream))
{
TarEntry entry = await reader.GetNextEntryAsync();
Assert.NotNull(entry);
Assert.Equal(format, entry.Format);
Assert.Equal(fileName, entry.Name);

Assert.Null(await reader.GetNextEntryAsync());
}
}
}

[Theory]
[MemberData(nameof(GetInvalidTarEntryFormats))]
public async Task CreateFromDirectoryAsync_InvalidFormat_Throws(TarEntryFormat format)
{
using (TempDirectory source = new TempDirectory())
using (TempDirectory destination = new TempDirectory())
{
string destinationArchiveFileName = Path.Join(destination.Path, "output.tar");

await Assert.ThrowsAsync<ArgumentOutOfRangeException>("format", () =>
TarFile.CreateFromDirectoryAsync(source.Path, destinationArchiveFileName, includeBaseDirectory: false, format));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,46 @@ public async Task NonExistentDirectory_Throws_Async()
}
}
}

[Theory]
[MemberData(nameof(GetTarEntryFormats))]
public async Task CreateFromDirectoryAsync_WithFormat(TarEntryFormat format)
{
using (TempDirectory source = new TempDirectory())
{
string fileName = "file.txt";
File.Create(Path.Join(source.Path, fileName)).Dispose();

await using (MemoryStream archive = new MemoryStream())
{
await TarFile.CreateFromDirectoryAsync(source.Path, archive, includeBaseDirectory: false, format);

archive.Position = 0;
await using (TarReader reader = new TarReader(archive))
{
TarEntry entry = await reader.GetNextEntryAsync();
Assert.NotNull(entry);
Assert.Equal(format, entry.Format);
Assert.Equal(fileName, entry.Name);

Assert.Null(await reader.GetNextEntryAsync());
}
}
}
}

[Theory]
[MemberData(nameof(GetInvalidTarEntryFormats))]
public async Task CreateFromDirectoryAsync_InvalidFormat_Throws(TarEntryFormat format)
{
using (TempDirectory source = new TempDirectory())
{
await using (MemoryStream archive = new MemoryStream())
{
await Assert.ThrowsAsync<ArgumentOutOfRangeException>("format", () =>
TarFile.CreateFromDirectoryAsync(source.Path, archive, includeBaseDirectory: false, format));
}
}
}
}
}
18 changes: 17 additions & 1 deletion src/libraries/System.Formats.Tar/tests/TarTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
Expand Down Expand Up @@ -560,6 +560,22 @@ public static IEnumerable<object[]> GetTestTarFormats()
}
}

public static IEnumerable<object[]> GetTarEntryFormats()
{
yield return new object[] { TarEntryFormat.V7 };
yield return new object[] { TarEntryFormat.Ustar };
yield return new object[] { TarEntryFormat.Pax };
yield return new object[] { TarEntryFormat.Gnu };
}

public static IEnumerable<object[]> GetInvalidTarEntryFormats()
{
yield return new object[] { TarEntryFormat.Unknown };
yield return new object[] { (TarEntryFormat)67 };
yield return new object[] { (TarEntryFormat)int.MaxValue };
yield return new object[] { (TarEntryFormat)int.MinValue };
}

public static IEnumerable<object[]> GetFormatsAndLinks()
{
foreach (TarEntryFormat format in new[] { TarEntryFormat.V7, TarEntryFormat.Ustar, TarEntryFormat.Pax, TarEntryFormat.Gnu })
Expand Down
Loading