1

I get a tar file in the body of a http post request and I want to extract the contents without first writing the tar file to disc. The file used to be a zip file and i could do

zip = zipfile.ZipFile(StringIO(request.content.read()))
zip.extractall(some_path)

How do i do the same thing using tarfile.TarFile?

I tried

tar = tarfile.open(fileobj = StringIO(self.request.content.read()))

but got the error message:

file could not be opened successfully
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
user1622094
  • 193
  • 1
  • 1
  • 7

1 Answers1

0

Use the fileobj parameter to tarfile.open.

Edit: I tried the following:

# t.py
import tarfile
import StringIO

tarf = open('test.tar', 'rb')
tarstrio = StringIO.StringIO(tarf.read())
tar = tarfile.open(fileobj=tarstrio)

tar.list()

and it worked as expected.

John Keyes
  • 5,479
  • 1
  • 29
  • 48