Unpack sample.cpio (in the zip), containing two text files.
dotnet add package RecursiveAI.OneArchiveRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneArchive; Archive.Extract("sample.cpio", "out"); Console.WriteLine( File.ReadAllText("out/hello.txt"));
hello from cpioCreates out/hello.txt and out/docs/info.txt; use a fresh destination.
Unzip the example, then dotnet run.
The full program
The example’s original, redistributable CRC-format fixture contains:
hello.txt hello from cpio docs/info.txt two files
Each file ends with a newline. The program lists regular files, then extracts with explicit entry, size and path budgets. Its fresh destination is a unique subfolder of extracted/.
using RecursiveAI.OneArchive; var limits = new ArchiveLimits { MaxEntryCount = 20, MaxTotalSize = 1024 * 1024, MaxPathLength = 120, MaxPathDepth = 8, MaxExpansionRatio = 100 }; using (var archive = Archive.Open("sample.cpio", new ArchiveOpenOptions { Limits = limits })) { foreach (var entry in archive.Entries) if (entry.Kind == ArchiveEntryKind.File) Console.WriteLine($"{entry.Name}: {entry.Size} bytes"); } string destination = Path.Combine("extracted", "cpio-" + Guid.NewGuid().ToString("N")); Archive.Extract("sample.cpio", destination, new ArchiveExtractOptions { Limits = limits, Symlinks = LinkPolicy.Skip, HardLinks = LinkPolicy.Skip, SpecialFiles = SpecialFilePolicy.Skip }); Console.WriteLine(File.ReadAllText(Path.Combine(destination, "hello.txt")).TrimEnd()); Console.WriteLine(File.ReadAllText(Path.Combine(destination, "docs/info.txt")).TrimEnd());
dotnet run prints:
hello.txt: 16 bytes docs/info.txt: 10 bytes hello from cpio two files
Unsafe paths raise ArchiveSafetyException, a size budget hit raises CompressionLimitException, and a file that fails midway is deleted while earlier extracted files remain.
Variations
Extract one named file. This standalone snippet uses the same sample.cpio; selected must not already contain that file.
using RecursiveAI.OneArchive; Archive.Extract("sample.cpio", "selected", new ArchiveExtractOptions { Filter = e => e.Name == "docs/info.txt" }); Console.WriteLine( File.ReadAllText("selected/docs/info.txt"));
two filesRelated recipes
The short answer
To extract a CPIO archive in C#, call Archive.Extract with the archive path and destination directory. OneArchive detects the container and applies shared extraction path checks and resource limits. Use Archive.Open and Entries to list the contents without writing them to disk.
Supported CPIO dialects are newc (070701), crc (070702) and odc (070707). The old binary format is recognized but raises ArchiveUnsupportedException. These are the released package’s documented CPIO rules, not a promise that every historical CPIO variant is accepted.
How do I extract a CPIO archive to a directory?
Use a fresh directory so a prior run cannot collide with the archive’s names. By default, an existing destination file is an error rather than an overwrite. The minimal snippet applies the standard limits; the full program sets smaller limits appropriate for its two tiny files. Choose budgets for your application’s expected input, not just the extension on the uploaded file.
MaxTotalSize caps the cumulative declared content, MaxEntryCount caps headers admitted, and the path limits bound name length and directory depth. The example also keeps an expansion-ratio bound for compressed input. That ratio check starts only after more than 32 MiB has been produced; it is not a replacement for a small total-size budget. The sample’s 1 MiB total budget is the relevant cap here.
Path traversal and link-write-through checks are part of extraction. Do not build destination paths by simply joining an arbitrary entry name to a directory and copying the bytes yourself. The full program skips symbolic links, hard-link entries and special files explicitly. Defaults otherwise allow lexically safe symbolic links, handle generic hard links as copies of earlier extracted files, and reject device and FIFO entries. The extractor never creates device nodes or FIFOs.
Extraction is not transactional. A later error does not remove earlier successful files. The shared extraction behavior also states that races with other processes changing the destination are not defended against. Use a destination your application controls, rather than a directory concurrently modified by unrelated processes.
How do I extract a specific file from a CPIO archive?
Use ArchiveExtractOptions.Filter to select an entry by its archive name, as in the variation. This retains the extraction path rules instead of substituting a manual file-copy loop. Skipped entries still count against limits, so selecting one name does not turn an unbounded archive scan into a bounded one.
CPIO hard-link stubs need particular care. On a seekable source, the reader can find the later member carrying the same inode’s data and expose that content for an earlier zero-size stub. On forward-only input, or with ResolveLinkStubs disabled, that stub is empty. These CPIO regular-file stubs are not generic HardLink entries, so the extraction link policy does not remove this format-level behavior.
How do I read a CPIO archive in C# and check its checksum?
The full program enumerates regular-file names and lengths through Archive.Open. Listing is not content verification. For crc-format regular files, the reader checks the stored sum only when the data is read from its first byte through its last byte in order; extraction does that. Skipping an entry or seeking through its data does not perform that check.
Despite the name, CPIO’s crc field is a 32-bit sum of data bytes, not the CRC-32 algorithm used in ZIP. newc and odc do not supply that data checksum. The reader also requires the TRAILER!!! end entry by default, validates numeric fields and names, and rejects malformed headers. Those structural checks matter even when the selected dialect has no content checksum.