|
1
|
|
|
<?php |
|
2
|
|
|
namespace wapmorgan\UnifiedArchive\Formats; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
|
|
6
|
|
|
class Gzip extends OneFileFormat |
|
7
|
|
|
{ |
|
8
|
|
|
const FORMAT_SUFFIX = 'gz'; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @param string $file GZipped file |
|
12
|
|
|
* @return array|false Array with 'mtime' and 'size' items |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function gzipStat($file) |
|
15
|
|
|
{ |
|
16
|
|
|
$fp = fopen($file, 'rb'); |
|
17
|
|
|
if (filesize($file) < 18 || strcmp(fread($fp, 2), "\x1f\x8b")) { |
|
|
|
|
|
|
18
|
|
|
return false; // Not GZIP format (See RFC 1952) |
|
19
|
|
|
} |
|
20
|
|
|
$method = fread($fp, 1); |
|
|
|
|
|
|
21
|
|
|
$flags = fread($fp, 1); |
|
|
|
|
|
|
22
|
|
|
$stat = unpack('Vmtime', fread($fp, 4)); |
|
23
|
|
|
fseek($fp, -4, SEEK_END); |
|
|
|
|
|
|
24
|
|
|
$stat += unpack('Vsize', fread($fp, 4)); |
|
25
|
|
|
fclose($fp); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
return $stat; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Gzip constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param $archiveFileName |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($archiveFileName) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct($archiveFileName); |
|
40
|
|
|
$stat = static::gzipStat($archiveFileName); |
|
41
|
|
|
if ($stat === false) { |
|
42
|
|
|
throw new Exception('Could not open Gzip file'); |
|
43
|
|
|
} |
|
44
|
|
|
$this->uncompressedSize = $stat['size']; |
|
45
|
|
|
$this->modificationTime = $stat['mtime']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $fileName |
|
50
|
|
|
* |
|
51
|
|
|
* @return string|false |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getFileContent($fileName = null) |
|
54
|
|
|
{ |
|
55
|
|
|
return gzdecode(file_get_contents($this->fileName)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $fileName |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool|resource|string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getFileResource($fileName = null) |
|
64
|
|
|
{ |
|
65
|
|
|
return gzopen($this->fileName, 'rb'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $data |
|
70
|
|
|
* |
|
71
|
|
|
* @return mixed|string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected static function compressData($data) |
|
74
|
|
|
{ |
|
75
|
|
|
return gzencode($data); |
|
76
|
|
|
} |
|
77
|
|
|
} |