[Tutor] using tarfile on strings or filelike objects
Kent Johnson
kent37 at tds.net
Sat Mar 3 23:26:06 CET 2007
Barton David wrote:
> I like that I can access the contents of a zip archive that's stored in
> memory (rather than on disk) by packing the archive contents into a
> StringIO or cStringIO object and feeding that to ZipFile...
>
> i.e.
>
> filelike=cStringIO.StringIO(archive_as_string)
> zf=zipfile.ZipFile(filelike)
> content=zf.read(archive_member_name)
> zf.close()
> filelike.close()
>
> but I can't get the same thing to work with TarFile. Is there any way to
> do this? (Other than first saving the archive data to disk and then
> passing the path to TarFile.open?) The tarfile module documentation
> talks about an optional fileobj flag but this doesn't seem to work.
Just from reading the docs I would try
filelike = cStringIO...
tf = tarfile.open(mode='r|', fileobj=filelike)
This is based on the example in the docs of reading from stdin.
Kent
More information about the Tutor
mailing list