---
title: "Create a Unix ar archive in C#"
url: https://recursiveai.org/onearchive/cookbook/create-unix-ar-archive/
description: "Create a Unix ar archive from regular files in C# with OneArchive. Write a .ar file and reopen it to list the stored members."
version: "1.2.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneArchive) · [GitHub](https://github.com/recursive-ai-oss/onearchive-csharp) · [Docs](https://recursiveai.org/onearchive/docs/) · [Download example (.zip)](https://recursiveai.org/onearchive/cookbook/create-unix-ar-archive/create-unix-ar-archive.zip)

A Unix `ar` archive bundles files without compressing them. This example puts the download’s `hello.txt` and `notes.txt` into `bundle.ar`.

```shell
dotnet add package RecursiveAI.OneArchive
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneArchive;

Archive.Create("bundle.ar")
    .Add("hello.txt", "hello.txt")
    .Add("notes.txt", "notes.txt")
    .Save();
```

Unzip [the example](https://recursiveai.org/onearchive/cookbook/create-unix-ar-archive/create-unix-ar-archive.zip), 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.

```csharp
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`.

```csharp
using RecursiveAI.OneArchive;

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

## Related recipes

- [Create a tar archive](https://recursiveai.org/onearchive/cookbook/create-tar-archive/)
- [Create a ZIP file](https://recursiveai.org/onearchive/cookbook/create-zip/)

## 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](https://ftp.gnu.org/old-gnu/Manuals/binutils-2.12/html_node/binutils_1.html) describes how ar collects and retrieves ordinary file members.

The [OneArchive writer](https://recursiveai.org/onearchive/docs/cpio-ar/#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](https://recursiveai.org/onearchive/cookbook/create-tar-archive/) instead.
