简体   繁体   中英

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

I am using the "PhpConcept Library - Zip Module 2.8.2" ( http://www.phpconcept.net/pclzip/ ), also called pclzip to create a zip file. I am running XAMPP on Windows 8.1.

I am able to create an ok zip-file content-wise. However, file and foldernames with swedish characters (åäö) gets messed up inside the zip-file.

Usage (zipping a folder):

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

I guess there is some character encoding issues. But how should this be solved? The PclZip library User Guide is quite hard to understand. The zip-format uses CP437 and UTF-8. My php is using ISO8859-1.

Well, I solved this one myself by adding a callback function "myPreAddCallBack" that runs when each file is added to the archive. It converts the filenames to CP437. Documentation for the PCLZIP_CB_PRE_ADD paremeter: 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 should have all of the sweedish characters. The iso8859-1 doesen't. Therefor you can use string utf8_decode ( string $data ) just startup with decoding the zipfile name :).

you could use $archiveNameDecoded = utf8_decode('archivename.zip'); $archive = new PclZip(archiveNameDecoded); $archiveNameDecoded = utf8_decode('archivename.zip'); $archive = new PclZip(archiveNameDecoded);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM