Packing and unpacking files using Tar archives in .NET

You must have come across files that were archives using the tar file format. Tar files are most often used on Unix systems like Linux but it happens that you need to deal with them in a .NET project.

You can find examples of .tar files throughout the Apache download pages, such this one. You’ll notice that .tar files are often also compressed using the GZip compression algorithm which together give the “.tar.gz” extension: they are files that were packed into a tar archive and then zipped using GZip. You can find an example of using GZip in .NET on this blog here. I have only little experience with Linux but I haven’t seen standalone “.tar” files yet, only ones that were compressed in some way. This is also the approach we’ll take in the example: pack and compress a group of files.

Tar files, as far as I know, do not compress the packaged files as opposed to zip files. So we can probably say that the Unix equivalent of .zip files are .tar.gz files. Feel free to correct these statements in the comments section if you are experienced with .tar and .tar.gz files.

Tar files are not supported in .NET out of the box but there’s a NuGet package that comes to the rescue:

sharpziplib nuget

Add this package to your .NET project. Suppose you’d like to compress the files in the c:\tar\start folder. Here’s a compact code example:

DirectoryInfo directoryOfFilesToBeTarred = new DirectoryInfo(@"c:\tar\start");
FileInfo[] filesInDirectory = directoryOfFilesToBeTarred.GetFiles();
String tarArchiveName = @"c:\tar\mytararchive.tar.gz";
using (Stream targetStream = new GZipOutputStream(File.Create(tarArchiveName)))
{
	using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
	{
		foreach (FileInfo fileToBeTarred in filesInDirectory)
		{
			TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
			tarArchive.WriteEntry(entry, true);
		}
	}
}

Note that other compression types such as Bzip2 and DEFLATE are available in the SharpZipLib library:

using (Stream targetStream = new BZip2OutputStream(File.Create(tarArchiveName), 9)) 
.
.
.
using (Stream targetStream = new DeflaterOutputStream(File.Create(tarArchiveName)))

We can then unpack the tar archive as follows:

FileInfo tarFileInfo = new FileInfo(@"c:\tar\mytararchive.tar.gz");
DirectoryInfo targetDirectory = new DirectoryInfo(@"c:\tar\finish");
if (!targetDirectory.Exists)
{
	targetDirectory.Create();
}
using (Stream sourceStream = new GZipInputStream(tarFileInfo.OpenRead()))
{
	using (TarArchive tarArchive = TarArchive.CreateInputTarArchive(sourceStream, TarBuffer.DefaultBlockFactor))
	{
		tarArchive.ExtractContents(targetDirectory.FullName);
	}				
}

Read all posts dedicated to file I/O here.

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Packing and unpacking files using Tar archives in .NET

  1. Thangasamy says:

    Hi Andras,

    After execution of this logic, I got below error

    Cannot access a closed file.

    at System.IO.__Error.FileNotOpen()
    at System.IO.FileStream.Flush(Boolean flushToDisk)
    at System.IO.FileStream.Flush()
    at ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream.Finish()
    at ICSharpCode.SharpZipLib.GZip.GZipOutputStream.Finish()
    at ICSharpCode.SharpZipLib.GZip.GZipOutputStream.Close()
    at System.IO.Stream.Dispose()
    at ZipSample.GZipForm.button1_Click(Object sender, EventArgs e) in D:\Workout\ZipSample\ZipSample\GZipForm.cs:line 87

    Please suggest me to resolve the issue.

    Thanks,
    Thangasamy

Leave a comment

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.