Working with CPIO Archives

Overview

CPIO (Copy In, Copy Out) is an archive format used primarily in Unix-like operating systems for packaging multiple files and directories into a single archive. Although less commonly used than formats such as TAR or ZIP, CPIO remains relevant for backups, software packaging, and interoperability with Unix tooling.

Aspose.ZIP for .NET can both create and extract CPIO archives. The API supports the common CPIO header variants OldBinary, OldAscii, NewAscii, and NewAsciiCrc, so you can package files, compress the resulting archive, or unpack existing .cpio files in .NET applications.

Collecting without compression

The following example shows how to combine two files into a CPIO archive:

 1    using (FileStream cpioFile = File.Open("combined.cpio", FileMode.Create))
 2    {
 3        FileInfo fi1 = new FileInfo("alice29.txt");
 4        FileInfo fi2 = new FileInfo("asyoulik.txt");
 5
 6        using (CpioArchive archive = new CpioArchive())
 7        {
 8            archive.CreateEntry("alice29.txt", fi1);
 9            archive.CreateEntry("asyoulik.txt", fi2);
10            archive.Save(cpioFile, CpioFormat.NewAscii);
11        }
12    }

Compressing cpio archive

On Unix-like operating systems, the cpio utility allows compressing cpio archive to gzip on creation. Aspose.ZIP mirrors this functionality through the SaveGzipped method.

1    using (var archive = new CpioArchive())
2    {
3        archive.CreateEntries("C:\\folder", false);
4        archive.SaveGzipped("result.cpio.gz");
5    }

Nowadays xz utility has become popular in Linux and Unix. Its compression of cpio is seamlessly integrated into Aspose.Zip. Use the SaveXzCompressed method of a cpio archive.

1    using (var archive = new CpioArchive())
2    {
3        archive.CreateEntries("C:\\folder", false);
4        archive.SaveXzCompressed("result.cpio.xz");
5    }

Aspose.ZIP for .NET is not limited to gzip and xz wrappers for CPIO archives. You can also save a compressed archive with lzip, LZMA, Zstandard, or classic Unix compress format by using SaveLzipped for .cpio.lz, SaveLZMACompressed for .cpio.lzma, SaveZstandard for .cpio.zst, and SaveZCompressed for .cpio.Z.

Extract whole CPIO archive

To unpack the complete contents of a CPIO archive, call ExtractToDirectory:

1    using (CpioArchive archive = new CpioArchive("archive.cpio"))
2    {
3        archive.ExtractToDirectory("extracted");
4    }

Extract selected CPIO entries

If you only need one or two files, extract entries directly to a file path or stream:

1    using (CpioArchive archive = new CpioArchive("archive.cpio"))
2    {
3        archive.Entries[0].Extract("alice29.txt");
4
5        using (MemoryStream output = new MemoryStream())
6        {
7            archive.Entries[1].Extract(output);
8        }
9    }

You can also open an entry as a readable stream and copy it wherever needed:

1    using (CpioArchive archive = new CpioArchive("archive.cpio"))
2    using (FileStream extracted = File.Create("alice29.txt"))
3    using (Stream decompressed = archive.Entries[0].Open())
4    {
5        decompressed.CopyTo(extracted);
6    }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.