3 ms·
Yes, it's clearly documented but very much unexpected the first time 'round. The reason for the default is probably that python can be built with none of zlib,
by masklinn 6y ago
Yes, it's clearly documented but very much unexpected the first time 'round.
The reason for the default is probably that python can be built with none of zlib, bzip2 or lzma support:
> If ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA is specified but the corresponding module (zlib, bz2 or lzma) is not available, RuntimeError is raised.
in which case only STORED is available, therefore that's the only possible default, any other would lead to zipfile raising errors out of the box for some people.
It would probably have been a better idea to just not use a default, and require the compression method to always be specified, at least when opening zipfiles with modes other than "r".
- duskwuff 6y agoTBH, Python's zipfile should probably just require zlib. Without that module, it's basically useless (can't open most ZIP files, can't emit compressed ZIP files).
- Aachen 6y agoActually, I've argued the opposite in the past. Any file that is of any significant size is already compressed: images, audio, video, gifs, PDFs, game map/save data... even documents (and by extension, spreadsheets and presentations) are compressed these days (because they're... zips! It's zips all the way down). Compression in your zip archive implementation is unnecessary complexity at this point. Compressing archives most only makes sense if you have some huge (many many millions of lines) set of source code because that's a lot of data, more than one file that you want to compress, and low entropy in those files. Only in that combination does zip with compression make sense over uncompressed archives or compression without archive format. Of course, zip can shoehorn both functions that you usually don't need together into one format and we're usually not CPU-bound anyway so I totally see why you might as well always apply a small amount of compression, but I would beg to differ if you say "it's basically useless to have zip without compression support". There are more cases where zip with compression is completely redundant than cases where it helps you.
- duskwuff 6y ago> even documents (and by extension, spreadsheets and presentations) are compressed these days (because they're... zips! It's zips all the way down) And how do you read those archives (or, for that matter, create them) using a ZIP library that doesn't know how to deal with compressed data? My point is simply that DEFLATE support should be considered mandatory for any ZIP implementation. Without it, most archives you encounter will be unreadable. Yes, it can still handle archives which contain only STOREd files -- but that's an edge case which most archives won't fall into.
- masklinn 6y ago> And how do you read those archives (or, for that matter, create them) using a ZIP library that doesn't know how to deal with compressed data? You don’t because you just want to archive them not read them.
- Dylan16807 6y ago> in which case only STORED is available, therefore that's the only possible default, any other would lead to zipfile raising errors out of the box for some people. I don't think that logic works. You can default to "deflate if available" just fine.
- masklinn 6y agoAn inconsistent and variable default would be significantly, unfathomably, worse than a "bad" default. And I don’t even have to imagine it, because there’s an example right in the python builtins: open in text mode will use… whatever garbage `locale.getpreferredencoding()` returns. Fantastic way to create hard to reproduce bugs.
- Dylan16807 6y agoThat's because using non-binary mode is bad, not because variable defaults are bad. > An inconsistent and variable default would be significantly, unfathomably, worse than a "bad" default. Even if you're right on that, I will still say that defaulting to no compression is significantly worse than going "error missing library" for people that have a partial install.
- masklinn 6y ago> That's because using non-binary mode is bad Text mode is perfectly good, it’s what users want in a good 90% of cases. Possibly more.
- Dylan16807 6y agoThey want text, and they want line endings that work, but that doesn't mean they want file contents secretly changed as they go.