tags:

views:

68

answers:

2

I have a large tar.gz file to analyze using a python script. The tar.gz file contains a number of zip files which might embed other .gz files in it. Before extracting the file, I would like to walk through the directory structure within the compressed files to see if certain files or directories are present. By looking at tarfile and zipfile module I don't see any existing function that allow me to get a table of content of a zip file within a tar.gz file.

Appreciate your help,

+1  A: 

I suspect that this is not possible and that you'll have to program it manually.

.tar.gz files are first tar'd then gzipped with what is essentially two different applications, in succession. To access the tar file, you're probably going to have to un-gzip it, first.

Also, once you do have access to the tar file after ungzipping it, it does not do random-access well. There is no central repository in the tar file that lists the contents.

orangeoctopus
The tarfile module has a option to list a TOC of a tar.gz filetar = tarfile.open("sample.tar.gz", "r:gz")What I am looking for is to get a TOC of a zip file within the tar.gz file without extracting the file.Thanks,
JasonA
A: 

You can't get at it without extracting the file. However, you don't need to extract it to disk if you don't want to. You can use the tarfile.TarFile.extractfile method to get a file-like object that you can then pass to tarfile.open as the fileobj argument. For example, given these nested tarfiles:

$ cat bar/baz.txt     
This is bar/baz.txt.
$ tar cvfz bar.tgz bar
bar/
bar/baz.txt
$ tar cvfz baz.tgz bar.tgz
bar.tgz

You can access files from the inner one like so:

>>> import tarfile
>>> baz = tarfile.open('baz.tgz')
>>> bar = tarfile.open(fileobj=baz.extractfile('bar.tgz'))
>>> bar.extractfile('bar/baz.txt').read()
'This is bar/baz.txt.\n'

and they're only ever extracted to memory.

Thomas Wouters
Thanks for the reply. I actually don't need to read the content of the files. I just need to get the TOC of a certain compressed file within the tgz file. Looks like it is still not possible.
JasonA
Not without extracting to memory, no; that's not how tar files work. tar files are streams (because they were meant to go on tape devices, which can't seek back and forth), so the only way to see what you have is by extracting and reading. You still don't have to extract *to disk*, though. You can get at `bar`'s TOC just like you can with `baz`'s.
Thomas Wouters