2023-06-26 14:30:45 +00:00
|
|
|
# MicroPython gzip module
|
|
|
|
# MIT license; Copyright (c) 2023 Jim Mussared
|
2015-01-28 01:04:11 +00:00
|
|
|
|
2023-06-26 14:30:45 +00:00
|
|
|
_WBITS = const(15)
|
|
|
|
|
2024-02-29 03:54:24 +00:00
|
|
|
import builtins, io, deflate
|
2023-06-26 14:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def GzipFile(fileobj):
|
|
|
|
return deflate.DeflateIO(fileobj, deflate.GZIP, _WBITS)
|
|
|
|
|
|
|
|
|
2024-02-29 03:54:24 +00:00
|
|
|
def open(filename, mode="rb"):
|
|
|
|
return deflate.DeflateIO(builtins.open(filename, mode), deflate.GZIP, _WBITS, True)
|
2023-06-26 14:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if hasattr(deflate.DeflateIO, "write"):
|
|
|
|
|
|
|
|
def compress(data):
|
|
|
|
f = io.BytesIO()
|
|
|
|
with GzipFile(fileobj=f) as g:
|
|
|
|
g.write(data)
|
|
|
|
return f.getvalue()
|
2015-01-28 01:04:11 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-01-28 01:04:11 +00:00
|
|
|
def decompress(data):
|
2023-06-26 14:30:45 +00:00
|
|
|
f = io.BytesIO(data)
|
|
|
|
with GzipFile(fileobj=f) as g:
|
|
|
|
return g.read()
|