Need PHP script to decompress and loop through zipped file

前端 未结 2 1992
情话喂你
情话喂你 2020-12-19 23:57

I am using a fairly straight-forward script to open and parse several xml files that are gzipped. I also need to do the same basic operation with a ZIP file. It seems like

2条回答
  •  被撕碎了的回忆
    2020-12-20 00:35

    There is a smart way to do it with ZipArchive. You can use a for loop with ZipArchive::statIndex() to get all the information you need. You can access the files by their index (ZipArchive::getFromIndex()) or name (ZipArchive::getFromName()).

    For example:

    function processZip(string $zipFile): bool
    {
        $zip = new ZipArchive();
        if ($zip->open($zipFile) !== true) {
            echo '

    Can\'t open zip archive!

    '; return false; } // As long as statIndex() does not return false keep iterating for ($idx = 0; $zipFile = $zip->statIndex($idx); $idx++) { $directory = \dirname($zipFile['name']); if (!\is_dir($zipFile['name'])) { // file contents $contents = $zip->getFromIndex($idx); } } $zip->close(); }

提交回复
热议问题