Passed
Push — 0.1.x ( 3e3fb7...4a45f6 )
by f
02:35
created

Zip::getPclZip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\PclzipZipInterface;
8
use ZipArchive;
9
10
/**
11
 * Class Zip
12
 *
13
 * @package wapmorgan\UnifiedArchive\Formats
14
 * @requires ext-zip
15
 */
16
class Zip extends BasicFormat
17
{
18
    /** @var ZipArchive */
19
    protected $zip;
20
21
    /**
22
     * BasicFormat constructor.
23
     *
24
     * @param string $archiveFileName
25
     * @throws \Exception
26
     */
27
    public function __construct($archiveFileName)
28
    {
29
        $this->open($archiveFileName);
30
    }
31
32
    /**
33
     * @param $archiveFileName
34
     *
35
     * @throws \Exception
36
     */
37
    protected function open($archiveFileName)
38
    {
39
        $this->zip = new ZipArchive();
40
        $open_result = $this->zip->open($archiveFileName);
41
        if ($open_result !== true) {
42
            throw new Exception('Could not open Zip archive: '.$open_result);
43
        }
44
    }
45
46
    /**
47
     * Zip format destructor
48
     */
49
    public function __destruct()
50
    {
51
        unset($this->zip);
52
    }
53
54
    /**
55
     * @return ArchiveInformation
56
     */
57
    public function getArchiveInformation()
58
    {
59
        $information = new ArchiveInformation();
60
        for ($i = 0; $i < $this->zip->numFiles; $i++) {
61
            $file = $this->zip->statIndex($i);
62
            $information->files[$i] = $file['name'];
63
            $information->compressedFilesSize += $file['comp_size'];
64
            $information->uncompressedFilesSize += $file['size'];
65
        }
66
        return $information;
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getFileNames()
73
    {
74
        $files = [];
75
        for ($i = 0; $i < $this->zip->numFiles; $i++)
76
            $files[] = $this->zip->getNameIndex($i);
77
        return $files;
78
    }
79
80
    /**
81
     * @param string $fileName
82
     *
83
     * @return bool
84
     */
85
    public function isFileExists($fileName)
86
    {
87
        return $this->zip->statName($fileName) !== false;
88
    }
89
90
    /**
91
     * @param string $fileName
92
     *
93
     * @return ArchiveEntry
94
     */
95
    public function getFileData($fileName)
96
    {
97
        $stat = $this->zip->statName($fileName);
98
        return new ArchiveEntry($fileName, $stat['comp_size'], $stat['size'], $stat['mtime'],
99
            $stat['comp_method'] != 0);
100
    }
101
102
    /**
103
     * @param string $fileName
104
     *
105
     * @return string|false
106
     * @throws \Exception
107
     */
108
    public function getFileContent($fileName)
109
    {
110
        $result = $this->zip->getFromName($fileName);
111
        if ($result === false)
112
            throw new Exception('Could not get file information: '.$result);
113
        return $result;
114
    }
115
116
    /**
117
     * @param string $fileName
118
     *
119
     * @return bool|resource|string
120
     */
121
    public function getFileResource($fileName)
122
    {
123
        return $this->zip->getStream($fileName);
124
    }
125
126
    /**
127
     * @param string $outputFolder
128
     * @param array $files
129
     * @return false|resource
130
     * @throws \Exception
131
     */
132
    public function extractFiles($outputFolder, array $files)
133
    {
134
        if ($this->zip->extractTo($outputFolder, $files) === false)
135
            throw new Exception($this->zip->getStatusString(), $this->zip->status);
136
137
        return count($files);
0 ignored issues
show
Bug Best Practice introduced by
The expression return count($files) returns the type integer which is incompatible with the documented return type false|resource.
Loading history...
138
    }
139
140
    /**
141
     * @param string $outputFolder
142
     * @throws \Exception
143
     */
144
    public function extractArchive($outputFolder)
145
    {
146
        if ($this->zip->extractTo($outputFolder) === false)
147
            throw new Exception($this->zip->getStatusString(), $this->zip->status);
148
        return $this->zip->numFiles;
149
    }
150
151
    /**
152
     * @param array $files
153
     *
154
     * @return int
155
     * @throws \Exception
156
     */
157
    public function deleteFiles(array $files)
158
    {
159
        $count = 0;
160
        foreach ($files as $file) {
161
            if ($this->zip->deleteName($file) === false)
162
                throw new Exception($this->zip->getStatusString(), $this->zip->status);
163
            $count++;
164
        }
165
166
        // reopen archive to save changes
167
        $archive_filename = $this->zip->filename;
168
        $this->zip->close();
169
        $this->open($archive_filename);
170
171
        return $count;
172
    }
173
174
    /**
175
     * @param array $files
176
     *
177
     * @return int
178
     * @throws \Exception
179
     */
180
    public function addFiles(array $files)
181
    {
182
        $added_files = 0;
183
        foreach ($files as $localName => $fileName) {
184
            if (is_null($fileName)) {
185
                if ($this->zip->addEmptyDir($localName) === false)
186
                    throw new Exception($this->zip->getStatusString(), $this->zip->status);
187
            } else {
188
                if ($this->zip->addFile($fileName, $localName) === false)
189
                    throw new Exception($this->zip->getStatusString(), $this->zip->status);
190
                $added_files++;
191
            }
192
        }
193
194
        // reopen archive to save changes
195
        $archive_filename = $this->zip->filename;
196
        $this->zip->close();
197
        $this->open($archive_filename);
198
199
        return $added_files;
200
    }
201
202
    /**
203
     * @param array  $files
204
     * @param string $archiveFileName
205
     *
206
     * @return false|int
207
     * @throws \Exception
208
     */
209
    public static function createArchive(array $files, $archiveFileName){
210
        $zip = new ZipArchive();
211
        $result = $zip->open($archiveFileName, ZipArchive::CREATE);
212
        if ($result !== true)
213
            throw new Exception('ZipArchive error: '.$result);
214
        foreach ($files as $localName => $fileName) {
215
            if ($fileName === null) {
216
                if ($zip->addEmptyDir($localName) === false)
217
                    return false;
218
            } else {
219
                if ($zip->addFile($fileName, $localName) === false)
220
                    return false;
221
            }
222
        }
223
        $zip->close();
224
225
        return count($files);
226
    }
227
228
    /**
229
     * @return \wapmorgan\UnifiedArchive\PclzipZipInterface
230
     */
231
    public function getPclZip()
232
    {
233
        return new PclzipZipInterface($this->zip);
234
    }
235
}