[英]Extract file from ZIP straight to another ZIP
My goal is to extract certain files from Zip archive and stream them straight to another Zip without having to perform intermediate extraction to the disk. 我的目标是从Zip存档中提取某些文件并将它们直接传输到另一个Zip,而不必对磁盘执行中间提取。
So far I have: 到目前为止,我有:
from zipfile import ZipFile, ZIP_DEFLATED
def stream_conents(src_zip, dst_zip, file_subset_list):
with ZipFile(src_zip, "r", compression=ZIP_DEFLATED) as src_zip_archive:
with ZipFile(dst_zip, "w", compression=ZIP_DEFLATED) as dst_zip_archive:
for zitem in src_zip_archive.namelist():
if zitem in file_subset_list:
zitem_object = src_zip_archive.open(zitem)
dst_zip_archive.write(zitem_object, zitem, )
But it just throws TypeError: argument should be string, bytes or integer, not ZipExtFile
但它只是抛出
TypeError: argument should be string, bytes or integer, not ZipExtFile
You can read the entire file into memory and use writestr
to write the archive. 您可以将整个文件读入内存并使用
writestr
来编写存档。
def stream_conents(src_zip, dst_zip, file_subset_list):
with ZipFile(src_zip, "r", compression=ZIP_DEFLATED) as src_zip_archive:
with ZipFile(dst_zip, "w", compression=ZIP_DEFLATED) as dst_zip_archive:
for zitem in src_zip_archive.namelist():
if zitem in file_subset_list:
# warning, may blow up memory
dst_zip_archive.writestr(zitem,
src_zip_archive.read(zitem))
Starting with python 3.6, ZipFile.open
will open archive files in write mode. 从python 3.6开始,
ZipFile.open
将以写入模式打开存档文件。 That lets you write the file in chunks and reduce overall memory usage. 这使您可以以块的形式编写文件并减少总体内存使用量。
def stream_conents(src_zip, dst_zip, file_subset_list):
with ZipFile(src_zip, "r", compression=ZIP_DEFLATED) as src_zip_archive:
with ZipFile(dst_zip, "w", compression=ZIP_DEFLATED) as dst_zip_archive:
for zitem in src_zip_archive.namelist():
if zitem in file_subset_list:
if sys.version_info >= (3, 6):
with src_zip_archive.open(zitem) as from_item:
with dst_zip_archive.open(zitem, "w") as to_item:
shutil.copyfileobj(from_item, to_item)
else:
# warning, may blow up memory
dst_zip_archive.writestr(zitem,
src_zip_archive.read(zitem))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.