【问题标题】:GZip decompression stops at arbitrary pointGZip 解压在任意点停止
【发布时间】:2010-10-20 00:02:28
【问题描述】:

我正在使用 .Net GZipStream 类来压缩和解压缩文件。在我进行解压之后,数据看起来还不错,但是在某个看似任意的点之后变成了零。例如,解压文件后,它的大小是正确的 19KB,但字节 10,588 及以上都是零。

我不确定我做错了什么。

这就是我进行压缩的方式:

Byte[] bytes = GetFileBytes(file);

using (FileStream fileStream = new FileStream("Zipped.gz", FileMode.Create))
{
    using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Compress))
    {
        zipStream.Write(bytes, 0, bytes.Length);
    }
}

这就是我的解压方式(Bytes 是压缩字节的数组,OriginalSize 是压缩前文件的大小):

using (MemoryStream memoryStream = new MemoryStream(Bytes))
{
    using (GZipStream zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
    {
        // Note: Since the compressed version can be larger, I use the larger of the original and the compressed size for the decompressed array's size.
        Byte[] decompressedBytes = new Byte[OriginalSize > Bytes.Length ? OriginalSize : Bytes.Length];

        Int32 numRead = zipStream.Read(decompressedBytes, 0, Bytes.Length);

        using (FileStream fileStream = new FileStream("Decompressed.txt", Name), FileMode.Create))
        {
            fileStream.Write(decompressedBytes, 0, Convert.ToInt32(OriginalSize));
        }
    }
}

【问题讨论】:

  • 你知道它是在解压还是压缩 - 你可以通过使用 gzip 手动解压/压缩文件来检查。

标签: .net gzip gzipstream


【解决方案1】:

我在这里看到一个潜在的错误:您做出假设! :) 在流之间复制时必须使用循环,请参阅this question

【讨论】:

  • 嗯,不 :) 再看看那个链接 :) (你甚至可以使用那里的方法,这是最简单的)
猜你喜欢
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 2014-10-21
  • 2012-06-26
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多