【问题标题】:How does GZipStream determine the size of compressed dataGZipStream如何确定压缩数据的大小
【发布时间】:2016-03-20 13:23:29
【问题描述】:

我有两个文件(我使用 7zip):n1.txt.gzn2.txt.gz。然后我通过命令提示符将它们合并到文件 n12.txt.gz 中:

type n1.txt.gz > n12.txt.gz
type n2.txt.gz >> n12.txt.gz

如果我用 7zip 解压文件 n12.txt.gz,我会得到合并后的原始文件 (n1.txt + n2.txt)。 但是如果我使用这段代码

public static void Decompress2(String fileSource, String fileDestination, int buffsize)
{
 using (var fsInput = new FileStream(fileSource, FileMode.Open, FileAccess.Read))
 {
  using (var fsOutput = new FileStream(fileDestination, FileMode.Create, FileAccess.Write))
  {
   using (var gzipStream = new GZipStream(fsInput, CompressionMode.Decompress))
   {
    var buffer = new Byte[buffsize];
    int h;
    while ((h = gzipStream.Read(buffer, 0, buffer.Length)) > 0)
    {
     fsOutput.Write(buffer, 0, h);
    }
   }
  }
 }
}

我将得到刚刚解压的n12.txt.gz的第一部分,即解压的n1.txt

为什么 GZipStream 在合并文件的第一部分后停止?以及7zip是如何解压整个文件的?

【问题讨论】:

  • 我实际上怀疑合并 gzip 文件是否像你说的那样工作......
  • @ThorstenDittmar 它有效。试试吧。

标签: c# gzipstream


【解决方案1】:

GZipStream 没有实现从一个流中解压缩多个文件的方法。

尝试使用处理 ZIP 存档的库,例如 DotNetZip

如果您绝对想使用 GZipStream,您可以在输入流中搜索 the gzip header,然后仅提供 GZipStream 流中属于每个文件的部分。

【讨论】:

  • 好的。谢谢。但是,我读过 gzip 格式。如果我将每个部分的大小写入 gzip 标头,我将读取这些部分,GZipStream 将正确解压缩它们。但是 7zip 解压我的 n12.txt.gz 文件,该文件没有部分的大小。你知道7zip是怎么做到的吗?
  • 不,我不知道 7zip 是如何做到的,但我认为他们可能只是假设您的存档已损坏并尝试充分利用它。看看how ZIP archives are structured,你会发现你的方法错过了识别每个文件开始位置所需的文件目录。
猜你喜欢
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多