-
Notifications
You must be signed in to change notification settings - Fork 22
/
NelexaZip.php
218 lines (188 loc) · 5.09 KB
/
NelexaZip.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace wapmorgan\UnifiedArchive\Drivers;
use PhpZip\ZipFile;
use wapmorgan\UnifiedArchive\ArchiveEntry;
use wapmorgan\UnifiedArchive\ArchiveInformation;
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
use wapmorgan\UnifiedArchive\Formats;
class NelexaZip extends BasicDriver
{
const TYPE = self::TYPE_PURE_PHP;
/**
* @var ZipFile
*/
protected $zip;
/**
* @var array
*/
protected $files;
public static function getDescription()
{
return 'nelexa/zip driver';
}
public static function isInstalled()
{
return class_exists('\\PhpZip\\ZipFile');
}
public static function getInstallationInstruction()
{
return 'install library [nelexa/zip]: `composer require nelexa/zip`';
}
public static function getSupportedFormats()
{
return [
Formats::ZIP,
];
}
public static function checkFormatSupport($format)
{
if (!static::isInstalled()) {
return [];
}
return [
BasicDriver::OPEN,
BasicDriver::OPEN_ENCRYPTED,
BasicDriver::GET_COMMENT,
BasicDriver::SET_COMMENT,
BasicDriver::EXTRACT_CONTENT,
BasicDriver::APPEND,
BasicDriver::DELETE,
];
}
public static function createArchive(
array $files,
$archiveFileName,
$archiveFormat,
$compressionLevel = self::COMPRESSION_AVERAGE,
$password = null,
$fileProgressCallable = null)
{
}
/**
* @inheritDoc
* @throws \PhpZip\Exception\ZipException
*/
public function __construct($archiveFileName, $format, $password = null)
{
parent::__construct($archiveFileName, $format);
$this->zip = new ZipFile();
$this->zip->openFile($archiveFileName);
if ($password !== null) {
$this->zip->setReadPassword($password);
}
}
/**
* @inheritDoc
*/
public function getArchiveInformation()
{
$this->files = [];
$information = new ArchiveInformation();
$files = method_exists($this->zip, 'getAllInfo')
? $this->zip->getAllInfo()
: $this->zip->getEntries();
foreach ($files as $info) {
if (method_exists($info, 'isFolder') ? $info->isFolder() : $info->isDirectory())
continue;
$this->files[] = $information->files[] = str_replace('\\', '/', $info->getName());
$information->compressedFilesSize += $info->getCompressedSize();
$information->uncompressedFilesSize += method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize();
}
return $information;
}
/**
* @inheritDoc
*/
public function getFileNames()
{
return $this->files;
}
/**
* @inheritDoc
*/
public function isFileExists($fileName)
{
return $this->zip->hasEntry($fileName);
}
/**
* @inheritDoc
*/
public function getFileData($fileName)
{
$info = method_exists($this->zip, 'getEntryInfo')
? $this->zip->getEntryInfo($fileName)
: $this->zip->getEntry($fileName);
return new ArchiveEntry(
$fileName,
$info->getCompressedSize(),
method_exists($info, 'getSize') ? $info->getSize() : $info->getUncompressedSize(),
$info->getMtime(),
null,
$info->getComment(),
$info->getCrc()
);
}
/**
* @inheritDoc
*/
public function getFileContent($fileName)
{
return $this->zip->getEntryContents($fileName);
}
/**
* @inheritDoc
* @throws NonExistentArchiveFileException
*/
public function getFileStream($fileName)
{
return static::wrapStringInStream($this->getFileContent($fileName));
}
/**
* @inheritDoc
*/
public function extractFiles($outputFolder, array $files)
{
$this->zip->extractTo($outputFolder, $files);
return count($files);
}
/**
* @inheritDoc
*/
public function extractArchive($outputFolder)
{
$this->zip->extractTo($outputFolder);
return count($this->files);
}
/**
* @inheritDoc
* @throws \PhpZip\Exception\ZipException
*/
public function addFileFromString($inArchiveName, $content)
{
return $this->zip->addFromString($inArchiveName, $content);
}
public function getComment()
{
return $this->zip->getArchiveComment();
}
public function setComment($comment)
{
return $this->zip->setArchiveComment($comment);
}
public function deleteFiles(array $files)
{
$deleted = 0;
foreach ($files as $file) {
$this->zip->deleteFromName($file);
$deleted++;
}
return $deleted;
}
public function addFiles(array $files)
{
foreach ($files as $inArchiveName => $fsFileName)
{
$this->zip->addFile($fsFileName, $inArchiveName);
}
}
}