1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SergioDaniloJr\Zipper; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Zipper |
9
|
|
|
* @package SergioDaniloJr\Zipper |
10
|
|
|
*/ |
11
|
|
|
class Zipper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \ZipArchive |
15
|
|
|
*/ |
16
|
|
|
private $zip; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $file; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $message; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Default Extension |
35
|
|
|
*/ |
36
|
|
|
const DEFAULT_EXTENSION = "zip"; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Zipper constructor. |
40
|
|
|
* @param string|null $pathSave |
41
|
|
|
*/ |
42
|
|
|
public function __construct(string $pathSave = null) |
43
|
|
|
{ |
44
|
|
|
$this->zip = new \ZipArchive(); |
45
|
|
|
$this->path = $pathSave; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function message(): string |
52
|
|
|
{ |
53
|
|
|
return $this->message; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function file() |
60
|
|
|
{ |
61
|
|
|
return $this->file; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $file |
66
|
|
|
* @return string|null |
67
|
|
|
*/ |
68
|
|
|
public function zipFile(string $file): ?string |
69
|
|
|
{ |
70
|
|
|
if (!is_null($dataFile = $this->dataFile($file))) { |
71
|
|
|
|
72
|
|
|
$path = $this->destiny($file); |
73
|
|
|
if ($this->zip->open($path, $this->zip::CREATE) === true) { |
74
|
|
|
$bool = $this->zip->addFile($file, $dataFile->basename); |
75
|
|
|
$this->zip->close(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (isset($bool) && $bool === true) { |
79
|
|
|
$this->file = $path; |
80
|
|
|
return $this->file; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
$this->message = "Algo deu errado ao Zipar!"; |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $files |
93
|
|
|
* @param string $nameFile |
94
|
|
|
* @param string|null $path |
95
|
|
|
* @return string|null |
96
|
|
|
*/ |
97
|
|
|
public function zipFiles(array $files, string $nameFile, string $path = null): ?string |
98
|
|
|
{ |
99
|
|
|
$path = ($this->path ?? $path); |
100
|
|
|
if (!$path) { |
101
|
|
|
$this->message = "Forneça um Caminho para Salvar o Arquivo!"; |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$nameFile = $nameFile . "." . self::DEFAULT_EXTENSION; |
106
|
|
|
$pathSave = $this->checkDir($path) . "/" . $nameFile; |
107
|
|
|
|
108
|
|
|
if (!empty($files) && is_array($files) && $this->zip->open($pathSave, $this->zip::CREATE) === true) { |
109
|
|
|
foreach ($files as $file) { |
110
|
|
|
if ($dataFile = $this->dataFile($file)) { |
111
|
|
|
$this->zip->addFile($file, $dataFile->basename); |
112
|
|
|
} else { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
$this->zip->close(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->file = $pathSave; |
120
|
|
|
return $this->file(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $file |
125
|
|
|
* @param string|null $destiny |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function extract(string $file, string $destiny = null): ?string |
129
|
|
|
{ |
130
|
|
|
if (!is_null($dataFile = $this->dataFile($file))) { |
131
|
|
|
|
132
|
|
|
$destiny = (!is_null($destiny) ? $this->checkDir($destiny) : $this->checkDir($dataFile->dirname)); |
133
|
|
|
|
134
|
|
|
if ($this->zip->open($file) === true) { |
135
|
|
|
$this->zip->extractTo($destiny); |
136
|
|
|
$this->zip->close(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $destiny; |
140
|
|
|
} |
141
|
|
|
$this->message = "Algo Impossibilitou a Extração do Arquivo!"; |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $path |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function download($path): bool |
150
|
|
|
{ |
151
|
|
|
$fileZip = $this->zipFile($path); |
152
|
|
|
|
153
|
|
|
if (file_exists($fileZip) && !is_dir($fileZip) && !is_null($fileZip)) { |
154
|
|
|
$fileName = pathinfo($fileZip)["basename"]; |
155
|
|
|
header("Content-Type: application/zip"); |
156
|
|
|
header('Content-Length:' . filesize($fileZip)); |
157
|
|
|
header('Content-Disposition: attachment; filename="' . $fileName . '"'); |
158
|
|
|
readfile($fileZip); |
159
|
|
|
unlink($fileZip); |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $path |
168
|
|
|
* @return object|null |
169
|
|
|
*/ |
170
|
|
|
private function dataFile(string $path): ?object |
171
|
|
|
{ |
172
|
|
|
if (!is_file($path)) { |
173
|
|
|
$this->message = "Forneça um Arquivo Válido!"; |
174
|
|
|
return null; |
175
|
|
|
} else { |
176
|
|
|
$pathinfo = pathinfo($path); |
177
|
|
|
$dataFile = new \stdClass(); |
178
|
|
|
$dataFile->dirname = $pathinfo["dirname"]; |
179
|
|
|
$dataFile->basename = $pathinfo["basename"]; |
180
|
|
|
$dataFile->extension = $pathinfo["extension"]; |
181
|
|
|
$dataFile->filename = $pathinfo["filename"]; |
182
|
|
|
|
183
|
|
|
return $dataFile; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $file |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
private function destiny(string $file): string |
193
|
|
|
{ |
194
|
|
|
if (!is_null($dataFile = $this->dataFile($file)) && !is_null($this->path)) { |
195
|
|
|
$pathSave = $this->checkDir($this->path) . "/" . $this->setFileName($file) . "." . self::DEFAULT_EXTENSION; |
196
|
|
|
} else { |
197
|
|
|
$pathSave = $dataFile->dirname . "/" . $this->setFileName($file) . "." . self::DEFAULT_EXTENSION; |
198
|
|
|
} |
199
|
|
|
$this->path = $pathSave; |
200
|
|
|
return $this->path; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $file |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
private function setFileName($file): string |
208
|
|
|
{ |
209
|
|
|
$filename = $this->dataFile($file)->filename . "-" . (string) time(); |
210
|
|
|
return $filename; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $path |
215
|
|
|
* @return string|null |
216
|
|
|
*/ |
217
|
|
|
private function checkDir(string $path): ?string |
218
|
|
|
{ |
219
|
|
|
if (is_dir($path)) { |
220
|
|
|
$this->path = $path; |
221
|
|
|
return $this->path; |
222
|
|
|
} |
223
|
|
|
//Caso a Pasta não exista, tem que criar um método para criar a pasta solicitada |
224
|
|
|
return $this->setDir($path); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $path |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
private function setDir(string $path): string |
233
|
|
|
{ |
234
|
|
|
if (!is_dir($path) && !is_file($path)) { |
235
|
|
|
mkdir($path, 777, true); |
236
|
|
|
} |
237
|
|
|
$this->path = $path; |
238
|
|
|
return $this->path; |
239
|
|
|
} |
240
|
|
|
} |