Un-zipping file problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    Un-zipping file problem

    Hi all

    Below is a bit of code that opens up and reads a
    zip file.

    I'm having trouble working out how to read in line
    by line. The file I am unzipping is a CSV file and I
    need to return each line so I can work on them

    Any help would be great

    Brian

    $zip = zip_open("$file path"); // << full path of the file
    if ($zip) {
    while ($zip_entry = zip_read($zip)) {
    if (zip_entry_open ($zip, $zip_entry, "r")) {
    $buf = zip_entry_read( $zip_entry, zip_entry_files ize($zip_entry) );
    print "$buf<br>\n "; // << what to return each line so i can work
    on it
    zip_entry_close ($zip_entry);
    }
    echo "<br>\n";
    }
    zip_close($zip) ;
    }



  • Benjamin Esham

    #2
    Re: Un-zipping file problem

    Brian wrote:
    Below is a bit of code that opens up and reads a zip file.
    >
    I'm having trouble working out how to read in line by line. The file I am
    unzipping is a CSV file and I need to return each line so I can work on
    them
    >
    $buf = zip_entry_read( $zip_entry, zip_entry_files ize($zip_entry) );
    >
    print "$buf<br>\n "; // << what to return each line so i can work on it
    I'm not really familiar with the zip functions, but if $buf is the contents
    of your file, you can use

    $lines = preg_split("/[\r\n]+/", $buf);

    to populate $lines with the lines of your file.

    If $buf is just a temporary buffer, you could do something like

    $contents .= $buf;

    during each iteration of the loop, and the use preg_split() with $contents
    after the loop has completed.

    HTH,
    --
    Benjamin D. Esham
    bdesham@gmail.c om | AIM: bdesham128 | Jabber: same as e-mail
    "A witty saying proves nothing." — Voltaire

    Comment

    Working...