Recursive AI

Detect an archive format from file contents 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.

Open the archive by its contents, not its suffix. The sample bundle.zip in the download is really a gzip-compressed tar containing readme.txt and notes.txt.

Terminal
dotnet add package RecursiveAI.OneArchive

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

C#
using RecursiveAI.OneArchive;
 
using var archive = Archive.Open("bundle.zip");
Console.Write("Container: ");
Console.WriteLine(archive.Format);
Console.Write("Compression: ");
Console.WriteLine(archive.Compression);

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Text
Container: Tar
Compression: Gzip

The full program

Run dotnet run -- path/to/file to inspect another archive. It doesn’t extract anything.

C#
using RecursiveAI.OneArchive;
 
using var archive = Archive.Open(args.FirstOrDefault() ?? "bundle.zip");
Console.WriteLine($"Container: {archive.Format}");
Console.WriteLine($"Compression: {archive.Compression}");
Text
Container: Tar
Compression: Gzip

If neither the contents nor the filename suggest a supported format, Archive.Open throws CompressionFormatException.

Variations

Identify a stream without a filename. Replace the Archive.Open line at the top with these two lines. The output is the same for the sample.

C#
using var input = File.OpenRead("bundle.zip");
using var archive = Archive.Open(input);

Handle a file that isn’t an archive. Create message.txt containing Hello from a text file. and run this in place of the snippet at the top:

C#
using RecursiveAI.OneArchive;
using RecursiveAI.OneCompress;
 
try
{
    using var archive = Archive.Open("message.txt");
    Console.WriteLine(archive.Format);
}
catch (CompressionFormatException)
{
    Console.WriteLine("Not a recognized archive.");
}
Text
Not a recognized archive.

The short answer

Call Archive.Open and read both Format and Compression. For a gzip-compressed tar, the values are Tar and Gzip. Renaming that file to .zip doesn’t change either value: OneArchive finds the recognizable signatures first.

How do magic bytes identify an archive?

A magic byte sequence is a recognizable pattern in a file header. OneArchive first checks for an outer compression signature. Gzip starts with 1F 8B, for example. If there’s a recognized wrapper, OneArchive examines up to 512 bytes after decompression to identify the container inside it. A tar header can contain ustar at byte offset 257; some older tar headers can be recognized by their fields and header checksum instead. A plain ZIP file has its own header signatures, including a separate one for an empty ZIP. See the detection rules and supported formats.

The two properties help you decide what to do next. Format says how the entries are organized, while Compression describes the wrapper around that organization. A ZIP can report Compression = None even when its individual entries are compressed, because those methods belong to the entries, not to an outer wrapper. A gzip file with no recognized container or matching filename can report SingleStream: it holds one compressed payload, not a tar directory of entries.

Can you detect an archive format without a file extension?

Yes, when the bytes identify it. Pass a readable Stream if the input didn’t come with a filename, or if you want the result without any filename fallback. If you pass a path, a recognizable suffix can still be used when no signature matches. An empty uncompressed .tar is an example: it doesn’t provide a populated tar header to inspect. That’s why a successful format guess isn’t proof that all entries are readable. Damage farther into the file can still surface when you read the entries.

All onearchive recipes