|
1
|
|
|
<?php |
|
2
|
|
|
namespace wapmorgan\UnifiedArchive; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use wapmorgan\UnifiedArchive\Drivers\BasicDriver; |
|
6
|
|
|
use wapmorgan\UnifiedArchive\UnifiedArchive; |
|
7
|
|
|
|
|
8
|
|
|
class CamApplication { |
|
9
|
|
|
/** |
|
10
|
|
|
* @param $file |
|
11
|
|
|
* @param null $password |
|
|
|
|
|
|
12
|
|
|
* @return UnifiedArchive |
|
13
|
|
|
* @throws Exceptions\UnsupportedOperationException |
|
14
|
|
|
* @throws Exception |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function open($file, $password = null) |
|
17
|
|
|
{ |
|
18
|
|
|
if (!UnifiedArchive::canOpen($file)) |
|
19
|
|
|
throw new Exception('Could not open archive '.$file.'. Try installing suggested packages or run `cam -f` to see formats support.'); |
|
20
|
|
|
|
|
21
|
|
|
$archive = UnifiedArchive::open($file, $password); |
|
22
|
|
|
if ($archive === null) |
|
23
|
|
|
throw new Exception('Could not open archive '.$file); |
|
24
|
|
|
|
|
25
|
|
|
return $archive; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
public function checkFormats() |
|
32
|
|
|
{ |
|
33
|
|
|
echo "format\topen\tstream\tcreate\tappend\tupdate\tencrypt\tdrivers".PHP_EOL; |
|
34
|
|
|
foreach(Formats::getFormatsReport() as $format => $config) { |
|
35
|
|
|
echo $format."\t" |
|
36
|
|
|
.($config['open'] ? '+' : '-')."\t" |
|
37
|
|
|
.($config['stream'] ? '+' : '-')."\t" |
|
38
|
|
|
.($config['create'] ? '+' : '-')."\t" |
|
39
|
|
|
.($config['append'] ? '+' : '-')."\t" |
|
40
|
|
|
.($config['update'] ? '+' : '-')."\t" |
|
41
|
|
|
.($config['encrypt'] ? '+' : '-')."\t" |
|
42
|
|
|
.implode(', ', array_map(function($val) { return substr($val, strrpos($val, '\\') + 1); }, $config['drivers'])).PHP_EOL; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function checkDrivers() |
|
47
|
|
|
{ |
|
48
|
|
|
$notInstalled = []; |
|
49
|
|
|
|
|
50
|
|
|
/** @var BasicDriver $driverClass */ |
|
51
|
|
|
$i = 1; |
|
52
|
|
|
foreach (Formats::$drivers as $driverClass) { |
|
53
|
|
|
$description = $driverClass::getDescription(); |
|
54
|
|
|
$install = $driverClass::getInstallationInstruction(); |
|
55
|
|
|
if (!empty($install)) { |
|
56
|
|
|
$notInstalled[] = [$driverClass, $description, $install]; |
|
57
|
|
|
} else { |
|
58
|
|
|
echo ($i++) . '. ' . $driverClass . ' - ' . $description . PHP_EOL; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (!empty($notInstalled)) { |
|
63
|
|
|
echo PHP_EOL.'Not installed:'.PHP_EOL; |
|
64
|
|
|
$i = 1; |
|
65
|
|
|
foreach ($notInstalled as $data) { |
|
66
|
|
|
echo ($i++) . '. ' . $data[0] . ' - ' . $data[1] . PHP_EOL |
|
67
|
|
|
. '- ' . $data[2] . PHP_EOL.PHP_EOL; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param $args |
|
74
|
|
|
* @throws Exception |
|
75
|
|
|
* @throws \Archive7z\Exception |
|
76
|
|
|
*/ |
|
77
|
|
|
public function listArray($args) |
|
78
|
|
|
{ |
|
79
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
80
|
|
|
foreach ($archive->getFileNames() as $file) { |
|
81
|
|
|
echo $file.PHP_EOL; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $args |
|
87
|
|
|
* @throws Exception |
|
88
|
|
|
* @throws \Archive7z\Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
public function table($args) |
|
91
|
|
|
{ |
|
92
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
93
|
|
|
|
|
94
|
|
|
echo sprintf('%51s | %4s | %-18s'.PHP_EOL, 'File name', 'Size', 'Date'); |
|
95
|
|
|
echo str_repeat('-', 80).PHP_EOL; |
|
96
|
|
|
foreach ($archive->getFileNames() as $file) { |
|
97
|
|
|
$info = $archive->getFileData($file); |
|
98
|
|
|
$size = $this->formatSize($info->uncompressedSize); |
|
99
|
|
|
$file_name = strlen($file) > 51 ? substr($file, 0, 49).'..' : $file; |
|
100
|
|
|
echo sprintf('%-51s | %1.1f%s | %18s'.PHP_EOL, |
|
101
|
|
|
$file_name, |
|
102
|
|
|
$size[0], |
|
103
|
|
|
$size[1], |
|
104
|
|
|
$this->formatDate($info->modificationTime) |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
$size = $this->formatSize($archive->countUncompressedFilesSize()); |
|
108
|
|
|
$packed_size = $this->formatSize($archive->countCompressedFilesSize()); |
|
109
|
|
|
echo str_repeat('-', 80).PHP_EOL; |
|
110
|
|
|
echo sprintf('%51s | %1.1f%s | %1.1f%s'.PHP_EOL, 'Total '.$archive->countFiles().' file(s)', $size[0], $size[1], $packed_size[0], $packed_size[1]); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param $bytes |
|
116
|
|
|
* @param int $precision |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function formatSize($bytes, $precision = 1) |
|
120
|
|
|
{ |
|
121
|
|
|
$units = ['b', 'k', 'm', 'g', 't']; |
|
122
|
|
|
|
|
123
|
|
|
$bytes = max($bytes, 0); |
|
124
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
|
125
|
|
|
$pow = min($pow, count($units) - 1); |
|
126
|
|
|
$bytes /= pow(1024, $pow); |
|
127
|
|
|
$i = round($bytes, $precision); |
|
128
|
|
|
if ($precision == 1 && $i >= 10) { |
|
129
|
|
|
$i = round($i / 1024, 1); |
|
130
|
|
|
$pow++; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return [$i, $units[$pow]]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param $unixtime |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
* @throws \Exception |
|
141
|
|
|
*/ |
|
142
|
|
|
public function formatDate($unixtime) |
|
143
|
|
|
{ |
|
144
|
|
|
if (strtotime('today') < $unixtime) |
|
145
|
|
|
return 'Today, '.date('G:m', $unixtime); |
|
146
|
|
|
else if (strtotime('yesterday') < $unixtime) |
|
147
|
|
|
return 'Yesterday, '.date('G:m', $unixtime); |
|
148
|
|
|
else { |
|
149
|
|
|
$datetime = new \DateTime(); |
|
150
|
|
|
$datetime->setTimestamp($unixtime); |
|
151
|
|
|
if ($datetime->format('Y') == date('Y')) |
|
152
|
|
|
return $datetime->format('d M, G:m'); |
|
153
|
|
|
else |
|
154
|
|
|
return $datetime->format('d M Y, G:m'); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $args |
|
160
|
|
|
* @throws Exception |
|
161
|
|
|
* @throws \Archive7z\Exception |
|
162
|
|
|
*/ |
|
163
|
|
|
public function info($args) |
|
164
|
|
|
{ |
|
165
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
166
|
|
|
echo 'Archive type: '.$archive->getFormat().PHP_EOL; |
|
167
|
|
|
echo 'Archive changed: '.$this->formatDate(filemtime($args['ARCHIVE'])).PHP_EOL; |
|
168
|
|
|
echo 'Archive contains: '.$archive->countFiles().' file'.($archive->countFiles() > 1 ? 's' : null).PHP_EOL; |
|
169
|
|
|
echo 'Archive compressed size: '.implode(' ', $this->formatSize($archive->countCompressedFilesSize(), 2)).PHP_EOL; |
|
170
|
|
|
echo 'Archive uncompressed size: '.implode(' ', $this->formatSize($archive->countUncompressedFilesSize(), 2)).PHP_EOL; |
|
171
|
|
|
echo 'Archive compression ratio: '.round($archive->countUncompressedFilesSize() / $archive->countCompressedFilesSize(), 6).'/1 ('.floor($archive->countCompressedFilesSize() / $archive->countUncompressedFilesSize() * 100).'%)'.PHP_EOL; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @param $args |
|
176
|
|
|
* @throws Exception |
|
177
|
|
|
* @throws \Archive7z\Exception |
|
178
|
|
|
*/ |
|
179
|
|
|
public function extract($args) |
|
180
|
|
|
{ |
|
181
|
|
|
$archive = $this->open($args['ARCHIVE'], isset($args['--password']) ? $args['--password'] : null); |
|
182
|
|
|
$output = getcwd(); |
|
183
|
|
|
if (isset($args['--output'])) { |
|
184
|
|
|
if (!is_dir($args['--output'])) |
|
185
|
|
|
mkdir($args['--output']); |
|
186
|
|
|
$output = realpath($args['--output']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (empty($args['FILES_IN_ARCHIVE']) || $args['FILES_IN_ARCHIVE'] == array('/') || $args['FILES_IN_ARCHIVE'] == array('*')) { |
|
190
|
|
|
$result = $archive->extractFiles($output); |
|
191
|
|
|
if ($result === false) echo 'Error occured'.PHP_EOL; |
|
|
|
|
|
|
192
|
|
|
else echo 'Extracted '.$result.' file(s) to '.$output.PHP_EOL; |
|
193
|
|
|
} else { |
|
194
|
|
|
$extracted = 0; |
|
195
|
|
|
$errored = []; |
|
196
|
|
|
foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
|
197
|
|
|
$result = $archive->extractFiles($output, $file); |
|
198
|
|
|
if ($result === false) $errored[] = $file; |
|
199
|
|
|
else $extracted += $result; |
|
200
|
|
|
} |
|
201
|
|
|
if (!empty($errored)) echo 'Errored: '.implode(', ', $errored).PHP_EOL; |
|
202
|
|
|
if ($extracted > 0) echo 'Exctracted '.$extracted.' file(s) to '.$output.PHP_EOL; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param $args |
|
208
|
|
|
* @throws Exception |
|
209
|
|
|
* @throws \Archive7z\Exception |
|
210
|
|
|
*/ |
|
211
|
|
|
public function printFile($args) |
|
212
|
|
|
{ |
|
213
|
|
|
$archive = $this->open($args['ARCHIVE'], isset($args['--password']) ? $args['--password'] : null); |
|
214
|
|
|
foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
|
215
|
|
|
if (!$archive->hasFile($file)) { |
|
216
|
|
|
echo 'File '.$file.' IS NOT PRESENT'.PHP_EOL; |
|
217
|
|
|
exit(-1); |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
// $info = $archive->getFileData($file); |
|
220
|
|
|
// echo 'File content: '.$file.' (size is '.implode('', $this->formatSize($info->uncompressedSize, 1)).')'.PHP_EOL; |
|
221
|
|
|
echo $archive->getFileContent($file); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param $args |
|
227
|
|
|
* @throws Exception |
|
228
|
|
|
* @throws \Archive7z\Exception |
|
229
|
|
|
*/ |
|
230
|
|
|
public function details($args) |
|
231
|
|
|
{ |
|
232
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
233
|
|
|
foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
|
234
|
|
|
$info = $archive->getFileData($file); |
|
235
|
|
|
if ($info === false) { |
|
236
|
|
|
echo 'File '.$file.' IS NOT PRESENT'.PHP_EOL; |
|
237
|
|
|
continue; |
|
238
|
|
|
} |
|
239
|
|
|
echo 'File name : '.$file.PHP_EOL; |
|
240
|
|
|
echo 'Compressed size : '.implode('', $this->formatSize($info->compressedSize, 2)).PHP_EOL; |
|
241
|
|
|
echo 'Uncompressed size: '.implode('', $this->formatSize($info->uncompressedSize, 2)).PHP_EOL; |
|
242
|
|
|
echo 'Is compressed : '.($info->isCompressed ? 'yes' : 'no').PHP_EOL; |
|
243
|
|
|
echo 'Date modification: '.$this->formatDate($info->modificationTime).PHP_EOL; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param $args |
|
249
|
|
|
* @throws Exception |
|
250
|
|
|
* @throws \Archive7z\Exception |
|
251
|
|
|
*/ |
|
252
|
|
|
public function delete($args) |
|
253
|
|
|
{ |
|
254
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
255
|
|
|
$files = $archive->getFileNames(); |
|
256
|
|
|
foreach ($args['FILES_IN_ARCHIVE'] as $file) { |
|
257
|
|
|
if (!in_array($file, $files)) { |
|
258
|
|
|
echo 'File '.$file.' is NOT in archive'.PHP_EOL; |
|
259
|
|
|
continue; |
|
260
|
|
|
} |
|
261
|
|
|
if ($archive->deleteFiles($file) === false) |
|
262
|
|
|
echo 'Error file '.$file.PHP_EOL; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param $args |
|
268
|
|
|
* @throws Exception |
|
269
|
|
|
* @throws \Archive7z\Exception |
|
270
|
|
|
*/ |
|
271
|
|
|
public function add($args) |
|
272
|
|
|
{ |
|
273
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
274
|
|
|
$added_files = $archive->addFiles($args['FILES_ON_DISK']); |
|
275
|
|
|
if ($added_files === false) |
|
276
|
|
|
echo 'Error'.PHP_EOL; |
|
277
|
|
|
else |
|
278
|
|
|
echo 'Added '.$added_files.' file(s)'.PHP_EOL; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param $args |
|
283
|
|
|
* @throws Exception |
|
284
|
|
|
* @throws \Archive7z\Exception |
|
285
|
|
|
*/ |
|
286
|
|
|
public function addFromStdin($args) |
|
287
|
|
|
{ |
|
288
|
|
|
$archive = $this->open($args['ARCHIVE']); |
|
289
|
|
|
$content = null; |
|
290
|
|
|
while (!feof(STDIN)) { |
|
291
|
|
|
$content .= fgets(STDIN); |
|
292
|
|
|
} |
|
293
|
|
|
$len = strlen($content); |
|
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
$added_files = $archive->addFileFromString($args['FILE_IN_ARCHIVE'], $content); |
|
296
|
|
|
if ($added_files === false) |
|
297
|
|
|
echo 'Error'.PHP_EOL; |
|
298
|
|
|
else { |
|
299
|
|
|
$size = $this->formatSize($len); |
|
300
|
|
|
echo sprintf('Added %s(%1.1f%s) file to %s', |
|
301
|
|
|
$args['FILE_IN_ARCHIVE'], $size[0], $size[1], $args['ARCHIVE']) . PHP_EOL; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param $args |
|
307
|
|
|
* @throws Exception |
|
308
|
|
|
*/ |
|
309
|
|
|
public function create($args) |
|
310
|
|
|
{ |
|
311
|
|
|
$password = isset($args['--password']) ? $args['--password'] : null; |
|
312
|
|
|
if (file_exists($args['ARCHIVE'])) { |
|
313
|
|
|
if (is_dir($args['ARCHIVE'])) |
|
314
|
|
|
echo $args['ARCHIVE'].' is a directory!'.PHP_EOL; |
|
315
|
|
|
else { |
|
316
|
|
|
echo 'File '.$args['ARCHIVE'].' already exists!'.PHP_EOL; |
|
317
|
|
|
} |
|
318
|
|
|
} else { |
|
319
|
|
|
$files = []; |
|
320
|
|
|
$is_absolute = $args['--path'] === 'absolute'; |
|
321
|
|
|
|
|
322
|
|
|
foreach ($args['FILES_ON_DISK'] as $i => $file) { |
|
323
|
|
|
$file = realpath($file); |
|
324
|
|
|
if ($is_absolute) { |
|
325
|
|
|
$files[] = $file; |
|
326
|
|
|
} else { |
|
327
|
|
|
$files[basename($file)] = $file; |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
$archived_files = UnifiedArchive::archiveFiles($files, $args['ARCHIVE'], BasicDriver::COMPRESSION_AVERAGE, $password); |
|
332
|
|
|
if ($archived_files === false) |
|
|
|
|
|
|
333
|
|
|
echo 'Error'.PHP_EOL; |
|
334
|
|
|
else |
|
335
|
|
|
echo 'Created archive ' . $args['ARCHIVE'] . ' with ' . $archived_files . ' file(s) of total size ' . implode('', $this->formatSize(filesize($args['ARCHIVE']))) . PHP_EOL; |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
public function createFake($args) |
|
340
|
|
|
{ |
|
341
|
|
|
$files = []; |
|
342
|
|
|
$is_absolute = $args['--path'] === 'absolute'; |
|
343
|
|
|
|
|
344
|
|
|
foreach ($args['FILES_ON_DISK'] as $i => $file) { |
|
345
|
|
|
$file = realpath($file); |
|
346
|
|
|
if ($is_absolute) { |
|
347
|
|
|
$files[] = $file; |
|
348
|
|
|
} else { |
|
349
|
|
|
$files[basename($file)] = $file; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
var_dump(UnifiedArchive::prepareForArchiving($files, $args['ARCHIVE'])); |
|
|
|
|
|
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|