Create a zip file from a generator in Python?

后端 未结 10 1886
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 07:32

I\'ve got a large amount of data (a couple gigs) I need to write to a zip file in Python. I can\'t load it all into memory at once to pass to the .writestr method of ZipFil

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 08:11

    The gzip library will take a file-like object for compression.

    class GzipFile([filename [,mode [,compresslevel [,fileobj]]]])
    

    You still need to provide a nominal filename for inclusion in the zip file, but you can pass your data-source to the fileobj.

    (This answer differs from that of Damnsweet, in that the focus should be on the data-source being incrementally read, not the compressed file being incrementally written.)

    And I see now the original questioner won't accept Gzip :-(

提交回复
热议问题