質問するログイン新規登録

回答編集履歴

1

比較検証しました

2025/07/28 08:05

投稿

FKM
FKM

スコア3668

answer CHANGED
@@ -4,15 +4,23 @@
4
4
  ```py
5
5
  import zipfile
6
6
 
7
+ #ファイル名は文字化けする
7
8
  with zipfile.ZipFile('zip_test.zip') as zipf:
8
9
  for zinfo in zipf.infolist(): # ZipInfoオブジェクトを取得
9
10
  #if not zinfo.flag_bits & 0x800: # flag_bitsプロパティで文字コードを取得
10
11
  #zinfo.filename = zinfo.filename.encode('cp437').decode('cp932')
11
12
  zipf.extract(zinfo, 'unzip')
13
+
14
+ #ファイル名は文字化けしない
15
+ with zipfile.ZipFile('zip_test.zip') as zipf:
16
+ for zinfo in zipf.infolist(): # ZipInfoオブジェクトを取得
17
+ if not zinfo.flag_bits & 0x800: # flag_bitsプロパティで文字コードを取得
18
+ zinfo.filename = zinfo.filename.encode('cp437').decode('cp932')
19
+ zipf.extract(zinfo, 'unzip')
12
20
  ```
13
21
 
14
22
  コメントアウトしない状態だと文字化けされてファイル保存され、コメントアウトを外しエンコード処理を施した後ではきちんと日本語ファイル名で表示されます。
15
-
23
+ 色々なページで現象の指摘がありましたが、一例として。
16
24
  https://www.shibutan-bloomers.com/python_libraly_zip_shutil/1402/
17
25
 
18
- この現象は圧縮でも展開でも同様に起きました。
26
+ ちなみに、この現象は圧縮でも展開でもzipfileのファイル名生成に関わる部分であるため、同様に起きました。