Question

I want to open in php a tar file of 45GB and about 5 millions files. I don't want to extract it. I just want to get the content of a single file(something about 10kb).

So far I have tried :

new PharData('tar file');

and

file_get_contents('phar://file.tar/single file'); 

I get this error:

Fatal error: Allowed memory size of 268435456 bytes exhausted

Is there a way to get the content of a single file from a large archive without reading the whole archive. Or something that takes less memory?

tar -xvf {tarball.tar} {path/to/file} is disabled by my host

Thanks

I found some answers where people were saying that this can not be done with tar. For tar you need to read the whole content in order to extract a single file.

My question is now, what format can you use in order to read a single file from a large archive without loading the entire archive.

Thank you

Was it helpful?

Solution

For tar you need to read the whole content in order to extract a single file

Not true. But compression adds complications when working with CLI tools. IIRC, the PEAR tar package can extract single files from gzipped tar archives without reading the whole file into memory.

Why do you need a 45Gb archive? Is it for deployment? If not then you really need to think about a different way of storing your data or switch providers.

OTHER TIPS

Try a zip with alias and absolute path.

$p=new PharData(dirname(__FILE__).'/pharao.zip', 0, 'alias.phar', Phar::GZ);
$p->buildFromDirectory('temp');

If the Phar exists you don't need to new it twice. Just call:

echo file_get_contents('phar://alias.phar/existing.file');

I think this is fast - faster than swapping a million files with the memory. But ...

file_put_contents('phar://alias.phar/new.file', 'Hello world');

... is very slow. So very much slow that it is better to collect new files as usual and add them later at once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top