How to construct a TarFile object in memory from byte buffer in Python 3? How to construct a TarFile object in memory from byte buffer in Python 3? python python

How to construct a TarFile object in memory from byte buffer in Python 3?


BytesIO() from IO module does exactly what you need.

import tarfile, iobyte_array = client.read_bytes()file_like_object = io.BytesIO(byte_array)tar = tarfile.open(fileobj=file_like_object)# use "tar" as a regular TarFile objectfor member in tar.getmembers():    f = tar.extractfile(member)    print(f)


Sure, something like this:

import ioio_bytes = io.BytesIO(byte_array)tar = tarfile.open(fileobj=io_bytes, mode='r')

(Adjust mode to fit the format of your tar file, e.g. possibly `mode='r:gz', etc.)