views:

43

answers:

2

Hi,

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.

+3  A: 

http://docs.python.org/library/zipfile.html

Specifically, try using the ZipFile.namelist() method.

Andrew Sledge
+1 for being first
David Zaslavsky
Thanks for answering first too :D
Jonathan
+5  A: 

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', ...]
David Zaslavsky
lol @ Stackoverflow race conditions
Andrew Sledge
oh heh, sorry @Andrew I didn't even notice your answer there, I was in such a hurry to edit mine ;-)
David Zaslavsky
no biggie. Good answer tho :)
Andrew Sledge
Great, thanks for the answer.
Jonathan