Skip to content

Instantly share code, notes, and snippets.

@JackNoordhuis
Created February 26, 2018 03:17
Show Gist options
  • Save JackNoordhuis/126f5fa823a533f975158ce4af9d526f to your computer and use it in GitHub Desktop.
Save JackNoordhuis/126f5fa823a533f975158ce4af9d526f to your computer and use it in GitHub Desktop.
PHP script to convert a phar archive to a zip
<?php
$opts = getopt("", ["file:"]);
if(!isset($opts["file"])) {
echo "Please specify a path with --path" . PHP_EOL;
exit(0);
}
$filename = rtrim($opts["file"], ".phar");
if(ini_get("phar.readonly") == 1){
echo "Set phar.readonly to 0 with -dphar.readonly=0" . PHP_EOL;
exit(1);
}
$folderPath = __DIR__ . DIRECTORY_SEPARATOR . "extracts";
@mkdir($folderPath);
$folderPath .= DIRECTORY_SEPARATOR . date("D_M_j-H.i.s-T_Y");
@mkdir($folderPath);
$pharPath = str_replace("\\", "/", rtrim(__DIR__ . DIRECTORY_SEPARATOR . $filename . ".phar", "\\/"));
$zipPath = __DIR__ . DIRECTORY_SEPARATOR . $filename . ".zip";
if(file_exists($zipPath)) {
unlink($zipPath);
}
$phar = new \Phar($pharPath);
$phar->convertToData(\Phar::ZIP);
copy($zipPath, $folderPath . DIRECTORY_SEPARATOR . $filename . ".zip");
unlink($zipPath);
echo "Extracted phar to " . $folderPath . PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment