简体   繁体   中英

Behavior of Python 2.7's ZipFile when zipping files

I'm currently working with python 2.7's ZipFile. I have been running into memory issues zipping multiple files to send to the user.

When I have code:

fp = open('myzip.zip', 'w')
archive = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
for filepath in filepaths:
   archive.write(filepath)
archive.close()

Does Python load all those files into memory at some point? I would have expected Python to stream the contents of the files into the zip, but I'm not sure that that is the case.

This question/answer (both by same user) suggests that it's all done in memory. They provide a link to a modified library EnhancedZipFile that sounds like it works as you desire, however it doesn't seem to have had much activity on the project.

If you're not dependent on zip specifically, then this answer implies that the bzip library can handle large files.

Python将把您迭代的所有文件保存在内存中,除了调用在操作系统上为您完成工作的可执行文件外,我无法想到绕过它。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM