fix: zip filename decode — поддержка UTF-8 от macOS zip + cp866 от Windows
Найдено в E2E на dev-сервере: macOS `zip` пишет UTF-8 имена БЕЗ флага UTF-8 (0x800), и Linux Python зфайл интерпретирует raw байты как cp437. Старый код жёстко конвертировал cp437→cp866 — это ломало UTF-8 имена. Фикс: пробуем оба codec'а (utf-8 первым — macOS, потом cp866 — Windows), берём результат без U+FFFD replacement chars. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+12
-3
@@ -16,13 +16,22 @@ from pathlib import Path
|
||||
|
||||
def _decode_zip_name(info: zipfile.ZipInfo) -> str:
|
||||
"""Python 3 zipfile авто-декодирует cp437 если флаг UTF-8 (0x800) не задан.
|
||||
Для имён от Windows-архиваторов нужно re-encode→re-decode как cp866."""
|
||||
Реальные байты могут быть в UTF-8 (macOS `zip`) или cp866 (Windows-архиватор).
|
||||
Пробуем оба, берём результат без replacement chars."""
|
||||
if info.flag_bits & 0x800:
|
||||
return info.filename
|
||||
try:
|
||||
return info.filename.encode('cp437').decode('cp866')
|
||||
except UnicodeError:
|
||||
raw = info.filename.encode('cp437')
|
||||
except UnicodeEncodeError:
|
||||
return info.filename
|
||||
for codec in ('utf-8', 'cp866'):
|
||||
try:
|
||||
decoded = raw.decode(codec)
|
||||
if '�' not in decoded:
|
||||
return decoded
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
return info.filename
|
||||
|
||||
|
||||
def extract_archive(zip_path: Path, target_dir: Path) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user