Get byte [] from the data set and compress

I am returning a custom class from a WCF operation. The netTcp binding is used. This custom class contains several data members. One of them is a data set. The data set can be huge depending on the specific actions. I plan to compress the data set to bytes, and then return the user class back.

Based on the reading, I came up with the following code to return compressed bytes from a dataset. But not sure if this is the best way (or the right way). 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;    
+3
source share
2 answers

There is an important step to save space:

ds.RemotingFormat = SerializationFormat.Binary;

xml, BinaryFormatter. gzip, . , ; :

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.

+4

:

        using(var memory = new MemoryStream())
        {
            using(var gzip = new GZipStream(memory, CompressionMode.Compress, true))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(gzip, ds);
            }

            return memory.ToArray();
        }
+2

Source: https://habr.com/ru/post/1785294/


All Articles