/
ZipRepository.php
190 lines (173 loc) · 4.75 KB
/
ZipRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Madnest\Madzipper\Repositories;
use Exception;
use ZipArchive;
class ZipRepository implements RepositoryInterface
{
private $archive;
/**
* Construct with a given path
*
* @param $filePath
* @param bool $create
* @param $archive
*
* @throws \Exception
*
* @return ZipRepository
*/
public function __construct($filePath, $create = false, $archive = null)
{
//Check if ZipArchive is available
if (! class_exists('ZipArchive')) {
throw new Exception('Error: Your PHP version is not compiled with zip support');
}
$this->archive = $archive ? $archive : new ZipArchive();
$res = $this->archive->open($filePath, ($create ? ZipArchive::CREATE : null));
if ($res !== true) {
throw new Exception("Error: Failed to open $filePath! Error: " . $this->getErrorMessage($res));
}
}
/**
* Add a file to the opened Archive
*
* @param $pathToFile
* @param $pathInArchive
*/
public function addFile($pathToFile, $pathInArchive)
{
$this->archive->addFile($pathToFile, $pathInArchive);
}
/**
* Add an empty directory
*
* @param $dirName
*/
public function addEmptyDir($dirName)
{
$this->archive->addEmptyDir($dirName);
}
/**
* Add a file to the opened Archive using its contents
*
* @param $name
* @param $content
*/
public function addFromString($name, $content)
{
$this->archive->addFromString($name, $content);
}
/**
* Remove a file permanently from the Archive
*
* @param $pathInArchive
*/
public function removeFile($pathInArchive)
{
$this->archive->deleteName($pathInArchive);
}
/**
* Get the content of a file
*
* @param $pathInArchive
*
* @return string
*/
public function getFileContent($pathInArchive)
{
return $this->archive->getFromName($pathInArchive);
}
/**
* Get the stream of a file
*
* @param $pathInArchive
*
* @return mixed
*/
public function getFileStream($pathInArchive)
{
return $this->archive->getStream($pathInArchive);
}
/**
* Will loop over every item in the archive and will execute the callback on them
* Will provide the filename for every item
*
* @param $callback
*/
public function each($callback)
{
for ($i = 0; $i < $this->archive->numFiles; ++$i) {
//skip if folder
$stats = $this->archive->statIndex($i);
if ($stats['size'] === 0 && $stats['crc'] === 0) {
continue;
}
call_user_func_array($callback, [
'file' => $this->archive->getNameIndex($i),
'stats' => $this->archive->statIndex($i),
]);
}
}
/**
* Checks whether the file is in the archive
*
* @param $fileInArchive
*
* @return bool
*/
public function fileExists($fileInArchive)
{
return $this->archive->locateName($fileInArchive) !== false;
}
/**
* Sets the password to be used for decompressing
* function named usePassword for clarity
*
* @param $password
*
* @return bool
*/
public function usePassword($password)
{
return $this->archive->setPassword($password);
}
/**
* Returns the status of the archive as a string
*
* @return string
*/
public function getStatus()
{
return $this->archive->getStatusString();
}
/**
* Closes the archive and saves it
*/
public function close()
{
@$this->archive->close();
}
private function getErrorMessage($resultCode)
{
switch ($resultCode) {
case ZipArchive::ER_EXISTS:
return 'ZipArchive::ER_EXISTS - File already exists.';
case ZipArchive::ER_INCONS:
return 'ZipArchive::ER_INCONS - Zip archive inconsistent.';
case ZipArchive::ER_MEMORY:
return 'ZipArchive::ER_MEMORY - Malloc failure.';
case ZipArchive::ER_NOENT:
return 'ZipArchive::ER_NOENT - No such file.';
case ZipArchive::ER_NOZIP:
return 'ZipArchive::ER_NOZIP - Not a zip archive.';
case ZipArchive::ER_OPEN:
return 'ZipArchive::ER_OPEN - Can\'t open file.';
case ZipArchive::ER_READ:
return 'ZipArchive::ER_READ - Read error.';
case ZipArchive::ER_SEEK:
return 'ZipArchive::ER_SEEK - Seek error.';
default:
return "An unknown error [$resultCode] has occurred.";
}
}
}