Collect files to tar and compress

Tar purpose

Tar 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 tar archive.

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

Compressing tar archive

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

1try (TarArchive archive = new TarArchive()) {
2    archive.createEntry("text.txt", "D:\\texts\\article.txt");
3    archive.createEntry("picture.png", "D:\\Picture\\photo.png");
4    archive.saveGzipped("result.tar.gz");
5}

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

1try (TarArchive archive = new TarArchive()) {
2    archive.createEntry("text.txt", "D:\\texts\\article.txt");
3    archive.createEntry("picture.png", "D:\\Picture\\photo.png");
4    archive.saveXzCompressed("result.tar.xz");
5}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.