瑞典字符(ÅÄÖ)在PclZip压缩文件中变得混乱


Swedish characters (ÅÄÖ) gets messed up in PclZip zip files

我正在使用"PhpConcept库- Zip模块2.8.2" (http://www.phpconcept.net/pclzip/),也称为pclzip来创建Zip文件。我在Windows 8.1上运行XAMPP。

我能够在内容方面创建一个ok的zip文件。但是,带有瑞典字符的文件和文件夹名(åäö)会在zip-file中弄乱。

用法(压缩文件夹):

require_once('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if ($archive->add('filestobezipped/') == 0) {
    die('Error : '.$archive->errorInfo(true));
}

我猜有一些字符编码问题。但是这个问题应该如何解决呢?PclZip库用户指南很难理解。zip格式使用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));
}

Utf-8应该包含所有的甜字符。iso8859-1则没有。因此,您可以使用string utf8_decode ( string $data )只是启动解码zip文件名:)。

你可以使用$archiveNameDecoded = utf8_decode('archivename.zip'); $archive = new PclZip(archiveNameDecoded);