Question

Do you have any idea how to improve the performance by reading CSV file from a zip file?

Firstly it open the zip file, then put the data into a memory and then read it by 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) {
  ....
}
Was it helpful?

Solution

Quick check with php manual showed that this should work:

<?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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top