Skip to content

Commit b0fae31

Browse files
committed
feat(handler): add flush method to decompression objects
1 parent cdadf84 commit b0fae31

File tree

1 file changed

+36
-6
lines changed
  • python/unblob/handlers/compression

1 file changed

+36
-6
lines changed

python/unblob/handlers/compression/uzip.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import lzma
12
import re
23
import zlib
3-
from lzma import LZMADecompressor
44
from pathlib import Path
5-
from typing import Optional
5+
from typing import Callable, Optional
66

7-
from pyzstd import EndlessZstdDecompressor
7+
import pyzstd
88

99
from unblob.file_utils import (
1010
Endian,
@@ -39,10 +39,39 @@
3939
LZMA_COMPRESSION = "#!/bin/sh\x0a#L3.0\x0a"
4040
ZSTD_COMPRESSION = "#!/bin/sh\x0a#Z4.0\x20"
4141

42-
DECOMPRESS_METHOD = {
43-
ZLIB_COMPRESSION.encode(): zlib.decompressobj,
42+
43+
class Decompressor:
44+
DECOMPRESSOR: Callable
45+
46+
def __init__(self):
47+
self._decompressor = self.DECOMPRESSOR()
48+
49+
def decompress(self, data: bytes) -> bytes:
50+
return self._decompressor.decompress(data)
51+
52+
def flush(self) -> bytes:
53+
return b""
54+
55+
56+
class LZMADecompressor(Decompressor):
57+
DECOMPRESSOR = lzma.LZMADecompressor
58+
59+
60+
class ZLIBDecompressor(Decompressor):
61+
DECOMPRESSOR = zlib.decompressobj
62+
63+
def flush(self) -> bytes:
64+
return self._decompressor.flush()
65+
66+
67+
class ZSTDDecompressor(Decompressor):
68+
DECOMPRESSOR = pyzstd.EndlessZstdDecompressor
69+
70+
71+
DECOMPRESS_METHOD: dict[bytes, type[Decompressor]] = {
72+
ZLIB_COMPRESSION.encode(): ZLIBDecompressor,
4473
LZMA_COMPRESSION.encode(): LZMADecompressor,
45-
ZSTD_COMPRESSION.encode(): EndlessZstdDecompressor,
74+
ZSTD_COMPRESSION.encode(): ZSTDDecompressor,
4675
}
4776

4877

@@ -67,6 +96,7 @@ def extract(self, inpath: Path, outdir: Path):
6796
decompressor = decompressor_cls()
6897
for chunk in iterate_file(infile, current_offset, compressed_len):
6998
outfile.write(decompressor.decompress(chunk))
99+
outfile.write(decompressor.flush())
70100
return ExtractResult(reports=fs.problems)
71101

72102

0 commit comments

Comments
 (0)