S
S
Sergey Kornienko2018-07-13 20:51:03
PHP
Sergey Kornienko, 2018-07-13 20:51:03

PHP How to encode filenames with Cyrillic when extracting from a zip archive?

Guys, help me, for 2 days I've been trying to recode file names when extracting from a zip archive.
here is the code itself:

$zip = new ZipArchive();
$state = $zip->open('test.zip');
if ($state === true) {
    for ($i = 0; $i < $zip->numFiles; ++$i) {
        //skip if folder
        $stats = $zip->statIndex($i);
        if ($stats['size'] === 0 && $stats['crc'] === 0) {
            continue;
        }
        $name = $zip->getNameIndex($i);
        $content = $zip->getFromIndex($i);
    }
    $zip->close();
}
unset($zip);

I always get some mangled names. type:
䫬π0¼Ñ¡Γᵿ∩ ¬ ​​Γ«αúá¼ Çαßᬿ ŽÑσá¡δ

Converting from and to cp1251, cp866, IBM437 and others did not help, or I did not find the right combination :(
here is a test archive that I tried to work with: Archive
on my WIn home machine: PHP 7.1.7 and
LC_COLLATE=C;LC_CTYPE=Russian_Russia.1251;LC_MONETARY=C;LC_NUMERIC=C;LC_TIME=C

on the server I have Debian 9 PHP 7.1.18 and
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION= C

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Kornienko, 2018-07-13
@DieZeeL

Partially managed to get the 1st result with such a perversion:

$name = iconv('UTF-8', 'cp1252//IGNORE', $zip->getNameIndex($i));
$name = iconv('cp1252', 'cp850//IGNORE', $name);
$name = iconv('cp866','UTF-8//IGNORE',$name);

but it turns out here is such a canoe:
Documentation to the ogs of Asaki Plean/Ak deekatsii.pdf

Although it should be:
Documentation for the auction Arsaki Plekhany/Defective act.pdf

PS By the method of scientific poke I picked up encodings.
$name = iconv('UTF-8', 'cp437//IGNORE', $zip->getNameIndex($i));
        $name = iconv('cp437', 'cp865//IGNORE', $name);
        $name = iconv('cp866','UTF-8//IGNORE',$name);

Maybe someone will come in handy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question