Correctly decoding zip entry file names — CP437, UTF-8 or?

≡放荡痞女 提交于 2019-11-29 13:47:14

The only way to determine if the filename is encoded as UTF-8 without using the EFS flag is to check to see if the high order bit is set in one of the characters. That could possibly mean that the character is UTF-8 encoded. However, it could still be the other way as there are some characters in CP437 that have the high order bit set and aren't meant to be decoded as UTF-8.

I would stick to the PKWARE app note specification and not hack in a solution that tries to conform to every known zip application in existence.

Nickolay Olshevsky

At the moment situation is as following:

  • most of Windows implementations use DOS (OEM) encoding
  • Mac OS zip utility uses utf-8, but it doesn't set utf-8 bit flags
  • *nix zip utilities silently uses system encoding

So the only way is to check if filename contains something like utf-8 characters (check description of utf8 encoding - first byte should be 110xxxxx, second - 10xxxxxx for 2-bytes encoded chars). If it is correct utf8 string - use utf8 encoding. If not - fall back to OEM/DOS encoding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!