fix script spacing and tabs below: import os import argparse import brotli import…
- User
fix script spacing and tabs below: import os import argparse import brotli import zipfile import io
def unpack_brotli_zip(archive_path, output_dir): """ Decompresses a Brotli-compressed file and extracts its contents as a ZIP archive.
Args: archive_path (str): The path to the Brotli-compressed ZIP file. output_dir (str): The path to the directory for extracted files. """ print(f"--- Processing archive: {archive_path} ---")
--- Stage 1: Decompress the file with Brotli ---
try: print(f"[*] Stage 1: Decompressing the entire file using Brotli...") with open(archive_path, 'rb') as f: compressed_data = f.read()
codedecompressed_data = brotli.decompress(compressed_data) print(f"[+] Brotli decompression successful. Decompressed size: {len(decompressed_data)} bytes.")except brotli.error as e: print(f"[!] ERROR: Brotli decompression failed: {e}") print("This file may not be a valid Brotli stream.") return except FileNotFoundError: print(f"[!] ERROR: The file '{archive_path}' was not found.") return except Exception as e: print(f"[!] An unexpected error occurred during decompression: {e}") return
--- Stage 2: Extract the decompressed data as a ZIP archive ---
try: print("\n[*] Stage 2: Treating decompressed data as a ZIP archive...")
code# Check for the ZIP signature 'PK\x03\x04' if not decompressed_data.startswith(b'\x50\x4b\x03\x04'): print("[!] WARNING: Decompressed data does not have the standard ZIP signature.") print("Extraction may fail or produce unexpected results.") # Use io.BytesIO to treat the in-memory bytes as a file zip_buffer = io.BytesIO(decompressed_data) with zipfile.ZipFile(zip_buffer, 'r') as zip_ref: file_list = zip_ref.namelist() print(f"[+] ZIP archive is valid. Found {len(file_list)} files to extract.") os.makedirs(output_dir, exist_ok=True) # Extract all files zip_ref.extractall(output_dir) print(f"\nSuccessfully extracted all files to: {output_dir}")except zipfile.BadZipFile: print("[!] ERROR: The decompressed data is not a valid ZIP file.") except Exception as e: print(f"[!] An unexpected error occurred during ZIP extraction: {e}") if name == 'main': parser = argparse.ArgumentParser( description="Unpacks custom archives that are Brotli-compressed ZIP files.", formatter_class=argparse.RawTextHelpFormatter, epilog="Example usage:\n python unpack_custom_archive.py unknown_file.halva2 ./extracted_files" )
- Assistant