GZipStream COM pression问题(丢失字节) [英] GZipStream compression problem ( Lost Byte )
问题描述
我有一些奇怪的问题,用gzip串行。
试图序列化对象,在它的数据。
随着code得出的结果(在POINT1在调试):ms.Length = 100028和uncom pressedStream.Length = 100027
POINT1后有异常流的末尾遇到解析完成之前。,我认为这是失去的字节的结果。
我使用.NET 4.0。
//生成数据
INT长度= 100000;
byte []的数据=新的字节[长度];
的for(int i = 0; I<长度;我++)
{
数据[I] = System.Convert.ToByte(ⅰ%100 + I%50);
}
//序列化到内存流
IFormatter格式化=新的BinaryFormatter();
VAR毫秒=新的MemoryStream();
formatter.Serialize(毫秒,数据);
ms.Seek(0,SeekOrigin.Begin);
// GZip压缩ZIP
MemoryStream的COM pressedStream =新的MemoryStream();
变种的COM preSS =新GZipStream(COM pressedStream,COM pressionMode.Com preSS);
ms.CopyTo(比较preSS);
COM pressedStream.Seek(0,SeekOrigin.Begin);
// GZip压缩解压
MemoryStream的uncom pressedStream =新的MemoryStream();
VAR DECOM preSS =新GZipStream(COM pressedStream,COM pressionMode.Decom preSS);
DECOM press.CopyTo(uncom pressedStream);
uncom pressedStream.Seek(0,SeekOrigin.Begin);
//从内存流反序列化
// POINT1
VAR 600 = formatter.Deserialize(uncom pressedStream);
变种O =(byte []的)面向对象;
//检查
Assert.AreEqual(data.Length,o.Length);
的for(int i = 0; I< data.Length;我++)
Assert.AreEqual(数据[I],邻[I]);
的COM pression流不冲水(而无法正常冲水),直到它们被关闭。您将需要关闭 GZipStream
。告诉它不要关闭底层流(的构造函数的一个参数)将使它更容易些。
//生成数据
INT长度= 100000;
byte []的数据=新的字节[长度];
的for(int i = 0; I<长度;我++)
{
数据[I] = System.Convert.ToByte(ⅰ%100 + I%50);
}
字节[] 0;
//序列化到内存流
IFormatter格式化=新的BinaryFormatter();
使用(VAR毫秒=新的MemoryStream())
{
formatter.Serialize(毫秒,数据);
ms.Seek(0,SeekOrigin.Begin);
// GZip压缩ZIP
使用(MemoryStream的COM pressedStream =新的MemoryStream())
{
使用(VAR的COM preSS =新GZipStream(COM pressedStream,COM pressionMode.Com preSS,真))
{
ms.CopyTo(比较preSS);
}
COM pressedStream.Seek(0,SeekOrigin.Begin);
// GZip压缩解压
使用(MemoryStream的uncom pressedStream =新的MemoryStream())
{
使用(VAR DECOM preSS =新GZipStream(COM pressedStream,COM pressionMode.Decom preSS,真))
{
DECOM press.CopyTo(uncom pressedStream);
}
uncom pressedStream.Seek(0,SeekOrigin.Begin);
VAR 600 = formatter.Deserialize(uncom pressedStream);
O =(byte []的)面向对象;
}
}
//从内存流反序列化
// POINT1
}
//检查
Debug.Assert的(data.Length == o.Length);
的for(int i = 0; I< data.Length;我++)
Debug.Assert的(数据[I] ==问题o [I]);
I've got some strange problem with GZip Serializer.
Trying serializing object with data in it.
Following code give results(at POINT1 in debug): ms.Length = 100028 and 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.
I am using .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
. Telling it not to close the underlying stream (one of the constructor arguments) will make this easier.
//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]);
这篇关于GZipStream COM pression问题(丢失字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!