Gzip::streamToFile()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 2
cp 0
rs 9.6111
cc 5
nc 4
nop 1
crap 30
1
<?php
2
namespace wapmorgan\UnifiedArchive\Drivers\OneFile;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException;
6
use wapmorgan\UnifiedArchive\Formats;
7
8
class Gzip extends OneFileDriver
9
{
10
    const EXTENSION_NAME = 'zlib';
11
    const FORMAT = Formats::GZIP;
12
13
    /**
14
     * @inheritDoc
15 1
     */
16
    public static function getDescription()
17
    {
18 1
        return 'adapter for ext-zlib'.(defined('ZLIB_VERSION') ? ' ('.ZLIB_VERSION.')' : null);
19
    }
20
21
    /**
22
     * @param string $file GZipped file
23
     * @return array|false Array with 'mtime' and 'size' items
24
     */
25
    public static function gzipStat($file)
26
    {
27
        $fp = fopen($file, 'rb');
28
        if (filesize($file) < 18 || strcmp(fread($fp, 2), "\x1f\x8b")) {
29
            return false;  // Not GZIP format (See RFC 1952)
30
        }
31
        $method = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $method is dead and can be removed.
Loading history...
32
        $flags = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $flags is dead and can be removed.
Loading history...
33
        $stat = unpack('Vmtime', fread($fp, 4));
34
        fseek($fp, -4, SEEK_END);
35
        $stat += unpack('Vsize', fread($fp, 4));
36
        fclose($fp);
37
38
        return $stat;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function __construct($archiveFileName, $format, $password = null)
45
    {
46
        parent::__construct($archiveFileName, $format, $password);
47
        $stat = static::gzipStat($archiveFileName);
48
        if ($stat === false) {
49
            throw new Exception('Could not open Gzip file');
50
        }
51
        $this->uncompressedSize = $stat['size'];
52
        $this->modificationTime = $stat['mtime'];
53
    }
54
55
    /**
56
     * @param string $fileName
57
     *
58
     * @return string|false
59
     */
60
    public function getFileContent($fileName = null)
61
    {
62
        return gzdecode(file_get_contents($this->fileName));
63
    }
64
65
    /**
66
     * @param string $fileName
67
     *
68
     * @return bool|resource|string
69
     */
70
    public function getFileStream($fileName = null)
71
    {
72
        return gzopen($this->fileName, 'rb');
73
    }
74
75
    /**
76
     * @param $data
77
     * @param $compressionLevel
78
     * @return mixed|string
79
     */
80
    protected static function compressData($data, $compressionLevel)
81
    {
82
        static $compressionLevelMap = [
83
            self::COMPRESSION_NONE => 0,
84
            self::COMPRESSION_WEAK => 2,
85
            self::COMPRESSION_AVERAGE => 4,
86
            self::COMPRESSION_STRONG => 7,
87
            self::COMPRESSION_MAXIMUM => 9,
88
        ];
89
        return gzencode($data, $compressionLevelMap[$compressionLevel]);
90
    }
91
92
    /**
93
     * @param string $targetPath
94
     */
95
    public function streamToFile($targetPath)
96
    {
97
        $sfp = gzopen($this->fileName, 'rb');
98
        $fp = fopen($targetPath, "w");
99
        while (!gzeof($sfp)) {
100
            $chunk = gzread($sfp, 8192);
101
            if($chunk === 0 || $chunk === false)
102
                throw new ArchiveExtractionException('Cannot read gzip chunk');
103
            if(fwrite($fp, $chunk, strlen($chunk)) === false)
104
                throw new ArchiveExtractionException('Cannot write gzip chunk');
105
        }
106
        gzclose($sfp);
107
        fclose($fp);
108
    }
109
}
110