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.
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.
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With