|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tartana\Console\Command\Extract; |
|
3
|
|
|
use Joomla\Registry\Registry; |
|
4
|
|
|
use League\Flysystem\Adapter\AbstractAdapter; |
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
6
|
|
|
use Tartana\Component\Command\Command; |
|
7
|
|
|
use Tartana\Component\Command\Runner; |
|
8
|
|
|
use Tartana\Util; |
|
9
|
|
|
|
|
10
|
|
|
class UnzipCommand extends SevenzCommand |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private $executable = ''; |
|
14
|
|
|
|
|
15
|
12 |
|
public function __construct (EventDispatcherInterface $dispatcher, Runner $runner, Registry $configuration) |
|
16
|
|
|
{ |
|
17
|
|
|
// Setting the command name based on the class |
|
18
|
12 |
|
parent::__construct($dispatcher, $runner, $configuration); |
|
19
|
|
|
|
|
20
|
|
|
// On the DSM 6 7z is available only |
|
21
|
12 |
|
$cmd = new Command('which'); |
|
22
|
12 |
|
$cmd->addArgument('7z'); |
|
23
|
12 |
|
$cmd->setAsync(false); |
|
24
|
12 |
|
if ($runner->execute($cmd)) |
|
25
|
|
|
{ |
|
26
|
11 |
|
$this->executable = '7z'; |
|
27
|
|
|
} |
|
28
|
|
|
else |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->executable = 'unzip'; |
|
31
|
|
|
} |
|
32
|
12 |
|
} |
|
33
|
|
|
|
|
34
|
12 |
|
protected function configure () |
|
35
|
|
|
{ |
|
36
|
12 |
|
parent::configure(); |
|
37
|
|
|
|
|
38
|
12 |
|
$this->setName('unzip'); |
|
39
|
12 |
|
} |
|
40
|
|
|
|
|
41
|
10 |
|
protected function isSuccessfullFinished ($output) |
|
42
|
|
|
{ |
|
43
|
10 |
|
if ($this->executable == '7z') |
|
44
|
|
|
{ |
|
45
|
9 |
|
return parent::isSuccessfullFinished($output); |
|
46
|
|
|
} |
|
47
|
|
|
else |
|
48
|
|
|
{ |
|
49
|
1 |
|
return strpos($output, 'cannot find zipfile directory') === false && strpos($output, 'archive had fatal errors') === false; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
10 |
|
protected function getExtractCommand ($password, AbstractAdapter $source, AbstractAdapter $destination) |
|
54
|
|
|
{ |
|
55
|
10 |
|
if ($this->executable == '7z') |
|
56
|
|
|
{ |
|
57
|
9 |
|
return parent::getExtractCommand($password, $source, $destination); |
|
58
|
|
|
} |
|
59
|
|
|
else |
|
60
|
|
|
{ |
|
61
|
1 |
|
$command = new Command('unzip'); |
|
62
|
|
|
// Overwrite existing files |
|
63
|
1 |
|
$command->addArgument('-o'); |
|
64
|
|
|
// Password |
|
65
|
1 |
|
$command->addArgument('-p' . $password); |
|
66
|
|
|
// Input files |
|
67
|
1 |
|
$command->addArgument($source->applyPathPrefix('*.zip')); |
|
68
|
|
|
// Output |
|
69
|
1 |
|
$command->addArgument('-d ' . $destination->getPathPrefix()); |
|
70
|
|
|
|
|
71
|
1 |
|
return $command; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
8 |
|
protected function getFilesToDelete (AbstractAdapter $source) |
|
76
|
|
|
{ |
|
77
|
8 |
|
$filesToDelete = []; |
|
78
|
8 |
|
foreach ($source->listContents() as $file) |
|
79
|
|
|
{ |
|
80
|
8 |
|
if (! Util::endsWith($file['path'], '.zip')) |
|
81
|
|
|
{ |
|
82
|
5 |
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
8 |
|
$filesToDelete[] = $file['path']; |
|
86
|
|
|
} |
|
87
|
8 |
|
return $filesToDelete; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
9 |
|
protected function getFileExtension () |
|
91
|
|
|
{ |
|
92
|
9 |
|
return 'zip'; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|