Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error While Renaming An Extracted Zip File To Other Languages In PHP

I use PHP ZipArchive class to extract a .zip file, It works fine for English, but cause problems in my local language (THAI).

I use icov('utf-8','windows-874',$zip->getNameIndex($i)) to convert utf-8 to THAI. It works for folder's/file's name, but doesn't work for extracted .zip file and cause this error :

iconv(): Detected an illegal character in input string

Can anyone please tell me what the problem is here?

My PHP code

$file = iconv('utf-8', 'windows-874', $_GET['File']);
$path = iconv('utf-8', 'windows-874', $_GET['Path']);

$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
    // convert to Thai language
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        //echo iconv("charset zip file", "windows-874", $name);
        //$zip->extractTo($path,$name); -> this problem
    }
    $zip->close();
    echo json_encode('unZip!!!');
} else {
    echo json_encode('Failed');
}

After I extract the zipped file, The file's name is not the one I set for it. After I extract the zipped file, The file's name is not the one I set for it.

This is name i try to set : This is name i try to set :

Here is my zipped file :

https://www.dropbox.com/s/9f4j04lkvsyuy63/test.zip?dl=0

UPDATE
I tried unzipping the file in windows XP, it works fine there but not in windows 7.

like image 447
Veerapat Boonvanich Avatar asked Jul 05 '15 14:07

Veerapat Boonvanich


Video Answer


1 Answers

You probably should try mb_detect_encoding() for help with this - see the code below. You may need to expand on this code if you also have a problem with its path. Just use a loop if you need to do that.

$file = iconv('utf-8', 'windows-874', $_GET['File']);
$path = iconv('utf-8', 'windows-874', $_GET['Path']);

$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
    // convert to Thai language
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        $order = mb_detect_order();
        $encoding = mb_detect_encoding($name, $order, true);
        if (FALSE === $encoding) {
             throw new UnexpectedValueException(
                sprintf(
                    'Unable to detect input encoding with mb_detect_encoding, order was: %s'
                , print_r($order, true)
                )
             );
        } else {
            $encoding = mb_detect_encoding($name);
            $stringUtf8 = iconv($encoding, 'UTF-8//IGNORE', $name);
            $zip->extractTo($path,$stringUtf8);
        }  
    }
    $zip->close();
    echo json_encode('unZip!!!');
} else {
    echo json_encode('Failed');
}
like image 163
Tech Savant Avatar answered Sep 17 '22 14:09

Tech Savant