-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathzlib.py
More file actions
39 lines (30 loc) · 976 Bytes
/
zlib.py
File metadata and controls
39 lines (30 loc) · 976 Bytes
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
# MicroPython zlib module
# MIT license; Copyright (c) 2023 Jim Mussared
import io, deflate
_MAX_WBITS = const(15)
def _decode_wbits(wbits, decompress):
if -15 <= wbits <= -5:
return (
deflate.RAW,
-wbits,
)
elif 5 <= wbits <= 15:
return (deflate.ZLIB, wbits)
elif decompress and wbits == 0:
return (deflate.ZLIB,)
elif 21 <= wbits <= 31:
return (deflate.GZIP, wbits - 16)
elif decompress and 35 <= wbits <= 47:
return (deflate.AUTO, wbits - 32)
else:
raise ValueError("wbits")
if hasattr(deflate.DeflateIO, "write"):
def compress(data, wbits=_MAX_WBITS):
f = io.BytesIO()
with deflate.DeflateIO(f, *_decode_wbits(wbits, False)) as g:
g.write(data)
return f.getvalue()
def decompress(data, wbits=_MAX_WBITS):
f = io.BytesIO(data)
with deflate.DeflateIO(f, *_decode_wbits(wbits, True)) as g:
return g.read()