-
Notifications
You must be signed in to change notification settings - Fork 3
/
gziplength.py
65 lines (48 loc) · 1.41 KB
/
gziplength.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
64
65
import zlib
import gzip
class GzipLengthCalc:
"""
Efficient way to compute both,
len(gzip.compress(data1))
ane
len(gzip.compress(data1 + b" " + data2))
(and can be reused for multiple data2)
Usage: (all data in bytes)
g = GzipLengthCalc(data1)
n1 = g.length1
n2 = g.length2(data2)
n2b = g.length2(data2b) # re-use for different data2
"""
def __init__(self,data):
"""
"""
#args copied from gzip.py:
compress = zlib.compressobj(
gzip._COMPRESS_LEVEL_BEST,
zlib.DEFLATED,
-zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL,
0)
self.len_header_footer = 10 + 8
#often '' (ie not flushed)
self.clen1 = len(compress.compress(data))
#store zlib state:
self.compress1 = compress.copy()
#finish:
n = self.len_header_footer
n += self.clen1
n += len(compress.flush())
# length1 = len(gzip.compress(data))
self.length1 = n
# we can do the " " here
self.len2a = len(self.compress1.compress(b" "))
def length2(self,data):
"""
"""
c = self.compress1.copy()
n = self.len_header_footer
#lengths from first step:
n += self.clen1 + self.len2a
n += len(c.compress(data))
n += len(c.flush())
return n