如何在 C# 中解压缩嵌套的 GZip (TGZ) 文件

发布于 2024-08-11 19:42:17 字数 248 浏览 8 评论 0原文

我收到一份 TGZ 文件,其中包含一个纯文本文件以及可能的一个或多个嵌套 TGZ 文件。我已经弄清楚如何解压缩主 TGZ 文件并读取其中包含的纯文本文件,但我无法弄清楚如何识别和解压缩嵌套的 TGZ 文件。以前有人遇到过这个问题吗?

另外,我无法控制收到的文件,因此无法更改包含嵌套 TGZ 文件的 TGZ 文件的格式。另一个需要注意的是(尽管我认为这并不重要),这些文件是在 Unix 或 Linux 环境中被压缩和焦化的。

预先感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-08-18 19:42:17

尝试 SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx )免费图书馆。

它允许您使用 TGZ 并具有在尝试膨胀文件之前测试文件的方法;因此,您可以依赖正确的文件扩展名,或者单独测试它们,看看是否可以将它们作为压缩文件读取 - 然后在主文件解压缩后将它们膨胀。

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.

无妨# 2024-08-18 19:42:17

要从 .NET 读取和写入 .tar 和 .tgz (或 .tar.gz )文件,您可以使用以下单文件 tar 类:

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

用法非常简单。要创建存档:

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

创建压缩 (gzip) tar 存档:

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

读取 tar 存档:

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

提取 tar 存档中的所有条目:

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

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
韵柒 2024-08-18 19:42:17

查看 CodePlex 上的 DotNetZip

“如果你想要的是更好的
DeflateStream 或 GZipStream 类
替换内置的那个
.NET BCL,也在这里。
DotNetZip 的 DeflateStream 和
GZipStream 可用于
独立程序集,基于 .NET
兹利布港。这些流支持
压缩级别并提供大量
比内置的性能更好
类。还有一个 ZlibStream 可以
完成该集(RFC 1950、1951、
1952)。”

看来您可以遍历压缩文件并将各个文件从存档中拉出。然后您可以测试解压缩的文件并查看它们本身是否是 GZip 文件。

这是来自他们的 < a href="http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples" rel="nofollow noreferrer">示例页面

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

Keith

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文