1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the alphaz Framework. |
4
|
|
|
* |
5
|
|
|
* @author Muhammad Umer Farooq (Malik) <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/alphazframework/framework |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
* |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace alphaz\Archive\Adapter; |
18
|
|
|
|
19
|
|
|
class Zip implements AdapterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Open and extract the archive. |
23
|
|
|
* |
24
|
|
|
* @param (string) $file The file that you want uncompress/open. |
25
|
|
|
* @param (string) $target Where to extract the file. |
26
|
|
|
* @param (bool) $delete True to delete the file; False to not delete it. |
27
|
|
|
* |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
* |
30
|
|
|
* @return bool True when succeeded; False when failed. |
31
|
|
|
*/ |
32
|
|
|
public function extract(string $file = '', string $target = '', bool $delete = false): bool |
33
|
|
|
{ |
34
|
|
|
// Guard against missing classes. |
35
|
|
|
if (!class_exists('\ZipArchive')) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$zip = new \ZipArchive(); |
40
|
|
|
$x = ($file !== '' && $zip->open($file)); |
41
|
|
|
|
42
|
|
|
// Return false here if the file failed to open. |
43
|
|
|
if ($x !== true) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$zip->extractTo($target); |
48
|
|
|
$zip->close(); |
49
|
|
|
if ($delete === true) { |
50
|
|
|
unlink($file); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Success. :-) |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Compress the file to an archive. |
59
|
|
|
* |
60
|
|
|
* @param (string|array) $files The files that you want to compress. |
61
|
|
|
* @param (string) $destination The file destination. |
62
|
|
|
* @param (bool) $overwrite True to delete the file; False to not delete it. |
63
|
|
|
* |
64
|
|
|
* @since 1.0.0 |
65
|
|
|
* |
66
|
|
|
* @return bool True when succeeded; False when failed. |
67
|
|
|
*/ |
68
|
|
|
public function compress($files, string $destination = '', bool $overwrite = false): bool |
69
|
|
|
{ |
70
|
|
|
// Guard against missing classes. |
71
|
|
|
if (!class_exists('\ZipArchive')) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Return false immediately if files isn't a string, isn't an array, or is an empty array. |
76
|
|
|
if ((!is_string($files) && !is_array($files)) || (is_array($files) && !count($files))) { |
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// If the destination already exists and overwrite is false, return false. |
81
|
|
|
if (file_exists($destination) && !$overwrite) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$valid_files = []; |
86
|
|
|
|
87
|
|
|
// Cycle through each file. |
88
|
|
|
if (is_string($files) && $files !== '') { |
89
|
|
|
if (is_readable($files)) { |
90
|
|
|
$valid_files[] = $files; |
91
|
|
|
} |
92
|
|
|
} elseif (is_array($files)) { |
93
|
|
|
foreach ($files as $file) { |
94
|
|
|
if (!empty($file) && is_readable($file)) { |
95
|
|
|
$valid_files[] = $file; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Return false immediately if we don't have any good files. |
101
|
|
|
if (!count($valid_files)) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Create the archive. |
106
|
|
|
$zip = new \ZipArchive(); |
107
|
|
|
if ($zip->open($destination, $overwrite ? \ZipArchive::OVERWRITE : \ZipArchive::CREATE) !== true) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Add the files. |
112
|
|
|
foreach ($valid_files as $file) { |
113
|
|
|
if ((($Del = strrpos($file, '\\')) !== false) || ($Del = strrpos($file, '/')) !== false) { |
114
|
|
|
$Safe = substr($file, $Del + 1); |
115
|
|
|
} else { |
116
|
|
|
$Safe = $file; |
117
|
|
|
} |
118
|
|
|
$zip->addFile($file, $Safe); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Close the zip -- done! |
122
|
|
|
$zip->close(); |
123
|
|
|
|
124
|
|
|
// Check to make sure the file exists. |
125
|
|
|
return file_exists($destination); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|