I need to edit a binary file and have to take consideration of a few constraints.
Context
- I have a very large binary file (for example, 1GB)
- It contains some strings, numbers and more.
- I know the structure of the file and know how to parse it.
I'd like to replace a known string (or byte array or whatever) in this file by another string (or whatever).
Problem
The main difficulties I'm facing are the following:
- The file is very large (potentially several gigabytes), so I don't want to load it entirely in memory. Therefore, deserializing it entirely isn't an option. I also don't want to create a new file next to the old one because there may not be enough space on the hard drive to host 2 very large files.
- The size of the replacing data may be different than the original one (think about a string with a different length for example). So I may need move some bytes in the file when replacing the data (is that even possible? That sounds tricky to me).
- The data to replace can be anywhere in the file.
Therefore, my question:
How can I replace a string (for example) in a large binary file without rewriting the entire file and loading the entire file in memory?
Something I'm allowed to do if it can help:
If I can't "replace", I can still "remove" the old data (which is anywhere in the file) and add the new one at the end of the file (or anywhere else I know it won't break the file format). But I also don't find how to do that without rewriting the entire file in a new one or loading the entire file in memory.
Thanks :)