问题
在写boaPHP的zip驱动时遇到一个问题,用ZipArchive类的setArchiveComment()添加中文注释,在winrar和7zip里面注释乱码,使用getArchiveComment()查看正常,我使用的是UTF-8编码。
原因
排查了PHP的setArchiveComment函数源码,其实它只限定了注释字符数不超过65536,然后就转给了libzip,libzip添加注释时采用UTF-8/ASCII编码,所以在winrar里面会乱码。
解决
在没有添加注释时,文档注释长度为0,此时zip文档最后两个字节存储的是注释长度值。
<?php
$_charset = 'GBK'; // 根据需要转换编码
$file = 'D:/boaphp.zip';
$comment = '中文ABC123'; // 注释
$zip = new ZipArchive;
$res = $zip->open($file, ZipArchive::CREATE);
if ($res) {
// 添加压缩文件
if($_charset){ //适用 Winrar, 7z...
$zip->close(); // 注意:先关闭ZipArchive
$str = mb_convert_encoding($comment, $_charset, 'UTF-8'); // 将注释转换为本地编码
$fh = fopen($file, 'r+b'); // 二进制方式打开压缩包
fseek($fh, -2, SEEK_END); // 查找压缩包最后2个字节
$str = pack('v', strlen($str)) . $str; // 生成二进制格式的数据
fwrite($fh, $str); // 从找到位置开始写入
fclose($fh);
}else{ //适用 $zip->getArchiveComment()
$zip->setArchiveComment($comment);
$zip->close();
}
}
?>
参考
1.ZIP 文档格式