Merge pull request #18838 from eileenmcnaughton/vrenew
[civicrm-core.git] / ext / afform / html / bin / add-zip-regex.php
1 #!/usr/bin/php
2 <?php
3
4 ## This script allows you to add to a ZIP file -- while filtering the filename.
5 ## For example, suppose you want to inject a prefix to put everything in a subdir;
6 ## match the start of the name (^) and put in the new sub dir:
7 ##
8 ## find -type f | add-zip-regex.php myfile.zip :^: 'the-new-sub-dir/'
9
10 if (PHP_SAPI !== 'cli') {
11 die("This tool can only be run from command line.");
12 }
13
14 if (empty($argv[1]) || empty($argv[2])) {
15 die(sprintf("usage: cat files.txt | %s <zipfile> <old-prefix> <new-prefix>\n", $argv[0]));
16 }
17
18 $zip = new ZipArchive();
19 $zip->open($argv[1], ZipArchive::CREATE);
20 $zip->addEmptyDir($argv[3]);
21
22 $files = explode("\n", file_get_contents('php://stdin'));
23 foreach ($files as $file) {
24 if (empty($file)) {
25 continue;
26 }
27 $file = preg_replace(':^\./:', '', $file);
28 $internalName = preg_replace($argv[2], $argv[3], $file);
29 if (file_exists($file) && is_dir($file)) {
30 $zip->addEmptyDir($internalName);
31 }
32 else {
33 $zip->addFile($file, $internalName);
34 }
35 }
36
37 $zip->close();