Passed
Push — 0.1.x ( 81ff64...72f342 )
by f
01:32
created

Rar::getFileContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\ArchiveEntry;
6
use wapmorgan\UnifiedArchive\ArchiveInformation;
7
use wapmorgan\UnifiedArchive\UnsupportedOperationException;
8
9
class Rar extends BasicFormat
10
{
11
    /** @var \RarArchive */
12
    protected $rar;
13
14
    /**
15
     * BasicFormat constructor.
16
     *
17
     * @param string $archiveFileName
18
     *
19
     * @throws \Exception
20
     */
21
    public function __construct($archiveFileName)
22
    {
23
        \RarException::setUsingExceptions(true);
24
        $this->open($archiveFileName);
25
    }
26
27
    /**
28
     * @param $archiveFileName
29
     *
30
     * @throws \Exception
31
     */
32
    protected function open($archiveFileName)
33
    {
34
        $this->rar = \RarArchive::open($archiveFileName);
35
        if ($this->rar === false) {
36
            throw new Exception('Could not open Rar archive');
37
        }
38
    }
39
40
    /**
41
     * Rar format destructor
42
     */
43
    public function __destruct()
44
    {
45
        $this->rar->close();
46
    }
47
48
    /**
49
     * @return ArchiveInformation
50
     */
51
    public function getArchiveInformation()
52
    {
53
        $information = new ArchiveInformation();
54
        foreach ($this->rar->getEntries() as $i => $entry) {
55
            $information->files[] = $entry->getName();
56
            $information->compressedFilesSize += $entry->getPackedSize();
57
            $information->uncompressedFilesSize += $entry->getUnpackedSize();
58
        }
59
        return $information;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getFileNames()
66
    {
67
        $files = [];
68
        foreach ($this->rar->getEntries() as $i => $entry) {
69
            $files[] = $entry->getName();
70
        }
71
        return $files;
72
    }
73
74
    /**
75
     * @param string $fileName
76
     *
77
     * @return bool
78
     */
79
    public function isFileExists($fileName)
80
    {
81
        return $this->rar->getEntry($fileName) !== false;
82
    }
83
84
    /**
85
     * @param string $fileName
86
     *
87
     * @return ArchiveEntry|false
88
     */
89
    public function getFileData($fileName)
90
    {
91
        $entry = $this->rar->getEntry($fileName);
92
        return new ArchiveEntry($fileName, $entry->getPackedSize(), $entry->getUnpackedSize(),
93
            strtotime($entry->getFileTime()), $entry->getMethod() != 48);
94
    }
95
96
    /**
97
     * @param string $fileName
98
     *
99
     * @return string|false
100
     */
101
    public function getFileContent($fileName)
102
    {
103
        $entry = $this->rar->getEntry($fileName);
104
        if ($entry->isDirectory()) return false;
105
        return stream_get_contents($entry->getStream());
106
    }
107
108
    /**
109
     * @param string $fileName
110
     *
111
     * @return bool|resource|string
112
     */
113
    public function getFileResource($fileName)
114
    {
115
        $entry = $this->rar->getEntry($fileName);
116
        if ($entry->isDirectory()) return false;
117
        return $entry->getStream();
118
    }
119
120
    /**
121
     * @param string $outputFolder
122
     * @param array  $files
123
     *
124
     * @return false|int
125
     */
126
    public function extractFiles($outputFolder, array $files)
127
    {
128
        $count = 0;
129
        foreach ($files as $file) {
130
            if ($this->rar->getEntry($file)->extract($outputFolder)) {
131
                $count++;
132
            }
133
        }
134
        return $count;
135
    }
136
137
    /**
138
     * @param string $outputFolder
139
     *
140
     * @return false|resource
141
     */
142
    public function extractArchive($outputFolder)
143
    {
144
        return $this->extractFiles($outputFolder, $this->getFileNames());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->extractFil... $this->getFileNames()) returns the type integer which is incompatible with the documented return type false|resource.
Loading history...
145
    }
146
147
    /**
148
     * @param array $files
149
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
150
     */
151
    public function deleteFiles(array $files)
152
    {
153
        throw new UnsupportedOperationException();
154
    }
155
156
    /**
157
     * @param array $files
158
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
159
     */
160
    public function addFiles(array $files)
161
    {
162
        throw new UnsupportedOperationException();
163
    }
164
165
    /**
166
     * @param array  $files
167
     * @param string $archiveFileName
168
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
169
     */
170
    public static function createArchive(array $files, $archiveFileName){
171
        throw new UnsupportedOperationException();
172
    }
173
}