TAR.GZ Archives Merging in C#
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.
1 string[] archivesPaths = new string[] { "data/first.tar.gz",
2 "data/second.tar.gz", "data/third.tar.gz" };
3 TarArchive[] archives = new TarArchive[archivesPaths.Length];
4
5 using (TarArchive mergred = new TarArchive())
6 {
7 for (int i = 0; i < archivesPaths.Length; i++)
8 {
9 TarArchive a = TarArchive.FromGZip(archivesPaths[i]);
10 archives[i] = a;
11 foreach (TarEntry entry in a.Entries)
12 mergred.CreateEntry(entry.Name, entry.Open());
13 }
14
15 mergred.SaveGzipped("merged.tar.gz");
16
17 for (int i = 0; i < archivesPaths.Length; i++)
18 {
19 archives[i].Dispose();
20 }
21 }
Similar approach for other archives
You can use the same approach to combine tar.lz, tar.xz, tar.z.