My existing code (a section) is loacted at the bottom of this post.
What I would like to do is extract all of the files from a directory inside of a zip file. Not the whole contents of a zip file, just the files in a directory ( /theme_files/
) that's inside of a zip. I have already imported the neccesary libraries.
Extract the files from [filename].tpk/theme_files
into ./workspace/[output]/
( .tpk
= .zip
)
theme_zip = zipfile.ZipFile(current_dir + "/themes/" + theme_name + ".tpk", 'r')
theme_zip.extractall(output_dir)
theme_zip.close()
for item in (f for f in theme_zip.filelist if 'theme_files/' in f.filename):
theme_zip.extract(item, output_dir)
You could use ZipFile.extractall()
and set the path
and members
args using a list comprehension on the filenames returned from ZipFile.namelist()
:
import zipfile
output_dir = './workspace/[output]/'
file = current_dir + "/themes/" + theme_name + ".tpk"
with zipfile.ZipFile(file, 'r') as f:
files = [n for n in f.namelist()
if n.startswith('/theme_files/') and not n.endswith('/')]
f.extractall(path=output_dir , members=files)
Search the file names in the specific subfolders with regex. + Extract them file by file:
with open('<file-name>.zip', 'rb') as f:
zf = zipfile.ZipFile(f)
for file in [m.group() for m in (re.search(r'/theme/(.+)', file) for file in file_list) if m]:
zf.extract(file)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.