0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python で zip 展開(日本語ファイル名対応)

Last updated at Posted at 2025-11-27

Python の zipfile モジュールを使うと、以下のような問題が出ることがあります。

  • 日本語ファイル名やフォルダ名が文字化けする
  • "フォルダ\ファイル.doc" というファイル名になる

このため、CP437 としてバイト列にして、CP932 に戻す方法を使ってきました。

ところが、これでは文字化けする zip ファイルが出てきました。

洪水浸水想定区域(1次メッシュ単位)データの2022年度だけです。ほかの年度は問題なし。

ポイントとなるコードは

info.filename = info.filename.encode('cp437').decode('cp932')

最初の encode を ["latin1", "cp437"] で、decode を ["cp932", "utf-8", "shift_jis", "euc_jp"] で試して、成功したものを採用するようにしました。

最終的なスクリプトはこちらになります。

作成に当たり、AI を活用しました。ChatGPT は解決できず、Gemini で解決しました。

macOS/Linux/Windows で検証しました。

国の提供する ZIP は、正しく作っていただきたいものです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?