Skip to content

A modern and standards compliance TAR reader/writer for .NET

License

Notifications You must be signed in to change notification settings

ultimaweapon/tape-archive

Repository files navigation

TapeArchive

Nuget

Usage

Before using this library you need to understand some basic of TAR structure. DO NOT SKIP THIS otherwise you won't known how to properly working with this library.

TAR is acronym for Tape Archive. It was designed for writing and reading to/from a tape drive. That mean its structure was designed for sequential access, not random access. If you want to seek to a specific file in a TAR what most library actually do is keep reading and discard all data until it reach that file. And TAR does not support compression by itself. The compression you see like file.tar.gz is just a TAR that compressed witgh GZIP later.

TAR format have a lot of variants:

  1. Original TAR that shipped with AT&T UNIX Version 7
  2. Pre-POSIX (AKA. POSIX.1-1988 draft)
  3. POSIX.1-1988 (AKA. ustar)
  4. pax (AKA. POSIX.1-2001)
  5. GNU
  6. Solaris
  7. AIX
  8. macOS

Usually most reader will be able to extract any variants. This library currently support up to ustar. But just as I said before that most reader will be able to extract any variants, including this library. So you should not have any problem when reading. For writing try to stick with ustar due to some reader like GNU Tar does not handle file mode properly for origial variant.

Reading

using TapeArchive;

await using var reader = new TapeArchive(stream, true);

await foreach (var item in reader.ReadAsync())
{
    // Do something with item.
}

Writing

using System;
using TapeArchive;

await using var builder = new ArchiveBuilder(stream, true);
var item = new UstarItem(PrePosixType.RegularFile, new("./file1"))
{
    Content = content,
    Size = size,
};

await builder.WriteItemAsync(item, null);
await builder.CompleteAsync();

Breaking changes

2.0 to 3.0

IArchiveBuilder.WriteItemAsync has been added a parameter to specify how to create parent entries.

1.0 to 2.0

Disposing of IArchiveBuilder is changed. In 1.0 it will complete the archive. For 2.0 it will abort the archive if archive is not completed with CompleteAsync. The aborted archive is a broken TAR and cannot be read by any TAR readers.

Development

Prerequisites

  • Latest .NET SDK

Build

dotnet build src/TapeArchive.sln

Run tests

dotnet test src/TapeArchive.sln

License

MIT

About

A modern and standards compliance TAR reader/writer for .NET

Topics

Resources

License

Stars

Watchers

Forks

Languages