Skip to content

Instantly share code, notes, and snippets.

@Kijewski
Created June 9, 2017 14:15
Show Gist options
  • Save Kijewski/081222d56bc7ee5b412af25aec53499e to your computer and use it in GitHub Desktop.
Save Kijewski/081222d56bc7ee5b412af25aec53499e to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# encoding: UTF-8
from os import (
close, open, read, write,
O_WRONLY, O_APPEND, O_CREAT, O_EXCL, O_NOCTTY,
)
from sys import argv
from zlib import compressobj, DEFLATED, Z_DEFAULT_STRATEGY
Z_PARTIAL_FLUSH = 1
Z_FULL_FLUSH = 3
Z_FINISH = 4
if __name__ == '__main__':
obj = compressobj(9, DEFLATED, 16 | 15, 8, Z_DEFAULT_STRATEGY)
if len(argv) != 2:
raise SystemExit('Usage: ' + argv[0] + ' /path/to/logfile.log.gz')
out_fd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT | O_EXCL | O_NOCTTY, 0o640)
try:
write(out_fd, obj.flush(Z_PARTIAL_FLUSH))
try:
while True:
try:
block = read(0, 4096)
except (EOFError, KeyboardInterrupt):
break
else:
if not block:
break
else:
write(1, block)
write(out_fd, obj.compress(block))
write(out_fd, obj.flush(Z_PARTIAL_FLUSH))
finally:
write(out_fd, obj.flush(Z_FINISH))
finally:
close(out_fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment