Need PHP script to decompress and loop through zipped file

前端 未结 2 1983
情话喂你
情话喂你 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:36

    If you have PHP 5 >= 5.2.0, PECL zip >= 1.5.0 then you may use the ZipArchive libraries:

    $zip = new ZipArchive; 
    if ($zip->open('source.zip') === TRUE) 
    { 
         for($i = 0; $i < $zip->numFiles; $i++) 
         {   
            $fp = $zip->getStream($zip->getNameIndex($i));
            if(!$fp) exit("failed\n");
            while (!feof($fp)) {
                $contents = fread($fp, 8192);
                // do some stuff
            }
            fclose($fp);
         }
    } 
    else 
    { 
         echo 'Error reading zip-archive!'; 
    } 
    

提交回复
热议问题