How to go through a tar.gz file containing zip files without extracting

I have a large tar.gz file for analysis using a python script. The tar.gz file contains several zip files that other .gz files can insert into it. Before extracting a file, I would like to go through the directory structure in compressed files to see if certain files or directories exist. If you look at the tarfile and zipfile module, I don’t see any existing function that would allow me to get the table of contents of the zip file in the tar.gz file.

Appreciate your help

+3
source share
2 answers

, . , . tarfile.TarFile.extractfile, - , tarfile.open fileobj. , tar :

$ 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

, :

>>> 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'

.

+5

, , .

.tar.gz tar'd, gzipped , , . tar, , , .

, tar , . tar .

+1

Source: https://habr.com/ru/post/1755602/


All Articles