Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python, getting the name of files in a zip archive

I have several very large zip files available to download on a website. I am using Flask microframework (based on Werkzeug) which uses Python.

Is there a way to show the contents of a zip file (i.e. file and folder names) - to someone on a webpage - without actually downloading it? As in doing the working out server side.

Assume that I do not know what are in the zip archives myself.

I apoligize that this post does not include code.

Thank you for helping.

like image 324
Jon Cox Avatar asked Sep 09 '10 17:09

Jon Cox


People also ask

How do I view the contents of a ZIP file in Python?

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.

How do I extract text from a ZIP file in Python?

extractall() method will extract all the contents of the zip file to the current working directory. You can also call extract() method to extract any file by specifying its path in the zip file. This will extract only the specified file.

What is ZIP file in Python?

Python's zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.

How do I extract data from a ZIP file?

To unzip filesOpen File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.


1 Answers

Sure, have a look at zipfile.ZipFile.namelist(). Usage is pretty simple, as you'd expect: you just create a ZipFile object for the file you want, and then namelist() gives you a list of the paths of files stored in the archive.

with ZipFile('foo.zip', 'r') as f:
    names = f.namelist()
print names
# ['file1', 'folder1/file2', ...]
like image 52
David Z Avatar answered Sep 25 '22 20:09

David Z