-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncompress.py
executable file
·53 lines (42 loc) · 1.2 KB
/
uncompress.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
#!/usr/bin/env python
from htree import mkht_methods, Node
from serqu import unserializq
from tobytes import tobytes
from struct import unpack, calcsize
def bytes2bits(res):
bs = []
for r in res:
if not isinstance(r, int):
r = ord(r)
bs += [True if (r & (1 << i)) != 0 else False for i in range(8)]
return bs
def unapplyhtr(bs, htr):
res = []
n = htr
for b in bs:
if not isinstance(n, Node):
res.append(n)
n = htr
n = n.r if b else n.l
return res
def main(fin, fout, meth):
if fin.read(2) != b'\x7fZ':
raise Exception('wrong file format!')
meth = fin.read(4).decode('utf-8').lower()
if meth not in mkht_methods:
raise Exception('unknown compressing method!')
outsize = unpack('I', fin.read(calcsize('I')))
if isinstance(outsize, tuple):
outsize = outsize[0]
que = unserializq(fin)
htr = mkht_methods[meth](que)
res = fin.read()
fin.close()
bs = bytes2bits(res)
res = unapplyhtr(bs, htr)
if len(res) < outsize:
res += ['\n' for i in range(outsize - len(res))]
else:
res = res[:outsize]
fout.write(tobytes(res))
fout.close()