GZip 往返截断 1 个或多个字节
我不明白。 我多年来一直使用类似/相同的方法,但从未经历过这种情况。
由于某种原因,直到今天我才发现,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,找到问题了。
您需要在检索字节之前关闭压缩流。
例如:
OK, found the problem.
You need to close the compression stream before retrieving the bytes.
Eg: