Search code examples
python-3.xarchiveextractgzip

Viewing the files contained in a .gz archive using python


I am looking for a simple way to view the contents (file names) of a .gz archive. I saw that there has been a discussion already on how to do this when using .zip archive. So basically I am looking for the analogue of ZipFile.namelist(), but for .gz archives.


Solution

  • .gz files are not, in and of themselves, archives. A .gz file contains a single compressed file. When you decompress foo.bar.gz, you get the file foo.bar. So there's your one name, in the name of the .gz file itself.

    You might have a .tar.gz file, which is a TAR (tape archive) file containing a representation of a directory of files, that is then compressed as a single file by gzip. In that case, you would need to use Python's tarfile to read the file and list the contents.