Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php creating zip file for files with unicode names

Tags:

php

unicode

zip

In my php app I did the following:

$dn = sys_get_temp_dir() . '/' . uniqid('td', false);
if (mkdir($dn) === true)
{
    $fn = $dn . '/' . '関連事業調査.xls';
    $writer = new \PHPExcel_Writer_Excel5($wb);
    $writer->save($fn);

    $fzip = sys_get_temp_dir() . '/' . uniqid('td', false) . '.zip';
    $password = $this->container->getParameter('ZipPassword');
    $out = null; 
    $ret = null;
    exec("zip -qjP $password $fzip $dn" . '/*', &$out, &$ret);
}

In linux environment it correctly creates zip file with given password. I can unzip the files in Linux without any problem. But if I download it to Windows, and extract the files, file names become incorrect. How to solve this problem?

like image 925
synergetic Avatar asked Mar 14 '26 13:03

synergetic


1 Answers

ZIP files don't have a specified encoding for filenames*. Consequently any use of non-ASCII characters is completely unreliable.

*: Not completely true: there is an extension to the format that allows UTF-8 filenames to be used, and the zip command will use it. But Windows's ZIP interface (“Compressed Folders”) doesn't support it, and always uses the default (“ANSI”) code page to interpret the filename bytes. If you know that your target audience all have Windows boxes with a particular locale then you can target that locale... otherwise, best stick to ASCII.

like image 178
bobince Avatar answered Mar 17 '26 01:03

bobince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!