An ar archive stores named files together without compression. This example includes a long member name.
dotnet add package RecursiveAI.OneArchiveRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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");
Wrote files.aUnzip the example, 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.
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:
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.
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");
Wrote files.aRead 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.
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");
Archived 15 bytesRelated recipes
- Create a cpio archive in C#
- Compress and decompress Deflate64 in C#
- List RAR 5 files and extract a stored entry in C#
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 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 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.