Packing and unpacking files using Tar archives in .NET
August 26, 2016 1 Comment
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:
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.
Useful article. Read all your series. Waiting for new materials