1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers\OneFile; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use wapmorgan\UnifiedArchive\Drivers\BasicDriver; |
6
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
7
|
|
|
use wapmorgan\UnifiedArchive\Drivers\OneFile\OneFileDriver; |
8
|
|
|
|
9
|
|
|
class Gzip extends OneFileDriver |
10
|
|
|
{ |
11
|
|
|
const FORMAT_SUFFIX = 'gz'; |
12
|
|
|
const PHP_EXTENSION = 'zlib'; |
13
|
|
|
const FORMAT = Formats::GZIP; |
14
|
|
|
|
15
|
1 |
|
/** |
16
|
|
|
* @inheritDoc |
17
|
|
|
*/ |
18
|
1 |
|
public static function getDescription() |
19
|
|
|
{ |
20
|
|
|
return 'adapter for ext-zlib'.(defined('ZLIB_VERSION') ? ' ('.ZLIB_VERSION.')' : null); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $file GZipped file |
25
|
|
|
* @return array|false Array with 'mtime' and 'size' items |
26
|
|
|
*/ |
27
|
|
|
public static function gzipStat($file) |
28
|
|
|
{ |
29
|
|
|
$fp = fopen($file, 'rb'); |
30
|
|
|
if (filesize($file) < 18 || strcmp(fread($fp, 2), "\x1f\x8b")) { |
31
|
|
|
return false; // Not GZIP format (See RFC 1952) |
32
|
|
|
} |
33
|
|
|
$method = fread($fp, 1); |
|
|
|
|
34
|
|
|
$flags = fread($fp, 1); |
|
|
|
|
35
|
|
|
$stat = unpack('Vmtime', fread($fp, 4)); |
36
|
|
|
fseek($fp, -4, SEEK_END); |
37
|
|
|
$stat += unpack('Vsize', fread($fp, 4)); |
38
|
|
|
fclose($fp); |
39
|
|
|
|
40
|
|
|
return $stat; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
47
|
|
|
{ |
48
|
|
|
parent::__construct($archiveFileName, $format, $password); |
49
|
|
|
$stat = static::gzipStat($archiveFileName); |
50
|
|
|
if ($stat === false) { |
51
|
|
|
throw new Exception('Could not open Gzip file'); |
52
|
|
|
} |
53
|
|
|
$this->uncompressedSize = $stat['size']; |
54
|
|
|
$this->modificationTime = $stat['mtime']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $fileName |
59
|
|
|
* |
60
|
|
|
* @return string|false |
61
|
|
|
*/ |
62
|
|
|
public function getFileContent($fileName = null) |
63
|
|
|
{ |
64
|
|
|
return gzdecode(file_get_contents($this->fileName)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $fileName |
69
|
|
|
* |
70
|
|
|
* @return bool|resource|string |
71
|
|
|
*/ |
72
|
|
|
public function getFileStream($fileName = null) |
73
|
|
|
{ |
74
|
|
|
return gzopen($this->fileName, 'rb'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $data |
79
|
|
|
* @param $compressionLevel |
80
|
|
|
* @return mixed|string |
81
|
|
|
*/ |
82
|
|
|
protected static function compressData($data, $compressionLevel) |
83
|
|
|
{ |
84
|
|
|
static $compressionLevelMap = [ |
85
|
|
|
self::COMPRESSION_NONE => 0, |
86
|
|
|
self::COMPRESSION_WEAK => 2, |
87
|
|
|
self::COMPRESSION_AVERAGE => 4, |
88
|
|
|
self::COMPRESSION_STRONG => 7, |
89
|
|
|
self::COMPRESSION_MAXIMUM => 9, |
90
|
|
|
]; |
91
|
|
|
return gzencode($data, $compressionLevelMap[$compressionLevel]); |
92
|
|
|
} |
93
|
|
|
} |