fix: кириллические имена файлов в ZIP — cp437→utf-8 decode
Python zipfile декодирует имена как CP437 если UTF-8 flag не установлен. Результат: ЛС → ╨¢╨í, regex не находит шифр → 0 обработано. Теперь re-encode cp437→utf-8 для каждого файла в архиве. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -115,9 +115,22 @@ def read_xlsx_xml(path: str, max_rows: int = 50) -> dict:
|
||||
|
||||
|
||||
def extract_zip(zip_path: str, dest_dir: str) -> list[str]:
|
||||
"""Extract zip and return list of xlsx files."""
|
||||
"""Extract zip and return list of xlsx files.
|
||||
|
||||
Handles Cyrillic filenames: Python's zipfile decodes names as CP437
|
||||
when the UTF-8 flag is not set. We detect and fix this.
|
||||
"""
|
||||
with zipfile.ZipFile(zip_path) as z:
|
||||
z.extractall(dest_dir)
|
||||
for info in z.infolist():
|
||||
# Fix Cyrillic filenames: if flag bit 11 is not set,
|
||||
# Python decodes as CP437 — re-encode and decode as UTF-8
|
||||
if not (info.flag_bits & 0x800):
|
||||
try:
|
||||
fixed = info.filename.encode('cp437').decode('utf-8')
|
||||
info.filename = fixed
|
||||
except (UnicodeDecodeError, UnicodeEncodeError):
|
||||
pass # keep original if re-decode fails
|
||||
z.extract(info, dest_dir)
|
||||
files = []
|
||||
for f in Path(dest_dir).rglob('*.xlsx'):
|
||||
if not f.name.startswith('~$'):
|
||||
|
||||
Reference in New Issue
Block a user