zip文件中文命名编码问题

201 阅读1分钟

参考链接: pkware.cachefly.net/webdocs/APP… zip格式说明文档

在用命令或者程序生成zip包时,有时候会遇到乱码的问题。其原因是zip格式在最初设计的时候并未为zip文件设计预留指定编码的位置,系统识别zip文件时,一般会根据当前系统默认的编码进行识别显示。

整个zip包格式如下:


  Overall .ZIP file format:

    [local file header 1]
    [file data 1]
    [data descriptor 1]
    . 
    .
    .
    [local file header n]
    [file data n]
    [data descriptor n]
    [archive decryption header] (EFS)
    [archive extra data record] (EFS)
    [central directory]
    [zip64 end of central directory record]
    [zip64 end of central directory locator] 
    [end of central directory record]

其文件头格式如下:

local file header signature     4 bytes  (0x04034b50)
        version needed to extract       2 bytes
        general purpose bit flag        2 bytes
        compression method              2 bytes
        last mod file time              2 bytes
        last mod file date              2 bytes
        crc-32                          4 bytes
        compressed size                 4 bytes
        uncompressed size               4 bytes
        file name length                2 bytes
        extra field length              2 bytes

        file name (variable size)
        extra field (variable size)

可见,local file header 中有 file name length 指定需要识别的文件名的长度,但是并未明确设计出指定编码的位置。故如果生成 zip 时包名使用中文,生成使用的环境不一致,有可能导致 zip 包名产生乱码现象,取决于当前系统用来识别的默认编码。