php 压缩解压ZipArchive设置注脚(注释)setArchiveComment方法乱码

1 篇文章 0 订阅

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';
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值