-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patharchive.php
40 lines (29 loc) · 1.39 KB
/
archive.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
<?php
// this example shows how the containerArchiveStream() method returns a TAR stream,
// how it can be passed to a TAR decoder and how we can then pipe each
// individual file to the console output.
require __DIR__ . '/../vendor/autoload.php';
$container = isset($argv[1]) ? $argv[1] : 'asd';
$path = isset($argv[2]) ? $argv[2] : '/etc/passwd';
echo 'Container "' . $container . '" dumping "' . $path . '" (pass as arguments to this example)' . PHP_EOL;
$client = new Clue\React\Docker\Client();
$stream = $client->containerArchiveStream($container, $path);
$tar = new Clue\React\Tar\Decoder();
// use caret notation for any control characters except \t, \r and \n
$caret = new Clue\CaretNotation\Encoder("\t\r\n");
$tar->on('entry', function ($header, React\Stream\ReadableStreamInterface $file) use ($caret) {
// write each entry to the console output
echo '########## ' . $caret->encode($header['filename']) . ' ##########' . PHP_EOL;
$file->on('data', function ($chunk) use ($caret) {
echo $caret->encode($chunk);
});
});
$tar->on('error', function (Exception $e) {
// should not be invoked, unless the stream is somehow interrupted
echo 'Error in TAR stream: ' . $e->getMessage() . PHP_EOL;
});
$stream->on('error', function (Exception $e) {
// will be called if either parameter is invalid
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
$stream->pipe($tar);