Merge any number of Tar.gz archives
Contents
[
Hide
Show
]If you want to combine arbitrary numbers of Tar.gz archives into single archive read this article.
Description
Aspose.ZIP API provides TarArchive class to extract and compose Tar archives, with or without compression. We can extract an archive to memory without saving it into intermediate storage and pass its entries to combined Tar archive right away.
Make sure you have enough virtual memory to keep content of all entries.
Transfer an Entry
The following code example demonstrates how to extract entries from several Tar.gz archives and immediately put it to tar archive. We compress it to gz in memory using saveGzipped method.
1String[] archivesPaths = new String[] { "data/first.tar.gz", "data/second.tar.gz", "data/third.tar.gz" };
2TarArchive[] archives = new TarArchive[archivesPaths.length];
3try (TarArchive merged = new TarArchive()) {
4 for (int i = 0; i < archivesPaths.length; i++) {
5 TarArchive a = TarArchive.fromGZip(archivesPaths[i]);
6 archives[i] = a;
7 for (TarEntry entry : a.getEntries()) {
8 merged.createEntry(entry.getName(), entry.open());
9 }
10 }
11
12 merged.saveGzipped("merged.tar.gz");
13
14 for (int i = 0; i < archivesPaths.length; i++) {
15 archives[i].close();
16 }
17}
Similar approach for other archives
You can use the same approach to combine tar.lz, tar.xz.