Passed
Pull Request — master (#36)
by Cédric
11:49
created

Gzip::gzipStat()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 1
dl 0
loc 14
ccs 0
cts 5
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $method is dead and can be removed.
Loading history...
34
        $flags = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $flags is dead and can be removed.
Loading history...
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
}