[英]GZipStream compression problem ( Lost Byte )
I've got some strange problem with GZip Serializer.我对 GZip Serializer 有一些奇怪的问题。
Trying serializing object with data in it.尝试用其中的数据序列化 object。
Following code give results( at POINT1 in debug ): ms.Length = 100028 and uncompressedStream.Length=100027以下代码给出结果(在调试中的 POINT1 处): ms.Length = 100028 和 uncompressedStream.Length=100027
After POINT1 there is exception "End of Stream encountered before parsing was completed.", which i think is result of this lost byte.在 POINT1 之后有异常“在解析完成之前遇到 Stream 结束。”,我认为这是这个丢失字节的结果。
I am using .net 4.0.我正在使用 .net 4.0。
//generating data
int length = 100000;
byte[] data = new byte[length];
for (int i = 0; i < length; i++)
{
data[i] = System.Convert.ToByte(i % 100 + i % 50);
}
//serialization into memory stream
IFormatter formatter = new BinaryFormatter();
var ms = new MemoryStream();
formatter.Serialize(ms, data);
ms.Seek(0, SeekOrigin.Begin);
//GZip zip
MemoryStream compressedStream = new MemoryStream();
var Compress = new GZipStream(compressedStream, CompressionMode.Compress);
ms.CopyTo(Compress);
compressedStream.Seek(0, SeekOrigin.Begin);
//GZip Unzip
MemoryStream uncompressedStream = new MemoryStream();
var Decompress = new GZipStream(compressedStream, CompressionMode.Decompress);
Decompress.CopyTo(uncompressedStream);
uncompressedStream.Seek(0, SeekOrigin.Begin);
//deserialization from memory stream
//POINT1
var oo = formatter.Deserialize(uncompressedStream);
var o = (byte[])oo;
//checking
Assert.AreEqual(data.Length, o.Length);
for (int i = 0; i < data.Length; i++)
Assert.AreEqual(data[i], o[i]);
Compression streams don't flush (and can't properly flush) until they are closed.压缩流在关闭之前不会刷新(也无法正确刷新)。 You will need to close the
GZipStream
.您将需要关闭
GZipStream
。 Telling it not to close the underlying stream (one of the constructor arguments) will make this easier.告诉它不要关闭底层 stream (构造函数参数之一)将使这更容易。
//generating data
int length = 100000;
byte[] data = new byte[length];
for (int i = 0; i < length; i++)
{
data[i] = System.Convert.ToByte(i % 100 + i % 50);
}
byte[] o;
//serialization into memory stream
IFormatter formatter = new BinaryFormatter();
using (var ms = new MemoryStream())
{
formatter.Serialize(ms, data);
ms.Seek(0, SeekOrigin.Begin);
//GZip zip
using(MemoryStream compressedStream = new MemoryStream())
{
using (var Compress = new GZipStream(compressedStream, CompressionMode.Compress, true))
{
ms.CopyTo(Compress);
}
compressedStream.Seek(0, SeekOrigin.Begin);
//GZip Unzip
using (MemoryStream uncompressedStream = new MemoryStream())
{
using (var Decompress = new GZipStream(compressedStream, CompressionMode.Decompress, true))
{
Decompress.CopyTo(uncompressedStream);
}
uncompressedStream.Seek(0, SeekOrigin.Begin);
var oo = formatter.Deserialize(uncompressedStream);
o = (byte[])oo;
}
}
//deserialization from memory stream
//POINT1
}
//checking
Debug.Assert(data.Length == o.Length);
for (int i = 0; i < data.Length; i++)
Debug.Assert(data[i] == o[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.