LoginSignup
11
2

More than 1 year has passed since last update.

macOSでunzipしようとしたら日本語が化けてエラーになる場合の対処法

Posted at

macOSでunzipしようとしたら Illegal byte sequence というエラーになる場合の対処法です。

$ unzip foo.zip
Archive: foo.zip
checkdir error:  cannot create foo������
                 Illegal byte sequence
                 unable to process foo������/bar.txt
...

結論としては brew で unzip をインストールし、それを使って unzip -O CP932 foo.zip のように文字コードを指定して実行してやればOKです。

詳細

まずお使いの unzip コマンドを確認しましょう。

$ which unzip
/usr/bin/unzip
$ unzip -h
UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.
...

/usr/bin/unzip (by Info-ZIP と表示されるバージョン6.00) を使っている場合は Shift_JIS 環境で作成された日本語ファイル名を持つZipアーカイブをうまく展開できないようです。

homebrew にはこの問題に対するパッチが適用されたバージョンの unzip がありますので、 brew install unzip などでインストールしましょう。 (homebrew 自体の導入方法は Homebrew 公式 を参照してください)

$ brew install unzip
==> Fetching unzip
==> Downloading https://ghcr.io/v2/homebrew/core/unzip/manifests/6.0_8
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/unzip/blobs/sha256:9f7f6a03fa3a
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring unzip--6.0_8.arm64_ventura.bottle.tar.gz
==> Caveats
unzip is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have unzip first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/unzip/bin:$PATH"' >> ~/.zshrc

==> Summary
🍺  /opt/homebrew/Cellar/unzip/6.0_8: 16 files, 648KB
==> Running `brew cleanup unzip`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

注意書きにあるようにデフォルトではPATHの通ったところにはインストールされないので、必要に応じて echo 'export PATH="/opt/homebrew/opt/unzip/bin:$PATH"' >> ~/.zshrc などを実行しましょう。
その後 source ~/.zshrc; rehash するとPATHが正しく設定されます。
(設定の書式や追加するファイルは使っている対話シェルによって異なりますので注意してください)

$ which unzip
/opt/homebrew/opt/unzip/bin/unzip
$ unzip -h
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

...
  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives
  -I CHARSET  specify a character encoding for UNIX and other archives

Debian プロジェクトがメンテしているバージョンの unzip を導入できました。 -O というオプションがあるのがわかります。

$ unzip -O CP932 foo.zip
Archive: foo.zip
   creating: foo日本語漢字/
  inflating: foo日本語漢字/bar.txt
...

CP932 の部分は Shift_JISsjis などでも通ります。(違いは調査していません)

ちなみに展開後のファイル名が文字化け状態になりますが、 -O CP932 を指定しなくてもエラーは発生しなくなりますので、 CP932 とか忘れてしまっても最悪展開だけはできます。

以上です。

11
2
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
11
2