Recursive AI

Read sparse TAR entry sizes in C#

OneArchive is a free, open-source archive and compression library for .NET from Recursive AI: zip, tar, 7z, RAR and more in RecursiveAI.OneArchive, with compression codecs in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

A sparse TAR maps a file’s zero-filled gaps instead of storing them. In the included sparse.tar, StoredSize counts entry data, not the archive’s size on disk.

Terminal
dotnet add package RecursiveAI.OneArchive

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneArchive.Tar;
 
using var input = File.OpenRead(
    "sparse.tar");
using var tar = new TarReader(input);
while (tar.GetNextEntry() is { } entry)
{
    Console.WriteLine(entry.Name);
    Console.WriteLine(
        $"Stored: {entry.StoredSize}");
    Console.WriteLine($"Logical: {entry.Size}");
}

For sparse.tar in the download, the sizes are in bytes:

Text
disk.img
Stored: 4096
Logical: 1048576

Unzip the example, then dotnet run.

The sample wraps disk.img, a 1 MiB file starting with start and a newline, followed by zeros. The TAR represents the tail as a hole rather than storing every zero. MaxTotalSize is a safety cap on the total unpacked size (1 GiB by default). If the sum of the declared logical sizes exceeds it, the reader throws CompressionLimitException even when you only list headers.

Variations

See where the stored bytes go. Inside the while loop, replace the two size lines with this code to print the region map:

C#
foreach (var region in entry.SparseRegions)
    Console.WriteLine(
        $"{region.Offset}: {region.Length} bytes");
Text
disk.img
0: 4096 bytes
1048576: 0 bytes

The short answer

Use OneArchive’s TarReader to get TarEntry objects. Read StoredSize for the number of entry data bytes stored in the TAR and Size for the full logical size, including the holes that read back as zeros. You don’t need to open the entry’s data stream or write a file to get either number.

Why is the stored size smaller than the logical size?

A sparse entry has a map of the data regions actually stored. Each region says where its bytes belong in the logical file. Positions not covered by data read back as zeros, so a program can see a 1 MiB disk image even though this sample stores just 4,096 bytes of data. The stored region includes any zeros recorded within that region, not just the six bytes of text at the beginning. The map can include a zero-length region, as the example shows at the end of the file.

The archive also contains headers and padding. StoredSize doesn’t tell you how much disk space the restored file will occupy. The logical size describes what an application sees when it reads the file; the physical allocation depends on how the file is written and on the destination file system.

Does TAR support sparse files?

OneArchive reads the old GNU sparse header used by the sample and GNU sparse PAX versions 0.0, 0.1, and 1.0. The sparse TAR documentation describes where those formats put the map. Use SparseFormat to identify which form the reader found, and SparseRegions when you need the offsets as well as the two sizes. For an ordinary file, there are no sparse regions, and there is no hole to account for when comparing stored and logical data size.

All onearchive recipes