Updater: Gzip resources dejavu, refactor for CompressStreamDecoder API (#152)

* Add back uzlib

* FBT: Support different resources compress methods

* Tar: Detect gzip compress type

* Tar: Generic compressed stream refactor

* Compress: Shared config params

* Fix comment

* Compress: Scaffolding for gzip implementation

* Compress: Handle errors from allocation

* Compress: Gzip support implementation

* Tar/Compress: Don't rewind if already at stream cursor

* Update changelog
This commit is contained in:
WillyJL
2024-07-03 02:26:59 +02:00
committed by GitHub
parent 1cd18f61a2
commit b18ac01485
14 changed files with 282 additions and 74 deletions

View File

@@ -1,4 +1,5 @@
import io
import gzip
import tarfile
import heatshrink2
@@ -6,7 +7,9 @@ import heatshrink2
from .heatshrink_stream import HeatshrinkDataStreamHeader
FLIPPER_TAR_FORMAT = tarfile.USTAR_FORMAT
TAR_HEATSRINK_EXTENSION = ".ths"
TAR_HEATSHRINK_EXTENSION = ".ths"
TAR_GZIP_EXTENSION = ".tgz"
def tar_sanitizer_filter(tarinfo: tarfile.TarInfo):
@@ -17,7 +20,12 @@ def tar_sanitizer_filter(tarinfo: tarfile.TarInfo):
def compress_tree_tarball(
src_dir, output_name, filter=tar_sanitizer_filter, hs_window=13, hs_lookahead=6
src_dir,
output_name,
filter=tar_sanitizer_filter,
hs_window=13,
hs_lookahead=6,
gz_level=9,
):
plain_tar = io.BytesIO()
with tarfile.open(
@@ -27,15 +35,21 @@ def compress_tree_tarball(
) as tarball:
tarball.add(src_dir, arcname="", filter=filter)
plain_tar.seek(0)
src_data = plain_tar.read()
compressed = heatshrink2.compress(
src_data, window_sz2=hs_window, lookahead_sz2=hs_lookahead
)
header = HeatshrinkDataStreamHeader(hs_window, hs_lookahead)
if output_name.endswith(TAR_HEATSHRINK_EXTENSION):
compressed = heatshrink2.compress(
src_data, window_sz2=hs_window, lookahead_sz2=hs_lookahead
)
header = HeatshrinkDataStreamHeader(hs_window, hs_lookahead)
compressed = header.pack() + compressed
elif output_name.endswith(TAR_GZIP_EXTENSION):
compressed = gzip.compress(src_data, compresslevel=gz_level, mtime=0)
else:
compressed = src_data
with open(output_name, "wb") as f:
f.write(header.pack())
f.write(compressed)
return len(src_data), len(compressed)