/tar-cs

dotnet standard library to work with .tar archives

Primary LanguageC#OtherNOASSERTION

tar-cs

C# Library to work with tar archives

News

Pre

If you ever had a need to send multiple files to a client using your C# code. If want your client to be able to extract those files with some kind of widely recognized application. If you did not want to make your code dependencies much much much bigger. If yes then you had the same requirement I do.

Result

I came out with this small library. This library compatible with mono and .NET framework 2.0 and higher. You can use it to create regular ".tar" files or to create "tar.gz" archives that are widely used across Linux/Unix world and recognized by shell extensions like "WinZip" or "WinRar".

Benefits

This is the library enables your to read and write tar archives from your .NET/mono code.

Here are the

Examples

First of all you should reference tar-cs.dll and create tar-cs.TarWriter object:


var tar = new TarWriter(OutStream);

OutStream is an object of any System.IO.Stream inherited class that supports write operation. That is a destination for you tar-ed data. It's your destination file in most cases.

As soon as TarWriter implements IDisposable, it's good idea to use keyword using while creating TarWriter, so the object will be closed right after you left the corresponding context:


using(var tar = new TarWriter(OutStream)) { // ... context ... }

Next thing we plan to do is to add some files to the archive. Let's go:

using(var tar = new TarWriter(OutStream)) { tar.Write("myFileToBeAdded.txt"); }

This will write your file contents to the tar archive.

You can also use overloaded Write methods

Write(string fileName)
Write(FileStream file)
Write(Stream data, long dataSizeInBytes, string name)
Write(Stream data, long dataSizeInBytes, string name, int userId, int groupId, int ode, DateTime lastModificationTime)

Reading archive

If you want to extract archive someone created for you, then you should use TarReader. Here is an example:

using (FileStream unarchFile = File.OpenRead(args[0])) { TarReader reader = new TarReader(unarchFile); reader.ReadToEnd("out_dir\data"); }

It's the easiest way. ReadToEnd just extracts all the files from your archive to a specified folder. If you want more control on extraction process, then you should use MoveNext and Read methods:

TarReader reader = new TarReader(unarchFile); while (reader.MoveNext(false)) { var path = reader.FileInfo.FileName; using (FileStream file = File.Create(path)) { Read(file); } }

When you don't need the real data

If you want to examine names of the files that are in your archive but not extract files here is the example:

TarReader reader = new TarReader(unarchFile); while (reader.MoveNext(true)) { Console.WriteLine("FileName is {0}, owner is {1}", reader.FileInfo.FileName, reader.FileInfo.UserName); }

MoveNext(true) means that you don't need the data and want just skip it.

More complex example

If you want to create tar.gz. The file where data is compressed. No problems at all:

using(var outFile = File.Create("myOutFile.tar.gz")) { using(var outStream = new GZipStream(outFile ,CompressionMode.Compress)) { using(var writer = new TarWriter(outStream)) { writer.Write("myFileToAdd.txt"); } } }

What is tar?

License?

BSD License. See ./COPYING