GZip 往返截断 1 个或多个字节

发布于 2024-07-19 07:05:56 字数 1719 浏览 1 评论 0原文

我不明白。 我多年来一直使用类似/相同的方法,但从未经历过这种情况。

由于某种原因,直到今天我才发现,GZip 往返会导致 1 个或多个字节被截断或数据出现乱码。

我编写了一个简单的测试来验证其他因素不会影响它。

这总是会因“长度不匹配”而失败。

有人能证实我没有疯吗? :)

谢谢

leppie

TEST

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

class Program
{
  const int BUFFER_SIZE = 8192;

  static void Main(string[] args)
  {
    var filename = args[0];
    var filedata = File.ReadAllBytes(filename);
    var cmp = Compress(filedata);
    var dec = Decompress(cmp);

    Assert(filedata, dec);

    Console.ReadLine();
  }

  static void Assert(byte[] orig, byte[] data)
  {
    if (orig.Length != data.Length)
    {
      Debug.Fail("length mismatch");
    }
    for (int i = 0; i < orig.Length; i++)
    {
      Debug.Assert(orig[i] == data[i], "data mismatch");
    }
  }

  static byte[] Compress(byte[] data)
  {
    var input = new MemoryStream(data);
    var output = new MemoryStream();

    var s = new GZipStream(output, CompressionMode.Compress);
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
      s.Write(buffer, 0, read);
    }

    return output.ToArray();
  }

  static byte[] Decompress(byte[] data)
  {
    var input = new MemoryStream(data);
    var s = new GZipStream(input, CompressionMode.Decompress);

    var output = new MemoryStream();
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = s.Read(buffer, 0, buffer.Length)) > 0)
    {
      output.Write(buffer, 0, read);
    }

    return output.ToArray();
  }
}

我也尝试过正确关闭流,使用不同的缓冲区大小,但结果都是相同的。

I dont get it. I have used a similar/same approach for many years now, and never experienced this.

For some reason, that I did not pick up until today, a GZip round trip results in 1 or more bytes being truncated or data being garbled.

I wrote a simple test to verify that something else is not affecting it.

This always fails with a 'length mismatch'.

Can someone conform to me that I am not crazy? :)

Thanks

leppie

TEST

using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;

class Program
{
  const int BUFFER_SIZE = 8192;

  static void Main(string[] args)
  {
    var filename = args[0];
    var filedata = File.ReadAllBytes(filename);
    var cmp = Compress(filedata);
    var dec = Decompress(cmp);

    Assert(filedata, dec);

    Console.ReadLine();
  }

  static void Assert(byte[] orig, byte[] data)
  {
    if (orig.Length != data.Length)
    {
      Debug.Fail("length mismatch");
    }
    for (int i = 0; i < orig.Length; i++)
    {
      Debug.Assert(orig[i] == data[i], "data mismatch");
    }
  }

  static byte[] Compress(byte[] data)
  {
    var input = new MemoryStream(data);
    var output = new MemoryStream();

    var s = new GZipStream(output, CompressionMode.Compress);
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
      s.Write(buffer, 0, read);
    }

    return output.ToArray();
  }

  static byte[] Decompress(byte[] data)
  {
    var input = new MemoryStream(data);
    var s = new GZipStream(input, CompressionMode.Decompress);

    var output = new MemoryStream();
    byte[] buffer = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = s.Read(buffer, 0, buffer.Length)) > 0)
    {
      output.Write(buffer, 0, read);
    }

    return output.ToArray();
  }
}

I have tried it with closing the streams properly too, with different buffer sizes, all the same result.

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

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

发布评论

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

评论(1

满栀 2024-07-26 07:05:56

好的,找到问题了。

您需要在检索字节之前关闭压缩流。

例如:

s.Close();
return output.ToArray();

OK, found the problem.

You need to close the compression stream before retrieving the bytes.

Eg:

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