English 中文(简体)
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.

最佳回答

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.

问题回答

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

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签