1: <?php
2:
3: namespace ngatngay;
4:
5: use ZipArchive;
6: use SplFileInfo;
7:
8: class zip extends ZipArchive {
9: public function add(string $path, ?string $relative = null): bool
10: {
11: if (!file_exists($path)) {
12: return false;
13: }
14:
15: $file = new SplFileInfo($path);
16: $path = $file->getPathname();
17: $pathRelative = $path;
18:
19: if ($relative) {
20: $pathRelative = substr($path, strlen($relative));
21: }
22:
23: if ($file->isFile()) {
24: $this->addFile($path, $pathRelative);
25: }
26:
27: if ($file->isDir()) {
28: $this->addEmptyDir($pathRelative);
29: }
30:
31: return true;
32: }
33: }
34: