-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathzlibio.py
executable file
·64 lines (52 loc) · 1.7 KB
/
zlibio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import sys
import zlib
import getopt
USAGE = '''Usage: zlibio.py [OPTION]...
zlib compress or decompress standard input to standard output.
-d, --decompress decode data
-h, --help display this help and exit
-0, --zero do not compress data; just add zlib header
-1, --fast compress faster
-9, --best compress better
'''
def log(str):
sys.stderr.write('%s: %s\n' % (sys.argv[0], str))
def usage():
sys.stderr.write(USAGE)
def main():
flgCompress = True
compressLvl = -1 # Default by zlib for automatic
try:
opts, args = getopt.getopt(sys.argv[1 : ], 'hd0123456789', ['help', 'decompress', 'zero', 'fast', 'best'])
except getopt.GetoptError as e:
log(e)
return 1
for arg in args:
log('ignoring unknown argument "%s"' % arg)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
return 1
elif opt in ('-d', '--decompress'):
flgCompress = False
elif opt == '--zero':
compressLvl = 0
elif opt == '--fast':
compressLvl = 1
elif opt == '--best':
compressLvl = 9
elif opt in ('-%d' % x for x in range(10)):
compressLvl = -int(opt)
obj = zlib.compressobj(compressLvl) if flgCompress else zlib.decompressobj()
data = sys.stdin.buffer.read(1024)
while data:
sys.stdout.buffer.write(obj.compress(data) if flgCompress else obj.decompress(data))
data = sys.stdin.buffer.read(1024)
sys.stdout.buffer.write(obj.flush())
return 0
if __name__ == '__main__':
try:
exit(main())
except KeyboardInterrupt:
exit(130)