This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZipArchive.php
130 lines (109 loc) · 2.67 KB
/
ZipArchive.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
<?php declare (strict_types = 1);
namespace Wavevision\Utils\Zip;
use Nette\FileNotFoundException;
use Nette\SmartObject;
use SplFileInfo;
use Wavevision\Utils\FileInfo;
use Wavevision\Utils\Finder;
use Wavevision\Utils\Path;
use ZipArchive as Zip;
use function basename;
use function file_exists;
use function is_dir;
use function is_file;
class ZipArchive
{
use SmartObject;
private const INITIAL_DEPTH = 1;
/**
* @var ZipArchiveItem[]
*/
private array $items;
private string $path;
private Zip $zip;
public function __construct(string $path, ZipArchiveItem ...$items)
{
$this->items = $items;
$this->path = $path;
$this->zip = new Zip();
}
public function addItem(ZipArchiveItem $item): self
{
$this->items[] = $item;
return $this;
}
public function close(): self
{
$this->zip->close();
return $this;
}
public function compress(): self
{
foreach ($this->items as $item) {
$path = $this->getItemPath($item);
if (is_dir($path)) {
$this->addDir($item, self::INITIAL_DEPTH);
} else {
$this->zip->addFile($path, $item->getName());
}
}
return $this->close();
}
public function extract(?string $dir = null): self
{
$this->zip->extractTo($dir ?? $this->getExtractDir());
return $this;
}
public function getName(): string
{
return basename($this->getPath());
}
public function getPath(): string
{
return $this->path;
}
public function read(): self
{
$this->zip->open($this->getPath());
return $this;
}
public function write(): self
{
$flag = is_file($this->getPath()) ? Zip::OVERWRITE : Zip::CREATE;
$this->zip->open($this->getPath(), $flag);
return $this;
}
/**
* @param string[] $parents
*/
private function addDir(ZipArchiveItem $item, int $depth, array $parents = []): void
{
$deep = $depth > self::INITIAL_DEPTH;
$dir = $item->getName();
$parents = [...$parents, $dir];
$this->zip->addEmptyDir($deep ? Path::join(...$parents) : $dir);
/** @var SplFileInfo $subItem */
foreach (Finder::find('*')->in($item->getPath()) as $subItem) {
$path = $subItem->getPathname();
if ($subItem->isDir()) {
$this->addDir(new ZipArchiveItem($path), $depth + 1, $parents);
} else {
$name = $subItem->getFilename();
$this->zip->addFile($path, $deep ? Path::join(...[...$parents, $name]) : $name);
}
}
}
private function getExtractDir(): string
{
$fileInfo = new FileInfo($this->getPath());
return Path::join($fileInfo->getDirName(), $fileInfo->getBaseName(true));
}
private function getItemPath(ZipArchiveItem $item): string
{
$path = $item->getPath();
if (!file_exists($path)) {
throw new FileNotFoundException("Zip archive item '$path' not found.");
}
return $path;
}
}