【发布时间】:2016-02-14 18:48:28
【问题描述】:
我有以下 zip 文件结构:
some_file.zip/folder/folder/files.xml
所以我在 zip 文件的子文件夹中有很多 xml 文件。
到目前为止,我已经设法使用以下代码解压缩 zip 文件:
import os.path
import zipfile
with zipfile.ZipFile('some_file.zip') as zf:
for member in zf.infolist():
# Path traversal defense copied from
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
words = member.filename.split('/')
path = "output"
for word in words[:-1]:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir, ''): continue
path = os.path.join(path, word)
zf.extract(member, path)
但我不需要提取文件,而是直接从 zip 文件中读取它们。因此,要么在 for 循环中读取每个文件并对其进行处理,要么将每个文件保存在 Python 中的某种数据结构中。有可能吗?
【问题讨论】:
标签: python python-2.7 zip zipfile