How to decompose GZip (TGZ) file attachments in C #

I get a TGZ file that will contain one simple text file along with possibly one or more nested TGZ files. I figured out how to unzip the main TGZ file and read the plain text file contained in it, but I could not figure out how to recognize and unzip the attached TGZ files. Has anyone encountered this problem before?

Also, I have no control over the file that I get, so I cannot change the format of the TGZ file containing the attached TGZ files. Another caveat (although I don’t think it matters) is that these files are compressed and downloaded to Unix or Linux.

Thanks in advance for your help.

+4
source share
3 answers

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

It allows you to work with TGZ and has methods for checking files before trying to inflate them; so you can either rely on file extensions that are correct, or test them individually to see if you can read them as compressed files β€” then inflate them as soon as the main file is unpacked.

+5
source

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

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

Very simple use. To create an archive:

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

Create a compressed (gzip'd) tarball:

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

Read the tar archive:

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

Extract all entries in the tar archive:

 var entries = Ionic.Tar.Extract("archive.tar"); // also handles .tgz files 
+4
source

Check out DotNetZip on CodePlex.

β€œIf all you want is better DeflateStream or GZipStream to replace the one that is built into .NET BCL, here it is. DotNetZip DeflateStream and GZipStream are available in a standalone .NET based assembly. Zlib port. Support for these compression level streams and better performance than the built-in classes. There is also ZlibStream to complete the suite (RFC 1950, 1951, 1952). "

It looks like you can iterate over a compressed file and pull individual files from the archive. You can then test the uncompressed files and see if they are the GZip files themselves.

Here is a snippet from Sample Pages

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

Whale

+1
source

All Articles