1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers\OneFile; |
3
|
|
|
|
4
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
5
|
|
|
use wapmorgan\UnifiedArchive\Drivers\OneFile\OneFileDriver; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Lzma |
9
|
|
|
* |
10
|
|
|
* @package wapmorgan\UnifiedArchive\Formats |
11
|
|
|
* @requires ext-lzma2 |
12
|
|
|
*/ |
13
|
|
|
class Lzma extends OneFileDriver |
14
|
|
|
{ |
15
|
|
|
const FORMAT_SUFFIX = 'xz'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
1 |
|
public static function getSupportedFormats() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
1 |
|
Formats::LZMA, |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $format |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public static function checkFormatSupport($format) |
32
|
|
|
{ |
33
|
|
|
switch ($format) { |
34
|
|
|
case Formats::LZMA: |
35
|
|
|
return extension_loaded('xz'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public static function getDescription() |
43
|
|
|
{ |
44
|
|
|
return 'adapter for ext-xz'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public static function getInstallationInstruction() |
51
|
|
|
{ |
52
|
|
|
return !extension_loaded('xz') |
53
|
|
|
? 'install `xz` extension' |
54
|
|
|
: null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
61
|
|
|
{ |
62
|
|
|
parent::__construct($archiveFileName, $password); |
63
|
|
|
$this->modificationTime = filemtime($this->fileName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $fileName |
68
|
|
|
* |
69
|
|
|
* @return string|false |
70
|
|
|
*/ |
71
|
|
|
public function getFileContent($fileName = null) |
72
|
|
|
{ |
73
|
|
|
return stream_get_contents(xzopen($this->fileName, 'r')); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $fileName |
78
|
|
|
* |
79
|
|
|
* @return bool|resource|string |
80
|
|
|
*/ |
81
|
|
|
public function getFileStream($fileName = null) |
82
|
|
|
{ |
83
|
|
|
return xzopen($this->fileName, 'r'); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $data |
88
|
|
|
* @param $compressionLevel |
89
|
|
|
* @return mixed|string |
90
|
|
|
*/ |
91
|
|
|
protected static function compressData($data, $compressionLevel) |
92
|
|
|
{ |
93
|
|
|
$fp = xzopen('php://temp', 'w'); |
|
|
|
|
94
|
|
|
xzwrite($fp, $data); |
|
|
|
|
95
|
|
|
$data = stream_get_contents($fp); |
96
|
|
|
xzclose($fp); |
|
|
|
|
97
|
|
|
return $data; |
98
|
|
|
} |
99
|
|
|
} |