svn-gvsig-desktop / tags / v1_0_2_Build_907 / extensions / extScripting / scripts / jython / Lib / zlib.py @ 11015
History | View | Annotate | Download (3.02 KB)
1 |
|
---|---|
2 |
from java import util, lang |
3 |
import jarray, binascii |
4 |
|
5 |
class error(Exception): |
6 |
pass
|
7 |
|
8 |
|
9 |
DEFLATED = 8
|
10 |
MAX_WBITS = 15
|
11 |
DEF_MEM_LEVEL = 8
|
12 |
ZLIB_VERSION = "1.1.3"
|
13 |
Z_BEST_COMPRESSION = 9
|
14 |
Z_BEST_SPEED = 1
|
15 |
|
16 |
Z_FILTERED = 1
|
17 |
Z_HUFFMAN_ONLY = 2
|
18 |
|
19 |
Z_DEFAULT_COMPRESSION = -1
|
20 |
Z_DEFAULT_STRATEGY = 0
|
21 |
|
22 |
# Most options are removed because java does not support them
|
23 |
# Z_NO_FLUSH = 0
|
24 |
# Z_SYNC_FLUSH = 2
|
25 |
# Z_FULL_FLUSH = 3
|
26 |
Z_FINISH = 4
|
27 |
|
28 |
def adler32(string, value=1): |
29 |
if value != 1: |
30 |
raise ValueError, "adler32 only support start value of 1" |
31 |
checksum = util.zip.Adler32() |
32 |
checksum.update(lang.String.getBytes(string)) |
33 |
return lang.Long(checksum.getValue()).intValue()
|
34 |
|
35 |
def crc32(string, value=0): |
36 |
return binascii.crc32(string, value)
|
37 |
|
38 |
|
39 |
def compress(string, level=6): |
40 |
if level < Z_BEST_SPEED or level > Z_BEST_COMPRESSION: |
41 |
raise error, "Bad compression level" |
42 |
deflater = util.zip.Deflater(level, 0)
|
43 |
deflater.setInput(string, 0, len(string)) |
44 |
deflater.finish() |
45 |
return _get_deflate_data(deflater)
|
46 |
|
47 |
def decompress(string, wbits=0, bufsize=16384): |
48 |
inflater = util.zip.Inflater(wbits < 0)
|
49 |
inflater.setInput(string) |
50 |
return _get_inflate_data(inflater)
|
51 |
|
52 |
|
53 |
class compressobj: |
54 |
def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS, |
55 |
memLevel=0, strategy=0): |
56 |
if abs(wbits) > MAX_WBITS or abs(wbits) < 8: |
57 |
raise ValueError, "Invalid initialization option" |
58 |
self.deflater = util.zip.Deflater(level, wbits < 0) |
59 |
self.deflater.setStrategy(strategy)
|
60 |
if wbits < 0: |
61 |
_get_deflate_data(self.deflater)
|
62 |
|
63 |
def compress(self, string): |
64 |
self.deflater.setInput(string, 0, len(string)) |
65 |
return _get_deflate_data(self.deflater) |
66 |
|
67 |
def flush(self, mode=Z_FINISH): |
68 |
if mode != Z_FINISH:
|
69 |
raise ValueError, "Invalid flush option" |
70 |
self.deflater.finish()
|
71 |
return _get_deflate_data(self.deflater) |
72 |
|
73 |
class decompressobj: |
74 |
def __init__(self, wbits=0): |
75 |
if abs(wbits) > MAX_WBITS or abs(wbits) < 8: |
76 |
raise ValueError, "Invalid initialization option" |
77 |
self.inflater = util.zip.Inflater(wbits < 0) |
78 |
self.unused_data = "" |
79 |
|
80 |
def decompress(self, string): |
81 |
self.inflater.setInput(string)
|
82 |
r = _get_inflate_data(self.inflater)
|
83 |
# Arrgh. This suck.
|
84 |
self.unused_data = " " * self.inflater.getRemaining() |
85 |
return r
|
86 |
|
87 |
def flush(self): |
88 |
#self.inflater.finish()
|
89 |
return _get_inflate_data(self.inflater) |
90 |
|
91 |
|
92 |
def _get_deflate_data(deflater): |
93 |
buf = jarray.zeros(1024, 'b') |
94 |
sb = lang.StringBuffer() |
95 |
while not deflater.finished(): |
96 |
l = deflater.deflate(buf) |
97 |
if l == 0: |
98 |
break
|
99 |
sb.append(lang.String(buf, 0, 0, l)) |
100 |
return sb.toString()
|
101 |
|
102 |
|
103 |
def _get_inflate_data(inflater): |
104 |
buf = jarray.zeros(1024, 'b') |
105 |
sb = lang.StringBuffer() |
106 |
while not inflater.finished(): |
107 |
l = inflater.inflate(buf) |
108 |
if l == 0: |
109 |
break
|
110 |
sb.append(lang.String(buf, 0, 0, l)) |
111 |
return sb.toString()
|
112 |
|