恢复损坏的文件序列化与Protobuf网

编程入门 行业动态 更新时间:2024-10-17 21:19:54
本文介绍了恢复损坏的文件序列化与Protobuf网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 当我假设我的应用程序正在更新一个文件时,机器的电源被切断了。当重新启动时,我的应用程序开始尝试反序列化文件。对 Serializer.Deserialize 的调用没有失败,但是结果对象具有每个属性的默认值。

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

没有_tmp文件,只是主文件。该文件的大小是非零,这使我相信信息是完整的。有没有办法恢复这些数据?

更新:

我试过了Marc对ProtoReader的建议并且问题中的文件导致在 reader.ReadFieldHeader()引发异常。 ProtoException读取:源数据中的无效字段:0

解决方案

我没有太多的去那里。 ..我想这个简短的版本是:它取决于文件中的内容。

你可以做的一件事就是走该文件,看看有什么: $ $ $ p $使用(var input = File.OpenRead(path)) using( var reader = new ProtoReader(input,RuntimeTypeModel.Default,null)) { while(reader.ReadFieldHeader()> 0) { Console.WriteLine(offset {0},字段{1},类型{2}, reader.Position,reader.FieldNumber,reader.WireType); reader.SkipField(); $ b $ p $ b pre $ b $如果你知道一些字段的布局(即字段3是一个字符串,字段7是一个子对象,等等),那么你可以使输出更详细。

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"

解决方案

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.

更多推荐

恢复损坏的文件序列化与Protobuf网

本文发布于:2023-06-06 01:38:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/531172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   化与   文件   Protobuf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!