[Tips] How to extract TGZ, TAR.GZ by SharpCompress C#

0
54

What different between TGZ format and TAR.GZ format?

TGZ with TAR.GZ is same

What is SharpCompress?

SharpCompress is a compression library for .NET/Mono/Silverlight/WP7, WP8, WP8.1/WindowsStore that can unrar decompress RAR, 7zip, zip/unzip, tar/untar bzip2/unbzip2 and gzip with forward-only reading and file random access APIs.

Write support for zip/tar/bzip2/gzip is implemented.

The following are not yet supported,
– RAR 5
– Write 7Zip
– Zip64
– Multi-volume Zip

How to work?

Install SharpCompress

To install SharpCompress, you can manually fork the repository from Github. Or you can use NuGet

How to extract TGZ, TAR.GZ by SharpCompress C#

Include Library

How to extract TGZ, TAR.GZ by SharpCompress C#

Source Code:

using SharpCompress.Reader;
using SharpCompress.Common;

You can download full source code from here

How to decompress tgz, tar.gz by using SharpCompress library

static void Main(string[] args)
{
unTAR(@"E:testserver.tgz");
Console.WriteLine("Decompress Succeed!");
Console.ReadLine();
}

private static string directoryPath = @"E:testextract";

static void unTAR(string tarFilePath)
{
// UTF7: support chinese font -> UTF.7
SharpCompress.Common.ArchiveEncoding.Default = Encoding.UTF7;

using (Stream stream = File.OpenRead(tarFilePath))
{
var reader = ReaderFactory.Open(stream);

while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
reader.WriteEntryToDirectory(directoryPath,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);

}
}
}

Before and after decompress from server.tgz file

How to extract TGZ, TAR.GZ by SharpCompress C#
E:testserver.tgz

You can download full source code from here

If you have any questions or feedback, leave your comment, we can discuss about it!
Zidane