从数据集中获取byte []并进行压缩 [英] Get byte[] from dataset and compress
问题描述
我正在从WCF操作返回一个自定义类。使用的绑定是netTcp。该自定义类包含几个数据成员。其中之一是数据集。数据集可能非常庞大,具体取决于特定操作。我打算将数据集压缩为字节,然后返回自定义类。
I am returning a custom class from a WCF operation. The binding used is netTcp. This custom class contains several data members. One of them is a dataset. The dataset can be huge depending on particular actions. I am planning to compress the dataset to bytes and then return the custom class back.
基于阅读,我想出了以下代码从数据集中返回压缩字节。但是不确定这是否是最好的方法(或正确的方法)。您的想法。
Based on the reading I have come up with following code to return compressed bytes from a dataset. But not sure if this is the best way (or the correct way) to go. Your thoughts pls. ??
byte[] bytes = null;
byte[] compressedBytes = null;
using(var memory = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(memory, ds);
bytes = memory.ToArray();
}
using(var memory = new MemoryStream())
{
using(var gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
gzip.Write(bytes, 0, bytes.Length);
compressedBytes = memory.ToArray();
}
}
return compressedBytes;
推荐答案
节省空间是重要的一步:
There is an important step to save space:
ds.RemotingFormat = SerializationFormat.Binary;
否则,它会内部使用xml,甚至通过 BinaryFormatter
。使用此功能后,您还可以添加gzip,但是获得的收益并不显着。碰巧的是,我有一些统计数据可以对此此处;将数据复制到以下位置:
Otherwise it internally uses xml, even via BinaryFormatter
. With this in place, you can also include gzip, but the gain isn't quite as significant. As it happens I have some stats to compare this here; to copy that data in:
DataTable (xml) (vanilla) 2269ms/6039ms
64,150,771 bytes
DataTable (xml) (gzip) 4881ms/6714ms
7,136,821 bytes
DataTable (xml) (deflate) 4475ms/6351ms
7,136,803 bytes
BinaryFormatter (rf:binary) (vanilla) 2006ms/3366ms
11,272,592 bytes
BinaryFormatter (rf:binary) (gzip) 3332ms/4267ms
8,265,057 bytes
BinaryFormatter (rf:binary) (deflate) 3216ms/4130ms
但是: DataSet
是不是一种非常WCF的处理方式。我将添加标准的OO类,然后交换序列化程序类似于protobuf-net ,它是 比 DataContractSerializer
或 NetDataContractSerializer
小得多。
But: DataSet
is not a very WCF way of doing things. I would add standard OO classes, and swap the serializer for something like protobuf-net, which is significantly smaller than either DataContractSerializer
or NetDataContractSerializer
.
这篇关于从数据集中获取byte []并进行压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!