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.

 1using (FileStream tarFile = File.Open("joint.tar", FileMode.Create))
 2{
 3    FileInfo fi1 = new FileInfo("text.txt");
 4    FileInfo fi2 = new FileInfo("picture.png"));
 5
 6    using (var archive = new TarArchive())
 7    {
 8        archive.CreateEntry("text.txt", fi1);
 9        archive.CreateEntry("picture.png", fi2);
10        archive.Save(tarFile);
11    }
12}
view raw TarArchiving.cs hosted with ❤ by GitHub

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.

1using (var archive = new TarArchive())
2{
3     archive.CreateEntry("text.txt", @"D:\texts\article.txt");
4     archive.CreateEntry("picture.png", @"D:\Picture\photo.png");
5     archive.SaveGzipped("result.tar.gz");
6}

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.

1using (FileStream xzFile = File.Open("archive.tar.xz", FileMode.Create))
2{
3    using (var archive = new TarArchive())
4    {
5        archive.CreateEntry("text.txt", @"D:\texts\article.txt");
6        archive.CreateEntry("picture.png", @"D:\Picture\photo.png");
7        archive.SaveXzCompressed(xzFile);
8    }
9}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.