Recursive AI

Create a Unix ar archive 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 Unix ar archive bundles files without compressing them. This example puts the download’s hello.txt and notes.txt into bundle.ar.

Terminal
dotnet add package RecursiveAI.OneArchive

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

C#
using RecursiveAI.OneArchive;
 
Archive.Create("bundle.ar")
    .Add("hello.txt", "hello.txt")
    .Add("notes.txt", "notes.txt")
    .Save();

Unzip the example, then dotnet run.

Writes bundle.ar with two members, named by the second argument to each Add call.

The full program

The program reads hello.txt and notes.txt from disk. Use the files in the download or replace the input paths with your own. Both sample files end with a newline:

Text
hello
Text
release notes

The full program reopens bundle.ar and lists the stored names and sizes.

C#
using RecursiveAI.OneArchive;
 
Archive.Create("bundle.ar")
    .Add("hello.txt", "hello.txt")
    .Add("notes.txt", "notes.txt")
    .Save();
 
using var archive = Archive.Open("bundle.ar");
foreach (var entry in archive.Entries)
    Console.WriteLine($"{entry.Name}: {entry.Size} bytes");
Text
hello.txt: 6 bytes
notes.txt: 14 bytes

An ar archive stores regular files, not directory entries or symlinks; the writer throws ArchiveUnsupportedException for unsupported entry kinds.

Variations

Choose ar with a different extension. Set Format explicitly when the output name doesn’t end in .ar or .a.

C#
using RecursiveAI.OneArchive;
 
var options = new ArchiveCreateOptions
{
    Format = ArchiveFormat.Ar
};
Archive.Create("bundle.bin", options)
    .Add("hello.txt", "hello.txt")
    .Save();

The short answer

Call Archive.Create("bundle.ar"), add each regular file with Add(inputPath, memberName), then call Save(). OneArchive selects ar from the extension. Use Archive.Open to read back the members; you don’t need to launch the ar command from C#.

What is the Unix ar archive format?

An ar file stores members one after the other. Each member has a header with its name, byte length, and metadata such as mode and modification time, followed by its original bytes. Unlike a compressed ZIP entry, an ar member’s data isn’t compressed. The GNU ar manual describes how ar collects and retrieves ordinary file members.

The OneArchive writer uses the GNU variant by default. It writes short names inline and puts long names in a table before the members. That means the writer buffers members until the archive is finalized, spilling to a temporary file after its memory allowance. If temporary files aren’t available and the archive exceeds that allowance, writing fails. BSD and Common variants stream members instead; Common refuses names over 15 bytes, while BSD can put longer names at the start of each member’s data.

Is an ar archive the same as a static library?

Not necessarily. A static library can use the ar container, but its members are object files rather than the text files used here. A linker can also use a symbol index to find the object file that defines a symbol. This recipe doesn’t build a symbol table or prepare files for a linker. Naming the output bundle.a selects the same container format; it doesn’t turn its contents into a static library.

Use plain ar when another program expects a regular ar container. For a file bundle that must preserve directory entries, choose tar instead.

All onearchive recipes