9

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

1 に答える 1

15

圧縮ストリームは、閉じられるまでフラッシュされません (適切にフラッシュできません)。を閉じる必要がありますGZipStream。基になるストリーム (コンストラクター引数の 1 つ) を閉じないように指示すると、これが簡単になります。

        //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]);
于 2011-06-13T18:19:13.343 に答える