All formats

SharpCompress

LanguageC# (.NET Framework 4.8, .NET Standard 2.0, .NET 6+)
LicenseMIT
Repositorygithub.com/adamhathcock/sharpcompress
DocumentationUSAGE.md, FORMATS.md
Depends onnothing (managed code)
Read / writeyes / yes; tar, tar.gz, tar.bz2, tar.lz read and write; tar.xz, tar.zst, tar.Z read only

SharpCompress is a managed library for many archive formats: ZIP, TAR, 7z, RAR (read), GZip and more. For TAR it has two APIs:

It writes GNU-style long names by default (TarHeaderWriteFormat.GNU_TAR_LONG_LINK), or strict ustar. It doesn’t write pax headers. Version 0.50 renamed many methods (Open became OpenArchive / OpenReader / OpenWriter); the examples below use the current names.

Installation#

Shell
dotnet add package SharpCompress

Basic usage: list and extract a .tar.gz#

ReaderFactory.OpenReader detects the compression and the archive format from the data:

C#
using SharpCompress.Common;
using SharpCompress.Readers;

using Stream stream = File.OpenRead("archive.tar.gz");
using var reader = ReaderFactory.OpenReader(stream);
while (reader.MoveToNextEntry())
{
    if (reader.Entry.IsDirectory)
        continue;
    Console.WriteLine($"{reader.Entry.Size,10} {reader.Entry.Key}");
    reader.WriteEntryToDirectory("out", new ExtractionOptions
    {
        ExtractFullPath = true,  // keep folder structure
        Overwrite = true,
    });
}

How to extract a whole tar.xz or tar.zst#

The same code works for every compression SharpCompress can read; WriteAllToDirectory loops for you:

C#
using SharpCompress.Common;
using SharpCompress.Readers;

using Stream stream = File.OpenRead("linux.tar.xz");
using var reader = ReaderFactory.OpenReader(stream);
reader.WriteAllToDirectory("out", new ExtractionOptions { ExtractFullPath = true, Overwrite = true });

How to create a .tar.gz from a folder#

C#
using SharpCompress.Common;
using SharpCompress.Writers;

using Stream stream = File.Create("backup.tar.gz");
using var writer = WriterFactory.OpenWriter(
    stream, ArchiveType.Tar, WriterOptions.ForTar(CompressionType.GZip));
writer.WriteAll("docs", "*", SearchOption.AllDirectories);

Use CompressionType.BZip2 or CompressionType.LZip for other compressors, CompressionType.None for a plain .tar.

How to write entries from memory#

C#
using System.Text;
using SharpCompress.Common;
using SharpCompress.Writers;

using Stream stream = File.Create("out.tar");
using var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, WriterOptions.ForTar());

using var data = new MemoryStream(Encoding.UTF8.GetBytes("hello\n"));
writer.Write("notes/hello.txt", data, DateTime.Now);

TAR needs the size before the data: pass a seekable stream (like MemoryStream or FileStream), or the writer throws.

How to read one entry of an uncompressed .tar#

TarArchive reads the headers and gives random access to the entries:

C#
using SharpCompress.Archives;
using SharpCompress.Archives.Tar;

using var archive = TarArchive.OpenArchive("archive.tar");
foreach (var entry in archive.Entries)
{
    if (entry.Key == "docs/readme.txt")
    {
        using var s = entry.OpenEntryStream();
        using var sr = new StreamReader(s);
        Console.WriteLine(sr.ReadToEnd());
    }
}

Tips#