Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch a remote zip file and list the files within

I'm working on a small Google App Engine project in which I need to fetch a remote zip file from a URL and then list the files contained in the zip archive.

I'm using the zipfile module.

Here's what I've come up with so far:

# fetch the zip file from its remote URL
result = urlfetch.fetch(zip_url)

# store the contents in a stream
file_stream = StringIO.StringIO(result.content)

# create the ZipFile object
zip_file = zipfile.ZipFile(file_stream, 'w')

# read the files by name
archive_files = zip_file.namelist()

Unfortunately the archive_files list is always of length 0.

Any ideas what I'm doing wrong?

like image 971
user722585 Avatar asked Feb 24 '23 10:02

user722585


1 Answers

You are opening the file with w permissions, which truncates it. Change it to r permissions for reading:

zip_file = zipfile.ZipFile(file_stream, 'r')

Reference: http://docs.python.org/library/zipfile.html#zipfile-objects

like image 53
Justin Morgan Avatar answered Mar 03 '23 23:03

Justin Morgan