-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZip.php
197 lines (151 loc) · 6.06 KB
/
Zip.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/*
* Fresns (https://fresns.org)
* Copyright (C) 2021-Present Jevan Tang
* Released under the Apache-2.0 License.
*/
namespace ZhenMu\Support\Utils;
use Illuminate\Support\Facades\File;
use PhpZip\ZipFile;
class Zip
{
protected $zipFile;
public function __construct()
{
$this->zipFile = new ZipFile();
}
public function fixFilesChineseName($sourcePath)
{
$encoding_list = [
"ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'
];
try {
$zip = new \ZipArchive();
$openResult = $zip->open($sourcePath);
if ($openResult !== true) {
throw new \Exception('Cannot Open zip file: ' . $sourcePath);
}
$fileNum = $zip->numFiles;
$files = [];
for ($i = 0; $i < $fileNum; $i++) {
$statInfo = $zip->statIndex($i, \ZipArchive::FL_ENC_RAW);
$encode = mb_detect_encoding($statInfo['name'], $encoding_list);
$string = mb_convert_encoding($statInfo['name'], 'UTF-8', $encode);
$zip->renameIndex($i, $string);
$newStatInfo = $zip->statIndex($i, \ZipArchive::FL_ENC_RAW);
$files[] = $newStatInfo;
}
} catch (\Throwable $e) {
throw $e;
} finally {
$zip->close();
}
return $files;
}
public function pack(string $sourcePath, ?string $filename = null, ?string $targetPath = null): ?string
{
if (!File::exists($sourcePath)) {
throw new \RuntimeException("Directory to be decompressed does not exist {$sourcePath}");
}
$filename = $filename ?? File::name($sourcePath);
$targetPath = $targetPath ?? File::dirname($sourcePath);
$targetPath = $targetPath ?: File::dirname($sourcePath);
File::ensureDirectoryExists($targetPath);
$zipFilename = str_contains($filename, '.zip') ? $filename : $filename . '.zip';
$zipFilepath = "{$targetPath}/{$zipFilename}";
while (File::exists($zipFilepath)) {
$basename = File::name($zipFilepath);
$zipCount = count(File::glob("{$targetPath}/{$basename}*.zip"));
$zipFilename = $basename . $zipCount . '.zip';
$zipFilepath = "{$targetPath}/{$zipFilename}";
}
// Compression
$this->zipFile->addDirRecursive($sourcePath, $filename);
$this->zipFile->saveAsFile($zipFilepath);
return $targetPath;
}
public function unpack(string $sourcePath, ?string $targetPath = null): ?string
{
try {
// Detects the file type and unpacks only zip files
$mimeType = File::mimeType($sourcePath);
} catch (\Throwable $e) {
\info("Unzip failed {$e->getMessage()}");
throw new \RuntimeException("Unzip failed {$e->getMessage()}");
}
// Get file types (only directories and zip files are processed)
$type = match (true) {
default => null,
str_contains($mimeType, 'directory') => 1,
str_contains($mimeType, 'zip') => 2,
};
if (is_null($type)) {
\info("unsupport mime type $mimeType");
throw new \RuntimeException("unsupport mime type $mimeType");
}
// Make sure the unzip destination directory exists
$targetPath = $targetPath ?? config('plugins.paths.unzip_target_path');
if (empty($targetPath)) {
\info('targetPath cannot be empty');
throw new \RuntimeException('targetPath cannot be empty');
}
if (!is_dir($targetPath)) {
File::ensureDirectoryExists($targetPath);
}
if ($targetPath == $sourcePath) {
return $targetPath;
}
// Empty the directory to avoid leaving files of other plugins
File::cleanDirectory($targetPath);
// Directory without unzip operation, copy the original directory to the temporary directory
if ($type == 1) {
File::copyDirectory($sourcePath, $targetPath);
// Make sure the directory decompression level is the top level of the plugin directory
$targetPath = $this->ensureDoesntHaveSubdir($targetPath);
return $targetPath;
}
if ($type == 2) {
$this->fixFilesChineseName($sourcePath);
// unzip
$zipFile = $this->zipFile->openFile($sourcePath);
$zipFile->extractTo($targetPath);
// Make sure the directory decompression level is the top level of the plugin directory
$targetPath = $this->ensureDoesntHaveSubdir($targetPath);
// Decompress to the specified directory
return $targetPath;
}
return null;
}
public function ensureDoesntHaveSubdir(string $targetPath): string
{
$targetPath = $targetPath ?? config('plugins.paths.unzip_target_path');
$pattern = sprintf('%s/*', rtrim($targetPath, DIRECTORY_SEPARATOR));
$files = [];
foreach (File::glob($pattern) as $file) {
if (str_contains($file, '__MACOSX')) {
continue;
}
$files[] = $file;
}
$fileCount = count($files);
if ($fileCount > 1) {
throw new \RuntimeException("Cannot handle the zip file, zip file count is: {$fileCount}, extract path is: {$targetPath}");
}
$tmpDir = $targetPath . '-subdir';
File::ensureDirectoryExists($tmpDir);
$firstEntryname = File::basename(current($files));
$path = $targetPath . "/{$firstEntryname}";
$tmpTargetPath = $tmpDir . "/{$firstEntryname}";
$parentDir = dirname($tmpTargetPath);
File::ensureDirectoryExists($parentDir);
if (is_dir($path)) {
File::copyDirectory($path, $tmpDir);
} else {
File::copyDirectory(dirname($path), $parentDir);
}
File::cleanDirectory($targetPath);
File::copyDirectory($tmpDir, $targetPath);
File::deleteDirectory($tmpDir);
return $targetPath;
}
}