如何将protobuf序列化内容直接写入SharpZipLib流

时间:2013-10-25 13:16:17

标签: c# protobuf-net sharpziplib

是否可以将protobuf序列化内容直接写入SharpZipLib流?当我尝试这样做时,看起来提供的流没有填充来自protobuf的数据。稍后我需要从提供的Zip流中取回反序列化的实体。 我的代码如下所示:

private byte[] ZipContent(T2 content)
    {
        const short COMPRESSION_LEVEL = 4; // 0-9
        const string ENTRY_NAME = "DefaultEntryName";


        byte[] result = null;

        if (content == null)
            return result;

        IStreamSerializerProto<T2> serializer = this.GetSerializer(content.GetType());
        using (MemoryStream outputStream = new MemoryStream())
        {
            using (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream))
            {
                zipOutputStream.SetLevel(COMPRESSION_LEVEL);
                ZipEntry entry = new ZipEntry(ENTRY_NAME);
                entry.DateTime = DateTime.Now;
                zipOutputStream.PutNextEntry(entry);

                serializer.Serialize(zipOutputStream, content);
            }

            result = outputStream.ToArray();
        }

        return result;
    }

private class ProtobufStreamSerializer<T3> : IStreamSerializerProto<T3>
        {
            public ProtobufStreamSerializer()
            {
                ProtoBuf.Serializer.PrepareSerializer<T3>();
            }

    public void Serialize(Stream outputStream, T3 content)
    {
        Serializer.Serialize(outputStream, content);
    }

            public T3 Deserialize(Stream inputStream)
            {
                T3 deserializedObj;
                using (inputStream)
                {
                    deserializedObj = ProtoBuf.Serializer.Deserialize<T3>(inputStream);
                }
                return deserializedObj;
            }
        }

我正在尝试序列化的类的示例:

[Serializable]
    [ProtoContract]
    public class Model
    {
        [XmlElement("ModelCode")]
        [ProtoMember(1)]
        public int ModelCode { get; set; }

                ...

    }

2 个答案:

答案 0 :(得分:4)

这是问题,我相信(在问题的原始代码中):

public void Serialize(Stream outputStream, T3 content)
{
    using (var stream = new MemoryStream())
    {
        Serializer.Serialize(stream, content);
    }
}

您完全忽略outputStream,而是将数据仅写入新的MemoryStream,然后将其忽略。

我怀疑你只是想要:

public void Serialize(Stream outputStream, T3 content)
{
    Serializer.Serialize(outputStream, content);
}

我还建议您从using方法中删除Deserialize语句:我希望调用者负责在他们'处理输入流时重新完成它。您的方法可以简化为:

public T3 Deserialize(Stream inputStream)
{
    return ProtoBuf.Serializer.Deserialize<T3>(inputStream);
}

答案 1 :(得分:2)

代码(由Jon指出的编辑)看起来很好。这是工作:

static void Main()
{
    var obj = new Bar{ X = 123, Y = "abc" };
    var wrapper = new Foo<Bar>();
    var blob = wrapper.ZipContent(obj);
    var clone = wrapper.UnzipContent(blob);
}

其中Bar是:

[ProtoContract]
class Bar
{
    [ProtoMember(1)]
    public int X { get; set; }
    [ProtoMember(2)]
    public string Y { get; set; }
}

Foo<T>是你的班级(我不知道名字),我已经添加了:

public T2 UnzipContent(byte[] data)
{
    using(var ms = new MemoryStream(data))
    using(var zip = new ZipInputStream(ms))
    {
        var entry = zip.GetNextEntry();
        var serializer = this.GetSerializer(typeof(T2));
        return serializer.Deserialize(zip);
    }
}

另外,请注意压缩是双刃的。在上面给出的示例中,基础大小(即,如果我们只写入MemoryStream)是7个字节。 ZipInputStream将这7个字节“压缩”到179个字节。压缩最适合较大的对象,通常在有大量文本内容时。