问题
Here is the important part of some existing code that I'm trying to adapt for my own uses.
The notable part is that self.archive
leads to a massive file and raw_file
is binary data extracted (painfully) from this giant file.
with open(self.archive, "rb") as f:
f.seek(offset)
raw_file = start + f.read(dlen - len(start))
...
f.write(raw_file)
The existing code extracts the contents of an archive-like file to disk, but I need to only read these stored files from this archive (if that makes any sense).
I need to use Pygame to read a few hundred mbs of data from this file, mostly as images, using methods such as pygame.image.load()
. Rather than having this code write all of the file's contents to disk and then re-reading it again in "not-binary", I'd like to directly do something like pygame.image.load(toVirtualFileObject(raw_file))
. Does anyone know of something like this?
回答1:
This is what StringIO (in Python 2) and io.BytesIO in (in Python 3) are for.
来源:https://stackoverflow.com/questions/16007412/treat-binary-data-as-a-file-object