Php Zip Manipulation

I have a zip file. I need an easy way to read the file name from zip and read the contents of one of the files.

Can this be done directly in memory without saving, opening and reading files?

+3
source share
1 answer

You need to open the archive, and then 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 contents 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 do this is to use the zip: // stream wrapper:

$file = fopen('zip://' . dirname(__FILE__) . '/test.zip#test', 'r');
...
+3
source

Source: https://habr.com/ru/post/1765060/


All Articles