Skip to content

Instantly share code, notes, and snippets.

@XCanG
Created December 6, 2021 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XCanG/b1ff0563468d5e137c525236021bc917 to your computer and use it in GitHub Desktop.
Save XCanG/b1ff0563468d5e137c525236021bc917 to your computer and use it in GitHub Desktop.
Extract ZIP archives what contain japanese file names, but have local encoding, therefore you get name like: В┴В┐В╚ВиР║ВЁПoВ╖Ч√ТВ╠ГЙГtГ@В┐ВсВёs1.png that you need convert. Have fallback to 7z (should be in PATH), but it will not fix names.
import sys
import os
import glob
import zipfile
import re
from colorama import init, Fore, Style
ext = re.compile(r"\.(zip|bz2|bzip2|xz|lzma|z)\Z", flags = re.I)
def extract(fn):
nd = os.path.splitext(fn)[0]
pwd = None
try:
with zipfile.ZipFile(fn, "r") as f:
zipInfo = f.infolist()
for member in zipInfo:
try:
_enc = member.filename.encode("cp437")
except UnicodeEncodeError:
# _enc = member.filename.encode("utf-8")
print(f"\n{Fore.YELLOW}UTF-8 encoding:{Style.RESET_ALL} {member.filename}")
else:
try:
member.filename = _enc.decode("sjis") # "shift_jis"
except UnicodeDecodeError:
try:
member.filename = _enc.decode("shift_jisx0213")
except UnicodeDecodeError:
try:
member.filename = _enc.decode("euc_jp")
except UnicodeDecodeError:
try:
member.filename = _enc.decode("iso2022_jp")
except UnicodeDecodeError:
try:
member.filename = _enc.decode("cp932")
except UnicodeDecodeError:
try:
member.filename = _enc.decode("iso2022_jp_2")
except UnicodeDecodeError:
print(f"\n{Fore.RED}↓ Encoding cannot match for:{Style.RESET_ALL} {member.filename}")
# On a rare occasion
# member.filename = _enc.decode("utf-8")
if not os.path.exists(os.path.join(nd, member.filename)):
print(f"{Fore.GREEN}Extracting:{Style.RESET_ALL} {nd}{os.sep}{member.filename}")
try:
f.extract(member, nd, pwd = pwd)
except RuntimeError as e:
pwd = input(f"{Fore.RED}Enter Password:{Style.RESET_ALL} ").encode()
f.extract(member, nd, pwd = pwd)
else:
print(f"{Fore.YELLOW}Skip existed:{Style.RESET_ALL} {nd}{os.sep}{member.filename}")
except NotImplementedError:
import subprocess
subprocess.Popen(["7z", "e", fn, f"-o{nd}", "-y"])
if __name__ == "__main__":
args = sys.argv[1:]
init()
for arg in args:
if os.path.isfile(arg):
print(f"{Fore.BLUE}Extracting archive:{Style.RESET_ALL} {arg}\n")
extract(arg)
print(Fore.BLUE + "▬" * 79 + Style.RESET_ALL + "\n")
elif arg == "**" or arg == "**/*" or arg == "**/*.zip" or arg == "**/*.*":
fs = filter(lambda x: bool(ext.search(x)), glob.iglob("**/*", recursive = True))
for f in fs:
print(f"{Fore.BLUE}Extracting archive:{Style.RESET_ALL} {f}\n")
extract(f)
print(Fore.BLUE + "▬" * 79 + Style.RESET_ALL + "\n")
print(Fore.BLUE + "▬" * 79 + Style.RESET_ALL + "\n")
elif arg == "*" or arg == "*.zip" or arg == "*.*":
fs = filter(lambda x: bool(ext.search(x)), glob.iglob("*"))
for f in fs:
print(f"{Fore.BLUE}Extracting archive:{Style.RESET_ALL} {f}\n")
extract(f)
print(Fore.BLUE + "▬" * 79 + Style.RESET_ALL + "\n")
print(Fore.BLUE + "▬" * 79 + Style.RESET_ALL + "\n")
else:
print(f"{Fore.RED}File not exist:{Style.RESET_ALL} {arg}\n")
os.system("pause")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment