|
1
|
|
|
<?php |
|
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers\OneFile; |
|
3
|
|
|
|
|
4
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException; |
|
5
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
|
6
|
|
|
|
|
7
|
|
|
class Bzip extends OneFileDriver |
|
8
|
|
|
{ |
|
9
|
|
|
const EXTENSION_NAME = 'bz2'; |
|
10
|
|
|
const FORMAT = Formats::BZIP; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @return array |
|
14
|
1 |
|
*/ |
|
15
|
|
|
public static function getFormats() |
|
16
|
|
|
{ |
|
17
|
1 |
|
return [ |
|
18
|
|
|
Formats::BZIP, |
|
19
|
|
|
]; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function getDescription() |
|
26
|
|
|
{ |
|
27
|
|
|
return 'adapter for ext-bzip2'.(static::isInstalled() ? ' ('.phpversion(static::EXTENSION_NAME).')' : null); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritDoc |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($archiveFileName, $format, $password); |
|
36
|
|
|
$this->modificationTime = filemtime($this->fileName); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $fileName |
|
41
|
|
|
* |
|
42
|
|
|
* @return string|false |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getFileContent($fileName = null) |
|
45
|
|
|
{ |
|
46
|
|
|
return bzdecompress(file_get_contents($this->fileName)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $fileName |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool|resource|string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getFileStream($fileName = null) |
|
55
|
|
|
{ |
|
56
|
|
|
return bzopen($this->fileName, 'r'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $data |
|
61
|
|
|
* @param int $compressionLevel |
|
62
|
|
|
* @return mixed|string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected static function compressData($data, $compressionLevel) |
|
65
|
|
|
{ |
|
66
|
|
|
static $compressionLevelMap = [ |
|
67
|
|
|
self::COMPRESSION_NONE => 1, |
|
68
|
|
|
self::COMPRESSION_WEAK => 2, |
|
69
|
|
|
self::COMPRESSION_AVERAGE => 4, |
|
70
|
|
|
self::COMPRESSION_STRONG => 7, |
|
71
|
|
|
self::COMPRESSION_MAXIMUM => 9, |
|
72
|
|
|
]; |
|
73
|
|
|
static $work_factor_multiplier = 27; |
|
74
|
|
|
|
|
75
|
|
|
// it seems not working at all |
|
76
|
|
|
$work_factor = ($compressionLevelMap[$compressionLevel] * $work_factor_multiplier); |
|
77
|
|
|
return bzcompress($data, $compressionLevelMap[$compressionLevel], $work_factor); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $targetPath |
|
82
|
|
|
*/ |
|
83
|
|
|
public function streamToFile($targetPath) |
|
84
|
|
|
{ |
|
85
|
|
|
$sfp = bzopen($this->fileName, 'r'); |
|
86
|
|
|
$fp = fopen($targetPath, "w"); |
|
87
|
|
|
while (!feof($sfp)) { |
|
88
|
|
|
$chunk = bzread($sfp, 8192); |
|
89
|
|
|
if($chunk === 0 || $chunk === false) |
|
90
|
|
|
throw new ArchiveExtractionException('Cannot read bzip chunk'); |
|
91
|
|
|
if(fwrite($fp, $chunk, strlen($chunk)) === false) |
|
92
|
|
|
throw new ArchiveExtractionException('Cannot write bzip chunk'); |
|
93
|
|
|
} |
|
94
|
|
|
bzclose($sfp); |
|
95
|
|
|
fclose($fp); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|