Hello iquano,
Using the stackexcange post, and adapting the python code posted for uncompression to compression, you can use the following:
import zlib
with open('test', 'rb') as input:
uncomp=input.read()
input.close()
z=zlib.compress(uncomp)
with open('test.z', 'wb') as f:
f.write(z)
f.close()
The input file here is called test, creating the compressed test.z