I have a zip file. I need a simple way to read the name of the files from the zip and read the contents of one of the files.
Can this be done directly in memory without saving,opening and reading the files ?
I have a zip file. I need a simple way to read the name of the files from the zip and read the contents of one of the files.
Can this be done directly in memory without saving,opening and reading the files ?
You need to open the archive and then can iterate over the files by index:
$zip = new ZipArchive();
if ($zip->open('archive.zip'))
{
for($i = 0; $i < $zip->numFiles; $i++)
{
echo 'Filename: ' . $zip->getNameIndex($i) . '<br />';
}
}
else
{
echo 'Error reading .zip!';
}
To read the content of a single file you can use ZipArchive::getStream($name).
$zip = new ZipArchive();
$zip->open("archive.zip");
$fstream = $zip->getStream("index.txt");
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents .= fread($fp, 2);
}
Another way to directly do it is using the zip:// stream wrapper:
$file = fopen('zip://' . dirname(__FILE__) . '/test.zip#test', 'r');
...