Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python tar file how to extract file into stream

Tags:

python

stream

tar

I am trying to extract a zipped folder but instead of directly using .extractall(), I want to extract the file into stream so that I can handle the stream myself. Is it possible to do it using tarfile? Or is there any suggestions?

like image 976
Robin W. Avatar asked Nov 26 '12 09:11

Robin W.


People also ask

How do I unpack a tar file?

To extract (unzip) a tar.gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.gz files. The -v option will make the tar command more visible and print the names of the files being extracted on the terminal.

How do I read a tar file in Python?

You can use the tarfile module to read and write tar files. To extract a tar file, you need to first open the file and then use the extract method of the tarfile module.


2 Answers

I was unable to extractfile while network streaming a tar file, I did something like this instead:

from backports.lzma import LZMAFile
import tarfile
some_streamed_tar = LZMAFile(requests.get('http://some.com/some.tar.xz').content)
with tarfile.open(fileobj=some_streamed_tar) as tf:
    tarfileobj.extractall(path="/tmp", members=None)

And to read them:

for fn in os.listdir("/tmp"):
    with open(os.path.join(t, fn)) as f:
        print(f.read())

python 2.7.13

like image 139
jmunsch Avatar answered Nov 15 '22 16:11

jmunsch


You can obtain each file from a tar file as a python file object using the .extractfile() method. Loop over the tarfile.TarFile() instance to list all entries:

import tarfile

with tarfile.open(path) as tf:
    for entry in tf:  # list each entry one by one
        fileobj = tf.extractfile(entry)
        # fileobj is now an open file object. Use `.read()` to get the data.
        # alternatively, loop over `fileobj` to read it line by line.
like image 31
Martijn Pieters Avatar answered Nov 15 '22 16:11

Martijn Pieters