GZipStream compression problem ( Lost Byte )(GZipStream 压缩问题(丢失字节))
问题描述
我在使用 GZip Serializer 时遇到了一些奇怪的问题.
I've got some strange problem with GZip Serializer.
尝试序列化包含数据的对象.
Trying serializing object with data in it.
以下代码给出结果(在调试中的 POINT1):ms.Length = 100028 和 uncompressedStream.Length=100027
Following code give results(at POINT1 in debug): ms.Length = 100028 and uncompressedStream.Length=100027
在 POINT1 之后出现异常在解析完成之前遇到流结束.",我认为这是丢失字节的结果.
After POINT1 there is exception "End of Stream encountered before parsing was completed.", which i think is result of this lost byte.
我使用的是 .net 4.0.
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]);
推荐答案
压缩流在关闭之前不会刷新(并且无法正确刷新).您需要关闭 GZipStream
.告诉它不要关闭底层流(构造函数参数之一)将使这更容易.
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 压缩问题(丢失字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GZipStream 压缩问题(丢失字节)
基础教程推荐
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 创建属性设置器委托 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01