Skip to content

Instantly share code, notes, and snippets.

@fredrike
Created March 4, 2014 13:46
Show Gist options
  • Save fredrike/9346756 to your computer and use it in GitHub Desktop.
Save fredrike/9346756 to your computer and use it in GitHub Desktop.
php-phar vs. shell tar
<?
addToArchive();
function addToArchive() {
$path = '/home/fer/SocialCrawler/';
$archiveDir = '/tmp/phar/';
$files = glob($path."*.json", GLOB_NOSORT);
//Create array of archives to compress to.
$archives = array();
foreach(array_map("stripPath", $files) as $row) {
$key = strstr($row,'_',true);
if(!array_key_exists($key, $archives))
$archives[$key] = array();
$archives[$key][] = $row;
}
foreach ($archives as $archive => $list) {
$archive = $archiveDir.$archive;
print "Creating $archive.tar".PHP_EOL;
get_execution_time(true);
$myPhar = new PharData($archive.'.tar',0);
$myPhar->startBuffering();
foreach($list as $file)
$myPhar->addFile($path.$file, $file);
$myPhar->stopBuffering();
$myPhar = null;
print count($list) . " files added in: ". get_execution_time(true) ."sec".PHP_EOL;
}
}
function stripPath($item) {
return substr(strrchr("/".$item, "/"), 1);
}
/**
* get execution time in seconds at current point of call in seconds
* @return float Execution time at this point of call
*/
function get_execution_time($delta = false) {
static $microtime_start = null;
static $microtime_delta = null;
if($microtime_start === null) {
$microtime_start = microtime(true);
$microtime_delta = $microtime_start;
return 0.0;
}
if($delta) {
$delta = microtime(true) - $microtime_delta;
$microtime_delta = microtime(true);
return $delta;
}
$microtime_delta = microtime(true);
return microtime(true) - $microtime_start;
}
?>
<?
addToArchive();
function addToArchive() {
$path = '/home/fer/SocialCrawler/';
$archiveDir = '/tmp/phar/';
$files = glob($path."*.json", GLOB_NOSORT);
//Create array of archives to compress to.
$archives = array();
foreach(array_map("stripPath", $files) as $row) {
$key = strstr($row,'_',true);
if(!array_key_exists($key, $archives))
$archives[$key] = array();
$archives[$key][] = $row;
}
foreach ($archives as $archive => $list) {
$archive = $archiveDir.$archive;
print "Creating $archive.tar".PHP_EOL;
get_execution_time(true);
$fname = implode(" ", $list);
$tarCmd = "tar ". (file_exists($archive.".tar") ? "-rf ":"-cf ") .$archive.".tar -C $path $fname";
exec($tarCmd." 2>&1", $result, $status);
print count($list) . " files added in: ". get_execution_time(true) ."sec".PHP_EOL;
}
}
function stripPath($item) {
return substr(strrchr("/".$item, "/"), 1);
}
/**
* get execution time in seconds at current point of call in seconds
* @return float Execution time at this point of call
*/
function get_execution_time($delta = false) {
static $microtime_start = null;
static $microtime_delta = null;
if($microtime_start === null) {
$microtime_start = microtime(true);
$microtime_delta = $microtime_start;
return 0.0;
}
if($delta) {
$delta = microtime(true) - $microtime_delta;
$microtime_delta = microtime(true);
return $delta;
}
$microtime_delta = microtime(true);
return microtime(true) - $microtime_start;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment