【问题标题】:Exception using GZip使用 GZip 的异常
【发布时间】:2012-06-30 18:40:27
【问题描述】:

大家好,我遇到了这个异常:

The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.

我所做的只是将文件解压缩为 GZip,然后将其传递到流中,以将其传递到文件中。

using (FileStream fInStream = new FileStream(@"C:\Users\UNI\Desktop\FrostbyteDBUpdateProgram\FrostbyteDBUpdateProgram\bin\Debug\" + fileName, FileMode.Open, FileAccess.Read))
        {
            using (GZipStream gZip = new GZipStream(fInStream, CompressionMode.Decompress))
            {
                using (FileStream fOutStream = new FileStream(@"C:\Users\UNI\Desktop\FrostbyteDBUpdateProgram\FrostbyteDBUpdateProgram\bin\Debug\test1.docx", FileMode.Create, FileAccess.Write))
                {
                    byte[] tempBytes = new byte[4096];
                    int i;
                    while ((i = gZip.Read(tempBytes, 0, tempBytes.Length)) != 0)
                    {
                        fOutStream.Write(tempBytes, 0, i);
                    }
                }
                gZip.Close();
            }
            fInStream.Close();
        }

【问题讨论】:

  • 问:您确认您打开的文件流的文件名是正确的吗?问:您是否打开该文件以确认它是一个有效的 .zip 文件?问:为什么是“4096”?
  • 旁注:无需致电Close(因为您正确使用using)并考虑使用Stream.CopyTo而不是手动复制流,因为您已经可以拥有4.0。
  • 你将如何实现stream.copyTo?

标签: c#


【解决方案1】:

GZipStream 类不适合读取 .gz 或 .zip 格式的压缩档案。它知道如何解压/压缩数据,它对存档文件结构一无所知。其中可以包含多个文件,请注意该类如何无法选择要解压缩的存档中的特定文件。

Zip 存档支持将在 .NET 4.5 中添加。在此之前,您可以使用流行的 SharpZipLib 或 DotNetZip 库。谷歌他们的名字以找到下载。

如果您要解压缩的文件实际上是由 GZipStream 生成的,那么执行该操作的代码中存在错误,我们看不到它。

【讨论】:

  • 好的,感谢您的提醒,我将对此进行更多研究。我只是想确认一下那些可以下载和使用合法库的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 2020-12-07
  • 2017-06-03
  • 1970-01-01
相关资源
最近更新 更多