Live data from Hacker News

Python – Create large ZIP archives without memory inflation

github.com

11–20 of 29 posts

Re: Python – Create large ZIP archives without memory inflation

#11

I'm a little perplexed by the "marketing" around this --- all the archivers I know of don't require more memory than the compression state (which AFAIK for ZIP/deflate is not much more than a 64k window), since it is natural that files can be larger than available RAM.

I believe the comparison is just to the bundled zipfile module and BytesIO, which would be the quick and dirty way to make a zipfile without creating actual files, but would be memory intensive.

Re: Python – Create large ZIP archives without memory inflation

#13

I'm a little perplexed by the "marketing" around this --- all the archivers I know of don't require more memory than the compression state (which AFAIK for ZIP/deflate is not much more than a 64k window), since it is natural that files can be larger than available RAM.

I think it's meant for a pretty narrow use-case: serving compressed files through frameworks(as mentioned, for example Django or Flask) that expect to serve file objects, but without writing to disk. The "usual"/naive solution (if you stay within the python ecosystem) is to compress the files and write to a BytesIO or other in-memory file like object, and then have your framework serve it. The naive solution leads to…

Ah, so it's like Haskell's streaming (de)compression functions?

Examples:

* https://hackage.haskell.org/package/conduit-extra-1.1.7.3/do...

* http://hackage.haskell.org/package/streaming-utils-0.2.0.0/d...

Re: Python – Create large ZIP archives without memory inflation

#15
post #2

Is there something like this for the JVM? I’m not sure whether with https://github.com/srikanth-lingala/zip4j#adding-entries-wit... will keep everything it is possible to keep it in constrained memory.

Even the JRE-builtin ZipOutputStream would do the job, it's a proper streaming implementation that doesn't keep more state than necessary.

Re: Python – Create large ZIP archives without memory inflation

#16

I'm a little perplexed by the "marketing" around this --- all the archivers I know of don't require more memory than the compression state (which AFAIK for ZIP/deflate is not much more than a 64k window), since it is natural that files can be larger than available RAM.

not a safe assumption with Python packages!

Re: Python – Create large ZIP archives without memory inflation

#17
I built a streaming zip app using nothing more then the Python stdlib zip implementation and some os primitives.

It runs on a small embedded device that can stream zip archives many times larger then the disk or system ram without any issue.

Example Python Falcon Proof of Concept:

https://gist.github.com/kylemanna/1e22bbf31b7e5ae84bbdfa32c6...

Other then what Python's zipfile buffers in memory, my implementation shouldn't use much more then a os.pipe()'s buffer (typically 64kB?).

Re: Python – Create large ZIP archives without memory inflation

#18
post #17

I built a streaming zip app using nothing more then the Python stdlib zip implementation and some os primitives. It runs on a small embedded device that can stream zip archives many times larger then the disk or system ram without any issue. Example Python Falcon Proof of Concept: https://gist.github.com/kylemanna/1e22bbf31b7e5ae84bbdfa32c6... Other then what Python's zipfile buffers in memory, my implementation shou…

Interesting.

I need to open a very large CSV file in Python, which is around 25GB in .zip format. Any idea how to do this in a streaming way, i.e. stopping after reading the first few thousand rows?

Re: Python – Create large ZIP archives without memory inflation

#19
post #18
post #17

I built a streaming zip app using nothing more then the Python stdlib zip implementation and some os primitives. It runs on a small embedded device that can stream zip archives many times larger then the disk or system ram without any issue. Example Python Falcon Proof of Concept: https://gist.github.com/kylemanna/1e22bbf31b7e5ae84bbdfa32c6... Other then what Python's zipfile buffers in memory, my implementation shou…

Interesting. I need to open a very large CSV file in Python, which is around 25GB in .zip format. Any idea how to do this in a streaming way, i.e. stopping after reading the first few thousand rows?

> I need to open a very large CSV file in Python, which is around 25GB in .zip format. Any idea how to do this in a streaming way, i.e. stopping after reading the first few thousand rows?

Replace the `file_paths` list in my proof of concept with your large file(s), delete the rest (lines 61-68, 77-79) and it should just work.

Re: Python – Create large ZIP archives without memory inflation

#20
I have questions about the code. Why do you need to say int('0x1', 16) and int('0x2', 16)? Why not just write 0x1 and 0x2? Or just plain 1 and 2?

I'm also perplexed by the goal as this seems to just call zipfile.write under the hood, which already streams to a zip file without accumulating a memory buffer?

[0] https://github.com/BuzonIO/zipfly/blob/master/zipfly/zipfly....

Post reply on HN