2

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.

  • "**The PclZip library User Guide is quite hard to understand**" so why are you using it instead of the php zip class ? http://php.net/manual/en/book.zip.php – Pedro Lobito Oct 09 '15 at 13:40
  • I tried the ZipArchive class at first, but it didnt perform that well. When the zip-file got big (>400Mb) it crashed. PclZip works but has this thing with the encoding. – Marcus Nyberg Oct 09 '15 at 13:43
  • 1
    @MarcusNyberg What do you mean it crashes? PHP doesn't simply crash without any errors. The root cause is very likely a bug in the pclzip library, I see a potential fix but it could cause other issues as well. So I really recommend using a library that is being supported, not one that is 9 years old and remains to be updated. And can you show me examples of *how* the file names are "messed up"? I see an issue that strings containing the `ÅÄÖ` get shortened and for me unknown effects if one of the characters are at the end of the file name. – Xorifelse Apr 09 '18 at 10:08

2 Answers2

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));
}
0

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);

Anton
  • 1
  • 2