I need to read the content of a single file, "test.txt", inside of a zip file. The whole zip file is a very large file (2gb) and contains a lot of files (10,000,000), and as such extracting the whole thing is not a viable solution for me. How can I read a single file?
Try using the zip://
wrapper:
$handle = fopen('zip://test.zip#test.txt', 'r');
$result = '';
while (!feof($handle)) {
$result .= fread($handle, 8192);
}
fclose($handle);
echo $result;
You can use file_get_contents
too:
$result = file_get_contents('zip://test.zip#test.txt');
echo $result;
how to tell if the file exists? if(!@$test = file_get_contents(... ?
@user1243068: I'm not sure. That should work. You can use
is_file
to check if the.zip
exists. As for the file inside the zip, I'm not sure.It also works with file, like $content=file('zip://test.zip#test.txt');
I have
.tar.gz
file instead of.zip
file, how to use thefile_get_contents('zip://test.zip#test.txt');
?@Sami: The
phar://
wrapper seems to support this.file_get_contents('phar://yourfile.tar.gz/path/to/file.txt');
See: stackoverflow.com/a/4878849