获取远程zip文件并列出其中的文件

use*_*585 3 python zip google-app-engine

我正在开发一个小型Google App Engine项目,我需要从URL中获取远程zip文件,然后列出zip存档中包含的文件.

我正在使用该zipfile模块.

这是我到目前为止所提出的:

# 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()
Run Code Online (Sandbox Code Playgroud)

不幸的是,archive_files列表总是长度为0.

我有什么想法我做错了吗?

Jus*_*gan 6

您正在打开具有w权限的文件,这会截断它.将其更改r为阅读权限:

zip_file = zipfile.ZipFile(file_stream, 'r')
Run Code Online (Sandbox Code Playgroud)

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