Peter
Peter

Reputation: 173

How to decompress tar.gz file with low memory usage [php 7]?

I have a 4 gb tar.gz file i'd like to decompress into a tar file. I've been using

$p = new PharData('/path/to/file/name');
$p->decompress();

to try and do so. However I get an error 'PHP Fatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 36864 bytes)'

Things I've tried:

(1) ini_set('memory_limit', '-1'); Result: Generic 'Killed' error message

(2) ini_set('memory_limit', '6000M'); Result: 'PHP Fatal error: Allowed memory size of 8589934592 bytes exhausted (tried to allocate 36864 bytes)' error message

I've also gone into the php.ini file to make the same changes. How can I decompress the tar.gz file into a tar file?

Upvotes: 0

Views: 660

Answers (1)

Sammitch
Sammitch

Reputation: 32272

Apparently the phar:// stream wrapper will allow you to read the CSV file contents directly out of the gzipped TAR.

$fh = fopen('phar://example.tar.gz/target_file.csv', 'r');

while( $row = fgetcsv($fh) ) {
    // code!
}

This should leverage PHP's stream goodness so that reading from the file doesn't require more than a few kilobytes actually reside in memory at a given time.

That said, if you start just appending rows to an array in that loop you're going to have the same memory problems. Do something with the row, and then discard it.

Upvotes: 0

Related Questions