4

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.

Flahrty
  • 168
  • 1
  • 2
  • 7

3 Answers3

5

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.

pierre
  • 1,235
  • 1
  • 13
  • 30
  • 1
    Both this answer and Keith's answer handled the GZip portion, but the SharpZipLib could also handle the TAR file that was being compressed while I couldn't find a way for the DotNetZip library to handle the TAR file. – Flahrty Nov 23 '09 at 15:46
  • 1
    DotNetZip doesn't handle Tar archives. !! – Cheeso Apr 06 '10 at 20:46
  • 2
    This is great for anyone willing to accept the GNU license! For the rest of us, is there another option? – Dan Bailiff Sep 26 '12 at 18:07
  • @DanBailiff : for the record this might be new but they've added an exception to the GPL license which allows you to use it in commercial projects. Have a look at the License section of their page : http://icsharpcode.github.io/SharpZipLib/ – SolarBear Aug 07 '15 at 17:47
  • It appears this is now under the MIT license. – bwing Jan 08 '19 at 23:01
4

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
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • The Tar.cs link is dead and I cannot find another. Could you provide a new one? – Matteus Hemström Sep 20 '12 at 09:23
  • 2
    Yes, here is an active link: http://cheesoexamples.codeplex.com/SourceControl/changeset/view/97756#1868643 If you want to download a zip with the code, a readme, and a makefile, use this: http://cheesoexamples.codeplex.com/releases/view/76708 – Cheeso Sep 24 '12 at 23:53
  • Will be fine to specify also directory to which extract all the content of tar archive. – Sergey Kulgan Sep 06 '17 at 12:16
1

Take a look at DotNetZip on CodePlex.

"If all you want is a better DeflateStream or GZipStream class to replace the one that is built-into the .NET BCL, that is here, too. DotNetZip's DeflateStream and GZipStream are available in a standalone assembly, based on a .NET port of Zlib. These streams support compression levels and deliver much better performance that the built-in classes. There is also a ZlibStream to complete the set (RFC 1950, 1951, 1952)."

It appears that you can iterate through the compressed file and pull the individual files out of the archive. You can then test the files you uncompressed and see if any of them are themselves GZip files.

Here is a snippit from their Examples Page

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  foreach (ZipEntry e in zip)
  {
    e.Extract(OutputStream);
  }
}

Keith

Keith Sirmons
  • 8,271
  • 15
  • 52
  • 75
  • 1
    As of this writing (August 2015) DotNetZip seems to be an abandoned project with some major, unfixed bugs. From the looks of it I can't recommend using it. – SolarBear Aug 07 '15 at 17:49