Skip to content

Instantly share code, notes, and snippets.

@danmoseley
Last active August 21, 2022 21:46
Show Gist options
  • Save danmoseley/384d30d0b2ed737888400d8fd778ba6d to your computer and use it in GitHub Desktop.
Save danmoseley/384d30d0b2ed737888400d8fd778ba6d to your computer and use it in GitHub Desktop.
test tars
// See https://aka.ms/new-console-template for more information
using System.Formats.Tar;
using Xunit;
public static class C
{
public async static Task Main()
{
List<Task> tasks = new();
foreach (string path in Directory.EnumerateFiles(@"C:\git\go\src\archive\tar\testdata", "*.tar"))
{
tasks.Add(Task.Run(async () =>
{
TarEntry? entry = null;
try
{
//Console.WriteLine($"{path} opening...");
using FileStream fs = new(path, FileMode.Open);
using TarReader reader = new(fs, leaveOpen: false);
while ((entry = await reader.GetNextEntryAsync()) != null)
{
var ms = new MemoryStream();
Assert.NotEmpty(entry.Name);
Assert.True(Enum.IsDefined(entry.EntryType));
Assert.True(Enum.IsDefined(entry.Format));
if (entry.EntryType == TarEntryType.Directory)
continue;
var ds = entry.DataStream;
if (ds != null && ds.Length > 0)
{
ds.CopyTo(ms);
}
}
}
catch (Exception ex) //when (!(ex is FormatException))
{
Console.WriteLine($"{path} opening {entry?.Name} threw {ex.Message}");
}
}));
}
await Task.WhenAll(tasks);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment