DEV Community

loading...

Open or create TAR files in C# with Aspose.ZIP

panchenko-dm-aspose
My name is Dmitry Panchenko. I am a developer at Aspose where we create great APIs for various file formats.
・2 min read

Prerequisite – C# ZIP Library

Aspose.ZIP for .NET is a powerful and easy-to-use API for zipping or unzipping files and folders within .NET applications. It supports all the most popular ZIP formats and provides AES encryption techniques to encrypt the files in ZIP archives. You can install the API from NuGet or download its binaries from the Aspose Downloads section.

How to create a TAR in .NET

The next part of the code demonstrates the creation of a new TAR archive:

using (var archive = new TarArchive())
{
    archive.CreateEntry("alice29.txt", File.OpenRead( "alice29.txt"));
    archive.Save("Archive.tar");
}
Enter fullscreen mode Exit fullscreen mode

That's all, after 3 lines of code the TAR archive has been saved to the disk!

Other TAR standards.

There are several different TAR standards: ustar, GNU tar, pax and V7. Ustar format is the default implementation for the most Windows applications, while GNU tar is the default for Linux command shell. Aspose.ZIP currently supports ustar and GNU tar. The Aspose.ZIP library creates archives in ustar format by default. To create a GNU tar archive the TAR format must be explicitly specified:

using (var archive = new TarArchive())
{
    archive.CreateEntry("alice29.txt", File.OpenRead( "alice29.txt"));
    archive.Save("Archive.tar", TarFormat.Gnu);
}
Enter fullscreen mode Exit fullscreen mode

Ustar vs GNU Tar. What's the difference?

In what cases it’s better to use GNU tar than ustar?

  1. Ustar has some limitations compared to GNU tar, for example, ustar doesn’t support entries with names more than 100 characters, the total size of the archive can’t be more than 8 Gb. If created tarball isn’t fit for these restrictions Aspose.ZIP will save the archive in GNU tar format even without passing TarFormat.Gnu in Save method.
  2. Another difference between standards is that GNU tar supports saving sparse files in the archive.

Compress TAR using GZip.

Tarball can be compressed to GZip using Aspose.ZIP:

using (FileStream tarGzStream = File.OpenWrite("Archive.tar.gz"))
{
    using (var archive = new TarArchive())
    {
        archive.CreateEntry("alice29.txt", File.OpenRead("alice29.txt"));
        archive.SaveGzipped(tarGzStream);
    }
}
Enter fullscreen mode Exit fullscreen mode

Learn more about Aspose.ZIP for .NET

Aspose.ZIP provides features to work with many other zip standards. Written totally on managed code Aspose.ZIP will be a good choice for applications that run on different OS.
Explore more about our C# ZIP API using the following resources:

Check what Aspose.ZIP is capable of with our free web-apps that are completely based on Aspose.ZIP for .NET:

Discussion (0)