---
title: "Create an ar archive in C#"
url: https://recursiveai.org/onearchive/cookbook/create-ar-archive/
description: "Create GNU and BSD ar archives in C# with long member names, then reopen them and read the stored bytes with OneArchive."
version: "1.2.0"
---

# Create an ar archive in C#

**OneArchive** is a free, open-source archive and compression library for .NET from Recursive AI: archives (zip, tar, 7z, RAR 5, cpio, ar, ISO) in RecursiveAI.OneArchive, codecs (zlib, gzip, xz, Zstandard, Brotli) 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-ar-archive/create-ar-archive.zip)

An ar archive stores named files together without compression. This example includes a long member name.

```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;
using RecursiveAI.OneArchive.Ar;

using var output = File.Create("files.a");
using var writer = new ArWriter(output);
writer.WriteEntry(
    new ArchiveEntry
    {
        Name = "a-long-member-name.txt",
        Size = 15
    },
    "Hello archive!\n"u8.ToArray());
writer.Finish();
Console.WriteLine("Wrote files.a");
```

```text
Wrote files.a
```

Unzip [the example](https://recursiveai.org/onearchive/cookbook/create-ar-archive/create-ar-archive.zip), then `dotnet run`.

## The full program

The example creates its input bytes in code: `Hello archive!` followed by a newline. It writes `files.a`, reopens it and checks the member’s text.

```csharp
using RecursiveAI.OneArchive;
using RecursiveAI.OneArchive.Ar;

using (var output = File.Create("files.a"))
using (var writer = new ArWriter(output))
{
    writer.WriteEntry(
        new ArchiveEntry { Name = "a-long-member-name.txt", Size = 15 },
        "Hello archive!\n"u8.ToArray());
    writer.Finish();
}
Console.WriteLine("Wrote files.a");
using var input = File.OpenRead("files.a");
using var reader = new ArReader(input);
while (reader.GetNextEntry() is { } entry)
{
    using var content = new StreamReader(reader.OpenEntryStream());
    string text = content.ReadToEnd();
    if (text != "Hello archive!\n")
        throw new InvalidDataException("Content changed");
    Console.WriteLine($"{entry.Name}: {entry.Size} bytes");
    Console.Write(text);
}
```

`dotnet run` prints:

```text
Wrote files.a
a-long-member-name.txt: 15 bytes
Hello archive!
```

If you pass a directory or link to `ArWriter`, it throws `ArchiveUnsupportedException`.

## Variations

Write BSD long names instead of a GNU name table. This complete snippet replaces the snippet at the top.

```csharp
using RecursiveAI.OneArchive;
using RecursiveAI.OneArchive.Ar;

using var output = File.Create("files.a");
using var writer = new ArWriter(output,
    new() { Format = ArFormat.Bsd });
writer.WriteEntry(
    new ArchiveEntry { Name = "a-long-member-name.txt", Size = 15 },
    "Hello archive!\n"u8.ToArray());
writer.Finish();
Console.WriteLine("Wrote files.a");
```

```text
Wrote files.a
```

Read a real file from disk instead. `hello.txt` comes in the download and contains `Hello archive!` followed by a newline. `Size` must equal the content’s byte length; `content.Length` computes it here.

```csharp
using RecursiveAI.OneArchive;
using RecursiveAI.OneArchive.Ar;

byte[] content = File.ReadAllBytes("hello.txt");
using var output = File.Create("from-disk.a");
using var writer = new ArWriter(output);
writer.WriteEntry(new ArchiveEntry
{
    Name = "hello.txt", Size = content.Length
}, content);
writer.Finish();
Console.WriteLine($"Archived {content.Length} bytes");
```

```text
Archived 15 bytes
```

## Related recipes

- [Create a cpio archive in C#](https://recursiveai.org/onearchive/cookbook/create-cpio-archive/)
- [Compress and decompress Deflate64 in C#](https://recursiveai.org/onearchive/cookbook/compress-decompress-deflate64/)
- [List RAR 5 files and extract a stored entry in C#](https://recursiveai.org/onearchive/cookbook/list-rar5-and-extract-stored-entry/)

## The short answer

Use `ArWriter` from RecursiveAI.OneArchive, add each regular file with `WriteEntry`, and finish the writer before opening the result. The default is GNU ar. The example deliberately uses a name that needs the GNU long-name table, then reads the content back with `ArReader`. No command-line archiver is involved.

The `Name` is the member name stored inside the archive, not a source file path. The bytes passed to `WriteEntry` supply its content. `Size` must match that content’s byte length; for the UTF-8 literal in this example it is 15. Count bytes rather than characters when replacing it with non-ASCII text.

## What is an AR archive?

An ar archive is a collection of named files, often object files. [IBM’s ar command reference](https://www.ibm.com/docs/en/zos/3.1.0?topic=descriptions-ar-create-maintain-library-archives) describes those files as members and notes that a member can be an arbitrary file. A filename ending in `.a` does not by itself mean that the contents are executable or linkable.

OneArchive’s [ar documentation](https://recursiveai.org/onearchive/docs/cpio-ar/#ar) describes a signature followed by member headers and their data. There is no compression layer or end trailer. A text member, as used here, is enough to demonstrate the container without depending on an object compiler.

An object-library symbol index is a separate concern. `ArWriter` does not build one from object files. Its GNU writer can store a caller-built symbol table through `SetSymbolTable`; merely adding `.o` members is not a promise of a link-ready static library. Likewise, writing an ar container is not a complete Debian package construction workflow.

## How do I create an ar archive in C# with long filenames?

Choose the name encoding your consumer expects. GNU is the default: short names go directly into headers and longer names go into a `//` table before the members. BSD writes a long name at the start of its member data. The Common variant has no long-name mechanism and refuses names over 15 bytes. The variation above writes the same payload with BSD encoding.

GNU output cannot emit the first member immediately because its long-name table must come first. The writer holds members until `Finish`, using memory up to `MaxMemoryBuffer` and then a temporary file. BSD and Common stream their members. Finish and close the writer before reopening the output, as the full program does; reading it while it is still pending is not equivalent.

Only regular files belong in this container. There are no directory or link entries to preserve a filesystem tree. For missing metadata, the writer uses mode `100644`, owner and group zero, and time zero rather than the current clock. Specify metadata explicitly when your consumer needs different values. The reader normally hides GNU name tables and symbol tables from the file listing, so the example reports only the member you added.
