简体   繁体   English

使用 Protobuf-net 恢复损坏的文件序列化

[英]Recovering corrupted file serialize with Protobuf-net

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.Serializer.Deserialize的调用没有失败,但结果对象的每个属性都有默认值。

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.没有 _tmp 文件,只有主文件。 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() .我已经尝试过 Marc 对 ProtoReader 的建议,并且有问题的文件导致在reader.ReadFieldHeader()处引发异常。 The ProtoException reads: "Invalid field in source data:0" ProtoException 读取:“源数据中的无效字段:0”

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 (ie "field 3 is a string, field 7 is a sub-object, etc"), then you could make the output more detailed.如果您知道某些字段的布局(即“字段 3 是字符串,字段 7 是子对象等”),那么您可以使输出更详细。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM