Search code examples
c#binaryfiles

Read middle of binary file using binary formatter


I am trying to use a binary formatter to serialize several objects into a file.

The file is very big, so if I need a specific object from the middle of the file I prefer not to de-serialize the entire file to get it.

Is there a way to do this? Use another serializer?
Also will I be able to change the object in the same spot?


Solution

  • TL;DR:

    • decide whether or not you need to conjoin/compress the files at all
    • don't implement something like this yourself
    • use .NET's ZipFile class

    When you re-open the file as a stream, you could use the stream's Seek method to go to the desired position (which you'd have to remember somewhere), and then read out the desired number of bytes (which you'd also have to either remember, or mark the end of the files with a marker bit). Modifying the files would also be immense fun.

    Don't do it! You'd be writing a bad tar equivalent, and plenty of other people have already done it, and better:

    Also note that .NET's binary serialisation is not guaranteed to be readable in later .NET versions. It's meant for transient things and not data archival. This may be important to you, and is another thing to consider.

    I don't know how what you're doing with the files; you might not want/need to do that - you might just be better off storing the things on disk as individual files. If compression/storage as a single file is in fact your thing, then I would recommend starting with .NET's ZipFile class, as you'd have no outside dependencies.