Added lz77 command to allow for decompressing LZ77-compressed files

This commit is contained in:
2025-01-08 20:09:34 -05:00
parent a6388834e0
commit 9a89f80247
3 changed files with 60 additions and 2 deletions

28
commands/archive/lz77.py Normal file
View File

@@ -0,0 +1,28 @@
# "commands/archive/lz77.py" from WiiPy by NinjaCheetah
# https://github.com/NinjaCheetah/WiiPy
import pathlib
import libWiiPy
from modules.core import fatal_error
def handle_lz77_compress(args):
print("Compression is not implemented yet.")
def handle_lz77_decompress(args):
input_path = pathlib.Path(args.input)
if args.output is not None:
output_path = pathlib.Path(args.output)
else:
output_path = pathlib.Path(input_path.name + ".out")
if not input_path.exists():
fatal_error(f"The specified file \"{input_path}\" does not exist!")
lz77_data = input_path.read_bytes()
data = libWiiPy.archive.decompress_lz77(lz77_data)
output_path.write_bytes(data)
print("LZ77 file decompressed!")

View File

@@ -26,8 +26,15 @@ def handle_u8_unpack(args):
if not input_path.exists():
fatal_error(f"The specified input file \"{input_path}\" does not exist!")
u8_data = input_path.read_bytes()
# U8 archives are sometimes compressed. In the event that the provided data is LZ77 data, assume it's a compressed
# U8 archive and decompress it before continuing. Standard checks will then catch it if it was something else.
if u8_data[0:4] == b'LZ77':
u8_data = libWiiPy.archive.decompress_lz77(u8_data)
# Output path is deliberately not checked in any way because libWiiPy already has those checks, and it's easier
# and cleaner to only have one component doing all the checks.
libWiiPy.archive.extract_u8(input_path.read_bytes(), str(output_path))
libWiiPy.archive.extract_u8(u8_data, str(output_path))
print("U8 archive unpacked!")