我正在使用“ PhpConcept库-Zip模块2.8.2”(http://www.phpconcept.net/pclzip/)(也称为pclzip)来创建一个zip文件。我在Windows 8.1上运行XAMPP。
我能够按内容创建一个好的zip文件。但是,带有瑞典语字符(åäö)的文件和文件夹名称在zip文件中被弄乱了。
用法(压缩文件夹):
require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if ($archive->add('filestobezipped/') == 0) {
die('Error : '.$archive->errorInfo(true));
}
我猜有一些字符编码问题。但是,应该如何解决呢?PclZip库用户指南很难理解。压缩格式使用CP437和UTF-8。我的PHP使用的是ISO8859-1。
好吧,我自己解决了这一问题,方法是添加一个回调函数“ myPreAddCallBack”,该函数在将每个文件添加到存档时运行。它将文件名转换为CP437。PCLZIP_CB_PRE_ADD参数表的文档:http ://www.phpconcept.net/pclzip/user-guide/50
require_once('pclzip.lib.php');
function myPreAddCallBack($p_event, &$p_header)
{
$encoding = mb_detect_encoding($p_header['stored_filename']);
$p_header['stored_filename'] = iconv($encoding,"CP437",$p_header['stored_filename']);
return 1;
}
$archive = new PclZip('archive.zip');
if ($archive->add('filestobezipped/',PCLZIP_CB_PRE_ADD, 'myPreAddCallBack') == 0) {
die('Error : '.$archive->errorInfo(true));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句