You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.9 KiB
65 lines
1.9 KiB
<?php
|
|
// php -f view_phar_file_contents.php -- -nosize
|
|
$pharFile = 'neatoDeploy.phar';
|
|
|
|
function hasNoSizeFlag(): bool {
|
|
global $argv;
|
|
return in_array('-nosize', $argv) || in_array('--nosize', $argv);
|
|
}
|
|
|
|
function formatBytes(int $bytes, int $precision = 2): string {
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
$bytes = max($bytes, 0);
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
|
$pow = min($pow, count($units) - 1);
|
|
$bytes /= (1 << (10 * $pow)); // Calculation: 1024^$pow
|
|
return round($bytes, $precision) . $units[$pow];
|
|
}
|
|
|
|
try {
|
|
if (!is_readable($pharFile)) {
|
|
echo "Need to be root!";
|
|
exit(1);
|
|
}
|
|
// Try different archive types
|
|
$ext = pathinfo($pharFile, PATHINFO_EXTENSION);
|
|
|
|
switch (strtolower($ext)) {
|
|
case 'phar':
|
|
$archive = new Phar($pharFile, 0);
|
|
break;
|
|
case 'tar':
|
|
case 'zip':
|
|
$archive = new PharData($pharFile);
|
|
break;
|
|
case 'gz':
|
|
case 'tgz':
|
|
$archive = new PharData('compress.zlib://' . $pharFile);
|
|
break;
|
|
default:
|
|
throw new Exception("Unsupported archive type: $ext");
|
|
}
|
|
|
|
// Get the iterator for the Phar archive
|
|
$iterator = new RecursiveIteratorIterator($archive);
|
|
|
|
foreach ($iterator as $file) {
|
|
$path = $file->getPathname();
|
|
$parent = dirname($path);
|
|
$bp = basename($parent);
|
|
if ($bp == $pharFile) {
|
|
$first = "";
|
|
} else {
|
|
$first = $bp . '/';
|
|
}
|
|
$lastTwoParts = $first . basename($path);
|
|
if (hasNoSizeFlag()) {
|
|
echo $lastTwoParts . PHP_EOL;
|
|
} else {
|
|
$size = formatBytes($file->getSize()) . "\t ";
|
|
echo $size . $lastTwoParts . PHP_EOL;
|
|
}
|
|
}
|
|
} catch (PharException | Exception $e) {
|
|
echo "Error reading Phar archive: " . $e->getMessage();
|
|
}
|
|
|