如何递归提取zip文件? [英] How to extract zip file recursively?
问题描述
我有一个 zip 文件,其中包含三个 zip 文件,如下所示:
I have a zip file which contains three zip files in it like this:
zipfile.zip\
dirA.zip\
a
dirB.zip\
b
dirC.zip\
c
我想在具有这些名称(dirA、dirB、dirC)的目录中提取 zip 文件中的所有内部 zip 文件.
基本上,我想最终得到以下架构:
I want to extract all the inner zip files that are inside the zip file in directories with these names (dirA, dirB, dirC).
Basically, I want to end up with the following schema:
output\
dirA\
a
dirB\
b
dirC\
c
我尝试了以下方法:
import os, re
from zipfile import ZipFile
os.makedirs(directory) # where directory is "\output"
with ZipFile(self.archive_name, "r") as archive:
for id, files in data.items():
if files:
print("Creating", id)
dirpath = os.path.join(directory, id)
os.mkdir(dirpath)
for file in files:
match = pattern.match(filename)
new = match.group(2)
new_filename = os.path.join(dirpath, new)
content = archive.open(file).read()
with open(new_filename, "wb") as outfile:
outfile.write(content)
但它只提取 zip 文件,我最终得到:
But it only extracts the zip file and I end up with:
output\
dirA\
dirA.zip
dirB\
dirB.zip
dirC\
dirC.zip
任何包括代码段的建议都将不胜感激,因为我尝试了很多不同的东西,但没有成功阅读文档.
Any suggestions including code-segments will be much appreciated cause I have tried so many different things and read the docs without success.
推荐答案
在提取 zip 文件时,您可能希望将内部 zip 文件写入内存而不是磁盘上.为此,我使用了 BytesIO
.
When extracting the zip file, you would want to write the inner zip files to memory instead of them on disk. To do this, I've used BytesIO
.
查看此代码:
import os
import io
import zipfile
def extract(filename):
z = zipfile.ZipFile(filename)
for f in z.namelist():
# get directory name from file
dirname = os.path.splitext(f)[0]
# create new directory
os.mkdir(dirname)
# read inner zip file into bytes buffer
content = io.BytesIO(z.read(f))
zip_file = zipfile.ZipFile(content)
for i in zip_file.namelist():
zip_file.extract(i, dirname)
如果您使用 zipfile.zip
运行 extract("zipfile.zip")
为:
If you run extract("zipfile.zip")
with zipfile.zip
as:
zipfile.zip/
dirA.zip/
a
dirB.zip/
b
dirC.zip/
c
输出应该是:
dirA/
a
dirB/
b
dirC/
c
这篇关于如何递归提取zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!