简体   繁体   中英

Improve performance for reading CSV file from ZIP?

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) {
  ....
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM