Collect files to cpio and compress

Cpio purpose

Cpio format is similar to tar. Cpio archive allows you to combine several files into a single file. You may compress the joint file, too.

Collecting without compression

There is a sample of collecting two files into a cpio archive.

 1try (FileOutputStream cpioFile = new FileOutputStream("combined.cpio")) {
 2    File fi1 = new File("text.txt");
 3    File fi2 = new File("picture.png");
 4
 5    try (CpioArchive archive = new CpioArchive()) {
 6        archive.createEntry("text.txt", fi1);
 7        archive.createEntry("picture.png", fi2);
 8        archive.save(cpioFile);
 9    }
10} catch (IOException ex) {
11    System.out.println(ex);
12}

Compressing cpio archive

In Unix-like operating systems cpio utility allows compressing cpio archive to gzip on creation. Aspose.Zip provides similar functionality with the saveGzipped method.

1try (CpioArchive archive = new CpioArchive()) {
2    archive.createEntries("C:\\folder", false);
3    archive.saveGzipped("result.cpio.gz");
4}

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.

1try (CpioArchive archive = new CpioArchive()) {
2    archive.createEntries("C:\\folder", false);
3    archive.saveXzCompressed("result.cpio.xz");
4}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.