TAR is a popular UNIX-based archiving format that is used to package multiple files including audios, videos, installers, etc. On the other hand, GNU is a data compression algorithm to compress large files before sharing. The TAR.GZ is the combination of TAR’s archiving and GNU’s compression. Similarly, XZ Utils also provides compression of TAR archives as TAR.XZ format. In this article, you will learn how to compress TAR archives and create TAR.GZ and TAR.XZ files in C#.
C# .NET API to Create TAR.GZ and TAR.XZ
Aspose.ZIP for .NET is a powerful API that allows you to create archives seamlessly from within your .NET applications. Furthermore, it lets you decompress and extract the archives of popular formats including ZIP, TAR, GZIP, BZ2, 7Zip, and RAR. We will use this API to create compressed TAR archives (.tar.gz and .tar.xz). You can either download the API’s DLL or install it directly using NuGet.
PM> Install-Package Aspose.Zip
Create a TAR.GZ File in C#
The following are the steps to create a compressed TAR file (tar.gz) in C#.
- Create an object of TarArchive class.
- Add files to archive using TarArchive.CreateEntry(string, string) method.
- Save the archive as TAR.GZ using TarArchive.SaveGzipped(string) method.
The following code sample shows how to create a TAR.GZ file in C#.
Create a TAR.XZ File in C#
XZ Utils is a popular utility that provides compression and decompression services. It compresses the TAR archives into TAR.XZ format. Aspose.ZIP for .NET also supports compression of TAR archives into TAR.XZ. The following are the steps to create a .tar.xz file in C#.
- Create a new FileStream object for .tar.xz file.
- Create an object of TarArchive class.
- Add files to archive using TarArchive.CreateEntry(string, string) method.
- Save archive as TAR.XZ using TarArchive.SaveXzCompressed(Stream) method.
The following code sample shows how to compress TAR into TAR.XZ format in C#.
Get a Free API License
You can get a free temporary license in order to use the API without evaluation limitations.
Conclusion
The compression and decompression of TAR archives are commonly performed on Unix/Linux operating systems. In this article, you have learned how to compress TAR archives and create TAR.GZ and TAR.XZ files in C#. You can simply install the API and integrate the provided code samples in your .NET applications to compress TAR archives. Furthermore, you can visit the documentation to explore more about Aspose.ZIP for .NET. Also, you can post your queries to our forum.