windows下尽管文件类型是UTF-8格式
头部也设置了header(‘Content-Type: text/html; charset=UTF-8’);
在生成zip文件时候,设置的zip注脚时候总是乱码,代码如下
header('Content-Type: text/html; charset=UTF-8');
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('test.txt', '你好你好');
$zip->setArchiveComment('你好');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
生成后,压缩包内的文件跟内容都正常,就是注释乱码
一开始一直尝试把文字转码,但不管转成什么码都是乱码,最后去官方文档看说是Zip archives are encoded in ISO-8859-1 when stored but comments seem to be added in UTF-8 everytime. So…
只能先用非中文字符来代替,生成zip,后用字符替换的方式换成对应的文字就好了。代码如下:
header('Content-Type: text/html; charset=UTF-8');
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('test.txt', '你好你好');
$oldstr='+++----+++';
$zip->setArchiveComment($oldstr);
$zip->close();
$newstr='你好我是中文字';
$file = file_get_contents('test.zip');
file_put_contents('test.zip', str_replace($oldstr, $newstr, $file));
echo 'ok';
} else {
echo 'failed';
}