Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php unzipping huge files

Tags:

file

php

unzip

I'm trying to unzip a huge file (400+M compressed well over 4G unzipped) using php zip archive. I'm only unzipping one csv file in the zipped file.. the file I am interested in, unzipped, is over 4G.. I get as far as 3 records from the end of the file and the process takes off into lala land.. I mean the process simply goes on.. no output.. no errors no looping it just goes on... I haven't a clue as to what its doing.. my code is simple:

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) 
{
   $num = $zip->numFiles;
   for($i = 0; $i < $zip->numFiles; $i++) 
   {
      $filename = $zip->getNameIndex($i);
  // if its the file I want then...
      $content = '';
  // my output file  .. yes I've already checked to make sure the dir exists
      $unzipped = fopen($dir ."/2" . $filename  , 'wb');         
      $fp = $zip->getStream($filename);
      if(!$fp) exit("failed\n");
      while (!feof($fp))
      {
    $chunkSize = 10240;
    $contents = fread($fp, $chunkSize);
        $fwrite = fwrite($unzipped, $contents);
      }
      fclose($fp);
      fclose($unzipped);
    }

    $zip->close();
    fclose($filename);

}  

I've removed the write statements that go to another file to track progress. I get most of the file out.. (as I said 3 records short of the entire file)... but the process seems to go off somewhere.. it gets happens on the fread I just can't figure out what is happening.. It hasn't reached the eof.. the source is intact (checked with is_source($fp) just before the fread.. it throws no errors.. closing the browser doesn't stop it.. can't even stop apache.. have to shut down to end it...

any ideas??

thanks

like image 943
Bridget Avatar asked Dec 07 '25 10:12

Bridget


1 Answers

sounds like a bug to be honest(in php).

you might want to try outputting calls to memory_get_usage() to help you debug. But, also, see stream_copy_to_stream() because you can get rid of all that looping cruft. It might also be intresteting to keep a running total of bytes written to see if the number where things go awry look suspicious.

like image 139
goat Avatar answered Dec 09 '25 00:12

goat