How to make ZipArchive not overwrite specific files and folders

I would like to extract the zip folder to a place and replace all the files and folders except a few, how can I do this?

I am currently doing the following.

$backup = realpath('./backup/backup.zip');

$zip = new ZipArchive();

if ($zip->open("$backup", ZIPARCHIVE::OVERWRITE) !== TRUE) {

    die ('Could not open archive');

}

$zip->extractTo('minus/');

$zip->close();

How can I set conditions for which files and folders should NOT be replaced? It would be great if you could use some kind of loop.

Thank you all for your help.

+3
source share
2 answers

You can do something like this, I checked it and it works for me:

// make a list of all the files in the archive
$entries = array();
for ($idx = 0; $idx < $zip->numFiles; $idx++) {
    $entries[] = $zip->getNameIndex($idx);
}

// remove $entries for the files you don't want to overwrite

// only extract the remaining $entries
$zip->extractTo('minus/', $entries);

numFiles getNameIndex, , ( /folder/subfolder/file.ext). , extractTo , , .

+7

( , ), ().

 $zip->extractTo('minus/', array('file1.ext', 'newfile2.xml'));

, , :

$files = array();

for($i = 0; $i < $zip->numFiles; $i++) {
    $filename = $zip->getNameIndex($i);
    // if $filename not in destination / or whatever the logic is then
        $files[] = $filename;
}
$zip->extractTo($path, $files);      
$zip->close();

$zip->getStream( $filename ) , .

0

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


All Articles