-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipFile.php
More file actions
176 lines (154 loc) · 4.54 KB
/
ZipFile.php
File metadata and controls
176 lines (154 loc) · 4.54 KB
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
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Utils;
use Arikaim\Core\Utils\File;
use \ZipArchive;
use Exception;
/**
* Zip file helpers
*/
class ZipFile
{
/**
* Extract zip arhive
*
* @param string $file
* @param string $destination
* @param array|string|int|null $files
* @throws Exception
* @return bool
*/
public static function extract(string $file, string $destination, $files = null): bool
{
$zip = new ZipArchive;
$result = $zip->open($file);
if ($result !== true) {
throw new Exception("Zip file not valid", 1);
}
if (File::isWritable($destination) == false) {
File::setWritable($destination);
}
if (\is_integer($files) == true) {
$item = $zip->getNameIndex($files);
$files = [$item];
}
$result = $zip->extractTo($destination,$files);
$zip->close();
return $result;
}
/**
* Get zip file item name
*
* @param string $zipFile
* @param int $index
* @return string|null
*/
public static function getItemPath(string $zipFile, int $index): ?string
{
$zip = new ZipArchive;
$result = $zip->open($zipFile);
if ($result !== true) {
return null;
}
return $zip->getNameIndex($index);
}
/**
* Create zip arhive
*
* @param string $source
* @param string $destination
* @param array $skipDir
* @return boolean
*/
public static function create(string $source, string $destination, array $skipDir = []): bool
{
$skipDir = $skipDir ?? ['.htaccess','.gitkeep','.git'];
$zip = new ZipArchive();
if ($zip->open($destination,ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
return false;
}
if (\is_dir($source) == true) {
$files = File::scanDirRecursive($source,function ($file, $key, $iterator) use ($skipDir) {
if ($iterator->hasChildren() && !in_array($file->getFilename(),$skipDir)) {
return true;
}
return $file->isFile();
});
$relativePath = '';
foreach ($files as $file) {
$relativePath = \str_replace($source,'',$file->getPathname());
if ($file->isDir() == false) {
$zip->addFile($file->getRealPath(),$relativePath );
}
}
} else {
$zip->addFile($source);
}
$zip->close();
return ($zip->status == ZIPARCHIVE::ER_OK);
}
/**
* Check if zip arhive is valid
*
* @param string $file
* @return boolean
*/
public static function isValid(string $file): bool
{
$zip = new ZipArchive();
$result = $zip->open($file,ZipArchive::CHECKCONS);
switch($result) {
case ZipArchive::ER_NOZIP:
return false;
case ZipArchive::ER_INCONS:
return false;
case ZipArchive::ER_CRC:
return false;
}
return true;
}
/**
* Get zip error
*
* @param mixed $resource
* @return string|null
*/
public static function getZipError($resource): ?string
{
switch($resource) {
case ZipArchive::ER_NOZIP :
return 'Not a zip archive';
case ZipArchive::ER_INCONS :
return 'Consistency check failed';
case ZipArchive::ER_CRC :
return 'Checksum failed';
}
return null;
}
/**
* Get zip file files
*
* @param string $zipFile
* @return array|null
*/
public static function getFiles(string $zipFile): ?array
{
$zip = new ZipArchive;
$result = $zip->open($zipFile);
if ($result !== true) {
return null;
}
$files = [];
for($index = 0; $index < $zip->numFiles; $index++) {
$files[] = $zip->getNameIndex($index);
}
return $files;
}
}