是否有可能将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; } ... }Is there possible to writte protobuf serialized content directly to SharpZipLib stream? When I try to do this, looks like the provided stream is not filled with the data from protobuf. Later I would need to get back the deserialized entity from provided Zip stream. My code looks like this:
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; } }Sample of a class which I'm trying to serialize:
[Serializable] [ProtoContract] public class Model { [XmlElement("ModelCode")] [ProtoMember(1)] public int ModelCode { get; set; } ... }最满意答案
这是问题,我相信(在问题中的原始代码中):
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); }我还建议从Deserialize方法中删除using语句:我希望调用者在完成输入流时负责处理输入流。 您的方法可以简化为:
public T3 Deserialize(Stream inputStream) { return ProtoBuf.Serializer.Deserialize<T3>(inputStream); }This is the problem, I believe (in the original code in the question):
public void Serialize(Stream outputStream, T3 content) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, content); } }You're completely ignoring outputStream, instead writing the data just to a new MemoryStream which is then ignored.
I suspect you just want:
public void Serialize(Stream outputStream, T3 content) { Serializer.Serialize(outputStream, content); }I'd also suggest removing the using statement from your Deserialize method: I'd expect the caller to be responsible for disposing of the input stream when they're finished with it. Your method can be simplified to:
public T3 Deserialize(Stream inputStream) { return ProtoBuf.Serializer.Deserialize<T3>(inputStream); }更多推荐
发布评论