开发者

PHP + ZipArchive + Russian language

i have the followed code

  $zip = new ZipArchive;
  $res = $zip->open('tmp/articles.zip');
  if ($res === TRUE) {
    $zip->extractTo('tmp/');
    $zip->close();
  }

It works fine for archives wi开发者_开发技巧th english filenames, but if i made archieve with russian characters, i have unreadable filenames. What should I do?

UPD: It doesn't work correctly when i use "unzip" from bash too.


Look at this code:

$z = new ZipArchive();
$res = $z->open('C:\Temp\Temp.zip');
if ($res)
{
    $z->extractTo('C:\Temp\Temp');
    $z->close();
}

$files = scandir('C:\Temp\Temp');
foreach ($files as $filename)
{
    print iconv('cp866', 'utf-8', $filename).PHP_EOL;
}

This code prints normal filenames.
So, WinRar uses old ms-dos charset 'cp866' for Cyrillic.
Hope you can change this code to create rename algorithm :)


This method not always works. I've found myself installing an external library like 7zip to do this job and it soves the problem. It's hard to know the encoding charset used for a file in windows. In my case I had to do this for avoiding errors in filenames after extraction. Here are my variables:

  // Extract file.
  $tmp_dir = uniqid();
  $zip_uri = "test.zip";
  $destination_dir = "C:\\Users\\user\\AppData\\Local\\Temp\\$tmp_dir";

This is the ZipArchive method:

$zip = new ZipArchive();
$zip->open($zip_uri);
if (!$zip->extractTo($destination_dir)) {
    die("Error extracting files.");
}
$zip->close();

This method not always worked, but the following does work beautifully (using 7Zip):

exec("C:\\7zip\\7za.exe e $zip_uri -o$destination_dir");

Hope it helps somebody to not spend hours trying with to figure out the encoding charset of a particular zipped file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜