SharpCompress
| Language | C# (.NET Framework 4.8, .NET Standard 2.0, .NET 6+) |
|---|---|
| License | MIT |
| Repository | github.com/adamhathcock/sharpcompress |
| Documentation | USAGE.md, FORMATS.md |
| Depends on | nothing (managed code) |
| Read / write | yes / 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:
- Reader / Writer (
ReaderFactory,WriterFactory): forward-only streaming. This is the API for compressed tar: the reader detects gzip, bzip2, xz, zstd, lzip and LZW around the tar data. - Archive (
TarArchive,ArchiveFactory): random access to entries of an uncompressed.tar, and adding entries before saving.
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#
dotnet add package SharpCompress
Basic usage: list and extract a .tar.gz#
ReaderFactory.OpenReader detects the compression and the archive format from the data:
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:
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#
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#
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:
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#
ArchiveFactory.OpenArchiverefuses compressed tar; useReaderFactory.OpenReaderfor.tar.gz,.tar.xzand the rest.- Async versions exist:
ReaderFactory.OpenAsyncReader,WriterFactory.OpenAsyncWriter. - For pax output (needed by some tools for UTF-8 names and nanosecond times) use System.Formats.Tar.