我对 GZip Serializer 有一些奇怪的问题。
尝试使用其中的数据序列化对象。
以下代码给出结果(在调试中的 POINT1 处): ms.Length = 100028 和 uncompressedStream.Length=100027
在 POINT1 之后出现异常“在解析完成之前遇到流结束。”,我认为这是丢失字节的结果。
我正在使用.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]);