Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Decompress nested GZip (TGZ) files in C#

I am receiving a TGZ file that will contain one plain text file along with possibly one or more nested TGZ files. I have figured out how to decompress the main TGZ file and read the plain text file contained in it, but I have not been able to figure out how to recognize and decompress the nested TGZ files. Has anyone come across this problem before?

Also, I do not have control over the file I am receiving, so I cannot change the format of a TGZ file containing nested TGZ files. One other caveat (even though I don't think it matters) is that these files are being compressed and tarred in a Unix or Linux environment.

Thanks in advance for any help.

like image 616
Flahrty Avatar asked Dec 30 '22 11:12

Flahrty


2 Answers

Try the SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx) free library.

It lets you work with TGZ and has methods to test files before trying to inflate them; so you can either rely on the file extensions being correct, or test them individually to see if you can read them as compressed files - then inflate them once the main file has been decompressed.

like image 89
pierre Avatar answered Jan 11 '23 12:01

pierre


To read and write .tar and .tgz (or .tar.gz ) files from .NET, you can use this one-file tar class:

http://cheesoexamples.codeplex.com/SourceControl/changeset/view/97756#1868643

Very simple usage. To create an archive:

string[] filenames = { ... };
Ionic.Tar.CreateArchive("archive.tar", filenames);

Create a compressed (gzip'd) tar archive:

string[] filenames = { ... };
Ionic.Tar.CreateArchive("archive.tgz", filenames, TarOptions.Compress);

Read a tar archive:

var entries = Ionic.Tar.List("archive.tar");  // also handles .tgz files

Extract all entries in a tar archive:

var entries = Ionic.Tar.Extract("archive.tar");  // also handles .tgz files
like image 28
Cheeso Avatar answered Jan 11 '23 12:01

Cheeso