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?
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.