Recovering corrupted file serialize with Protobuf-net

帅比萌擦擦* 提交于 2019-12-11 03:32:49

问题


The machine power was cut while, I assume, my application was updating a file. When turned back on and my application started it attempted to deserialize the file. The call to Serializer.Deserialize did not fail, but the resulting object has default values for every property.

My file updating/saving:

using (FileStream theStream = File.Open(fileName + "_tmp", FileMode.Create)) {
    ProtoBuf.Serializer.Serialize<MyObject>(theStream, inObjectToSerialize);
}
File.Copy(fileName + "_tmp", fileName, true);

There is no _tmp file, just the main file. The size of the file is non-zero which makes me believe the information is intact. Is there a way to recover this data?

Update:

I've tried the Marc's suggestion with ProtoReader and the file in questions causes an exception the be thrown at reader.ReadFieldHeader(). The ProtoException reads: "Invalid field in source data:0"


回答1:


There isn't much for me to go on there... I guess the short version would be: it depends what is left in the file.

One thing you could do would be to walk the file to see what is there:

using (var input = File.OpenRead(path))
using (var reader = new ProtoReader(input, RuntimeTypeModel.Default, null))
{
    while (reader.ReadFieldHeader() > 0)
    {
        Console.WriteLine("offset {0}, field {1}, type {2}",
            reader.Position, reader.FieldNumber, reader.WireType);
        reader.SkipField();
    }
}

That would at least allow you to see how much data is currently processing.

If you know the layout of some fields (i.e. "field 3 is a string, field 7 is a sub-object, etc"), then you could make the output more detailed.



来源:https://stackoverflow.com/questions/12680015/recovering-corrupted-file-serialize-with-protobuf-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!