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

前端 未结 2 634
温柔的废话
温柔的废话 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.)

提交回复
热议问题