Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

c# - GZipStream compression problem ( Lost Byte )

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]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...