Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python zipfile, How to set the compression level?

Tags:

python

zip

zlib

Python supports zipping files when zlib is available, ZIP_DEFLATE

see: https://docs.python.org/3.4/library/zipfile.html

The zip command-line program on Linux supports -1 fastest, -9 best.

Is there a way to set the compression level of a zip file created in Python's zipfile module?

like image 538
ideasman42 Avatar asked Dec 17 '14 12:12

ideasman42


2 Answers

Starting from python 3.7, the zipfile module added the compresslevel parameter.

https://docs.python.org/3/library/zipfile.html

I know this question is dated, but for people like me, that fall in this question, it may be a better option than the accepted one.

like image 123
Andre Guilhon Avatar answered Sep 21 '22 23:09

Andre Guilhon


The zipfile module does not provide this. During compression it uses constant from zlib - Z_DEFAULT_COMPRESSION. By default it equals -1. So you can try to change this constant manually, as possible solution.

like image 45
Alex Lisovoy Avatar answered Sep 18 '22 23:09

Alex Lisovoy