Created
November 26, 2020 13:08
-
-
Save onetdev/7397e536bc7e13a77bcbc37aaa7cab09 to your computer and use it in GitHub Desktop.
PHP ZipArchive merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Merge multiple zip files into one single. In case of colission the later | |
* one overwrites the previous one. | |
* @param string[] $sources paths | |
* @param string $target path | |
*/ | |
function zip_archive_merge(array $sources, $target) { | |
$zip = new ZipArchive; | |
$zip->open($target, ZipArchive::CREATE); | |
foreach ($sources as $source) { | |
$source_zip = new ZipArchive; | |
$res = $source_zip->open($source); | |
if ($res !== true) { continue; } | |
for ($i = 0; $i < $source_zip->numFiles; $i++) { | |
$stat = $source_zip->statIndex($i); | |
$zip->addFromString($stat['name'], $source_zip->getFromName($stat['name'])); | |
} | |
$source_zip->close(); | |
} | |
$zip->close(); | |
} | |
zip_archive_merge( | |
[__DIR__ . '/zip_a.zip', __DIR__ . '/zip_b.zip'], | |
__DIR__ . '/target.zip' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment