Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get file list of files contained in a zip file

People also ask

How do I list files in a zip file?

To list/view the contents of a compressed file on a Linux host without uncompressing it (and where GZIP is installed), use the "zcat" command. This is not an answer to my question; it's about zip, not gzip; and not about cat'ing the contents but listing which files are in the zip archive.

How do I browse the contents of a zip file?

There are several commands that can read the contents of ZIP files, but the easiest to remember by far is the command "zipinfo." Just open the Terminal, then type "zipinfo" followed by a space. Then drag the ZIP file to the Terminal window and press Enter after the full path to the file has been entered.

How do I view the content of a zip file in Linux?

Lucky for you, the unzip command has the -l option that displays the contents of a zip file without extracting them. To view a ZIP file's contents, run the unzip command to list ( -l ) the zip file's ( newdir. zip ) contents without extracting them.

How do you view the contents of zip file without extracting the file in Linux?

Using Vim. Vim command can also be used to view the contents of a ZIP archive without extracting it. It can work for both the archived files and folders. Along with ZIP, it can work with other extensions as well, such as tar.


What you need is ZipFile.namelist() that will give you a list of all the contents of the archive, you can then do a zip.open('filename_you_discover') to get the contents of that file.


import zipfile

# zip file handler  
zip = zipfile.ZipFile('filename.zip')

# list available files in the container
print (zip.namelist())

# extract a specific file from the zip container
f = zip.open("file_inside_zip.txt")

# save the extraced file 
content = f.read()
f = open('file_inside_zip.extracted.txt', 'wb')
f.write(content)
f.close()

import zipfile

zip=zipfile.ZipFile('my_zip.zip')
f=zip.open('my_txt_file.txt')
contents=f.read()
f.close()

You can see the documentation here. In particular, the namelist() method will give you the names of the zip file members.