提高从ZIP读取CSV文件的性能?

时间:2012-12-11 17:50:58

标签: php fgetcsv

您是否知道如何通过从zip文件中读取CSV文件来提高性能?

首先打开zip文件,然后将数据放入内存,然后通过fgetcsv

读取
$zip = new ZipArchive();
if ($zip->open($fileName)) {
    $info = $zip->statIndex(0);
    $fp = $zip->getStream($info['name']);
    if(!$fp) exit("failed\n");

    while (!feof($fp)) {
        $contents .= fread($fp, 2);
    }
        fclose($fp);
        $zip->close();
}

$temp = fopen("php://memory", "rw");
fwrite($temp, $contents);
fseek($temp, 0);

while (($data = fgetcsv($temp, 0)) !== false) {
  ....
}

1 个答案:

答案 0 :(得分:3)

使用php手册快速检查表明这应该有效:

<?php

    $fp = fopen('zip://test.zip#test', 'r'); // test  name of file in archive
    if (!$fp) {
        exit("cannot open\n");
    }
    while (($data = fgetcsv($fp, 0)) !== false) {
        ...
    }

    fclose($fp);