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

前端 未结 2 627
温柔的废话
温柔的废话 2020-12-30 19:54

Is it possible to create a TarFile object in memory using a buffer containing the tar data without having to write the TarFile to disk and open it up again? We get the bytes

相关标签:
2条回答
  • 2020-12-30 20:21

    Sure, something like this:

    import io
    
    io_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.)

    0 讨论(0)
  • 2020-12-30 20:40

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

    import tarfile, io
    byte_array = client.read_bytes()
    file_like_object = io.BytesIO(byte_array)
    tar = tarfile.open(fileobj=file_like_object)
    # use "tar" as a regular TarFile object
    for member in tar.getmembers():
        f = tar.extractfile(member)
        print(f)
    
    0 讨论(0)
提交回复
热议问题