Skip to content

Instantly share code, notes, and snippets.

@gh640
Created August 2, 2020 05:04
Show Gist options
  • Select an option

  • Save gh640/44f10b8defaad130c4f074e8dcc1ef96 to your computer and use it in GitHub Desktop.

Select an option

Save gh640/44f10b8defaad130c4f074e8dcc1ef96 to your computer and use it in GitHub Desktop.
サンプルコード: Windows 等の SJIS 環境で作られた zip ファイルを文字化け無しで展開するサンプル
# For more information, see https://www.lifewithpython.com/2020/08/python-sjis-zip.html .
import zipfile
from pathlib import Path
from typing import List
SRC_ZIP = '日本語のファイル名を含む.zip'
DEST_DIR = 'out'
def main():
extract_sjis_zip(SRC_ZIP, DEST_DIR)
# print(list_sjis_zip(SRC_ZIP))
def extract_sjis_zip(src_zip: str, dest: str) -> None:
"""SJIS の zip ファイルをファイル名の文字化けを避けて展開する"""
with zipfile.ZipFile(src_zip) as zfile:
for info in zfile.infolist():
_rename(info)
zfile.extract(info, DEST_DIR)
def list_sjis_zip(src_zip: str) -> List[zipfile.ZipInfo]:
"""SJIS の zip ファイルの中身をファイル名の文字化けを避けてリストアップする"""
info_all = []
with zipfile.ZipFile(src_zip) as zfile:
for info in zfile.infolist():
_rename(info)
info_all.append(info)
return info_all
def _rename(info: zipfile.ZipInfo) -> None:
"""ヘルパー: `ZipInfo` の中身をデコードし直す"""
LANG_ENC_FLAG = 0x800
encoding = 'utf-8' if info.flag_bits & LANG_ENC_FLAG else 'cp437'
info.filename = info.filename.encode(encoding).decode('cp932')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment