新增解压包命令
This commit is contained in:
parent
46a41c86ea
commit
b4a411f343
@ -8,66 +8,142 @@ use think\console\Command;
|
|||||||
use think\console\Input;
|
use think\console\Input;
|
||||||
use think\console\input\Argument;
|
use think\console\input\Argument;
|
||||||
use think\console\input\Option;
|
use think\console\input\Option;
|
||||||
|
use think\console\input\Option as InputOption;
|
||||||
use think\console\Output;
|
use think\console\Output;
|
||||||
|
|
||||||
class CompressPackageToZipCommand extends Command
|
class CompressPackageCommand extends Command
|
||||||
{
|
{
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
// 指令配置
|
// 指令配置
|
||||||
$this->setName('package:zip')
|
$this->setName('package:zip')
|
||||||
->addArgument('package', Argument::REQUIRED, 'package name')
|
->addArgument('package', Argument::REQUIRED, 'package name')
|
||||||
|
->addOption('compress', 'c', InputOption::VALUE_REQUIRED, 'zip or unzip')
|
||||||
->setDescription('compress package to zip');
|
->setDescription('compress package to zip');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(Input $input, Output $output)
|
protected function execute(Input $input, Output $output)
|
||||||
{
|
{
|
||||||
$package = $this->input->getArgument('package');
|
$package = $this->input->getArgument('package');
|
||||||
if (!is_dir(CatchAdmin::directory() . $package)) {
|
$compress = $this->input->getOption('compress');
|
||||||
$package = $this->output->ask($this->input, sprintf('Can not find [%s] in catchAdmin directory, you should input package again', $package));
|
|
||||||
}
|
if (!extension_loaded('zip')) {
|
||||||
|
$this->output->error('you should install extension [zip]');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($compress == 'false') {
|
||||||
|
$this->unzip();
|
||||||
|
} else {
|
||||||
|
$this->zip($package);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 压缩包
|
||||||
|
*
|
||||||
|
* @time 2019年12月16日
|
||||||
|
* @param $package
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function zip($package): void
|
||||||
|
{
|
||||||
|
if (!is_dir(CatchAdmin::directory() . $package)) {
|
||||||
|
$package = $this->output->ask($this->input, sprintf('Can not find [%s] in catchAdmin directory, you should input package again', $package));
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_dir(CatchAdmin::directory() . $package)) {
|
if (!is_dir(CatchAdmin::directory() . $package)) {
|
||||||
$this->output->error('check package exists?');exit;
|
$this->output->error('check package exists?');exit;
|
||||||
}
|
}
|
||||||
$this->zip($package);
|
|
||||||
|
|
||||||
// 指令输出
|
$files = $this->getFilesFromDir(CatchAdmin::directory() . $package);
|
||||||
$output->info('succeed!');
|
$packageZip = new \ZipArchive();
|
||||||
}
|
$packageZip->open(CatchAdmin::directory() . $package . '.zip', \ZipArchive::CREATE);
|
||||||
|
$packageZip->addEmptyDir($package);
|
||||||
protected function zip($package)
|
|
||||||
{
|
|
||||||
$packageZip = new \ZipArchive($package . '.zip', \ZipArchive::CREATE);
|
|
||||||
|
|
||||||
$files = $dirs = [];
|
|
||||||
$this->getFilesFromDir(CatchAdmin::directory() . $package, $files, $dirs);
|
|
||||||
|
|
||||||
dd($dirs);
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
var_dump(basename($file));
|
$baseName = basename($file);
|
||||||
|
$localName = str_replace([CatchAdmin::directory(), $baseName], ['', ''], $file);
|
||||||
|
$packageZip->addFile($file, $localName . $baseName);
|
||||||
}
|
}
|
||||||
$packageZip->addFile();
|
|
||||||
$packageZip->close();
|
$packageZip->close();
|
||||||
|
$this->output->info(sprintf('%s.zip compress successfully!', $package));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* overwrite package
|
||||||
|
*
|
||||||
|
* @time 2019年12月16日
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
protected function unzip()
|
protected function unzip()
|
||||||
{
|
{
|
||||||
|
$zip = new \ZipArchive();
|
||||||
|
|
||||||
|
//dd($zip->open(CatchAdmin::directory() . 'permissions.zip'));
|
||||||
|
$res = $zip->open(CatchAdmin::directory() . 'permissions.zip');
|
||||||
|
$extractToPath = CatchAdmin::directory() . 'goods';
|
||||||
|
|
||||||
|
if (is_dir($extractToPath)) {
|
||||||
|
$this->rmDir($extractToPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
$extractToPath = CatchAdmin::makeDirectory($extractToPath);
|
||||||
|
|
||||||
|
if ($res === true) {
|
||||||
|
$zip->extractTo($extractToPath);
|
||||||
|
$zip->close();
|
||||||
|
$this->output->info('unzip successfully');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->output->error('unzip failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function getFilesFromDir($packageDir, &$files = [], &$dir = [])
|
protected function rmDir($packageDir)
|
||||||
{
|
{
|
||||||
|
$fileSystemIterator = new \FilesystemIterator($packageDir);
|
||||||
|
try {
|
||||||
|
foreach ($fileSystemIterator as $fileSystem) {
|
||||||
|
if ($fileSystem->isDir()) {
|
||||||
|
if ((new \FilesystemIterator($fileSystem->getPathName()))->valid()) {
|
||||||
|
$this->rmDir($fileSystem->getPathName());
|
||||||
|
} else {
|
||||||
|
rmdir($fileSystem->getPathName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unlink($fileSystem->getPathName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
$this->output->error($exception->getMessage());
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
rmdir($packageDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get files from dir
|
||||||
|
*
|
||||||
|
* @time 2019年12月16日
|
||||||
|
* @param $packageDir
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFilesFromDir($packageDir): array
|
||||||
|
{
|
||||||
|
$files = [];
|
||||||
|
|
||||||
$fileSystemIterator = new \FilesystemIterator($packageDir);
|
$fileSystemIterator = new \FilesystemIterator($packageDir);
|
||||||
|
|
||||||
foreach ($fileSystemIterator as $fileSystem) {
|
foreach ($fileSystemIterator as $fileSystem) {
|
||||||
if ($fileSystem->isDir()) {
|
if ($fileSystem->isDir()) {
|
||||||
$this->getFilesFromDir($fileSystem->getPathName(), $files);
|
$files = array_merge($this->getFilesFromDir($fileSystem->getPathName()), $files);
|
||||||
$dir[] = $fileSystem->getPathName();
|
|
||||||
} else {
|
} else {
|
||||||
$files[] = $fileSystem->getPathName();
|
$files[] = $fileSystem->getPathName();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $files;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user