php版本的文件夹合并
[php]
#!/bin/env php
<?php
error_reporting(7);
$dir1 = $argv[1];
$dir2 = $argv[2];
if(empty($dir1) || empty($dir2))
{
echo "php {$argv[0]} dir_merged dir_merge_save\n";
exit(0);
}
if(! is_dir($dir1))
{
echo "{$dir1} is not a dir\n";
exit(0);
}
if(! is_dir($dir2))
{
echo "{$dir2} is not a dir\n";
exit(0);
}
merge_dir($dir1, $dir2);
function merge_dir($from, $to)
{
$from = rtrim($from, "/")."/";
$to = rtrim($to, "/")."/";
echo "search file from {$from}";
$files = glob($from."*");
echo "\t\tdone\n";
foreach($files as $f)
{
$relative_path = substr_replace($f, "", 0, strlen($from));
$ff = $to . $relative_path;
//目录不存在则直接复制目录
if(is_dir($f))
{
if(!is_dir($ff))
{
$cmd = "cp -r ‘{$f}’ ‘{$ff}’";
`$cmd`;
logs($relative_path);
} else {
merge_dir($f, $ff);
}
}else{
if(!is_file($ff))
{
copy($f, $ff);
logs($relative_path);
continue;
}
//对特殊文件进行检查
$ext = substr($relative_path, strrpos($relative_path, ".")+1);
if($ext == "apk" && filesize($f) != filesize($ff))
{
$compare = apk_version_compare($f, $ff);
if($compare)
{
copy($f, $ff);
logs($relative_path);
}
}
}
}
}
function apk_version_compare($apk1, $apk2)
{
return aapt($apk1) > aapt($apk2);
}
function aapt($apk)
{
$apk = realpath($apk);
$aapt = "aapt d badging {$apk} | grep versionCode | awk ‘{print $3}’";
$aapt = `$aapt`;
return substr($aapt, 13, -2);
}
function logs($file)
{
printf("%s\n", $file);
}
[/php]