4 * This file is part of Zippy.
6 * (c) Alchemy <info@alchemy.fr>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 namespace Alchemy\Zippy\Parser;
13 use Alchemy\Zippy\Exception\RuntimeException;
16 * This class is responsible of parsing GNUTar command line output
18 class GNUTarOutputParser implements ParserInterface
20 const PERMISSIONS = '([ldrwx-]+)';
21 const OWNER = '([a-z][-a-z0-9]*)';
22 const GROUP = '([a-z][-a-z0-9]*)';
23 const FILESIZE = '(\d*)';
24 const ISO_DATE = '([0-9]+-[0-9]+-[0-9]+\s+[0-9]+:[0-9]+)';
25 const FILENAME = '(.*)';
30 public function parseFileListing($output)
32 $lines = array_values(array_filter(explode("\n", $output)));
35 foreach ($lines as $line) {
38 // -rw-r--r-- gray/staff 62373 2006-06-09 12:06 apple
39 if (!preg_match_all("#".
40 self::PERMISSIONS . "\s+" . // match (-rw-r--r--)
41 self::OWNER . "/" . // match (gray)
42 self::GROUP . "\s+" . // match (staff)
43 self::FILESIZE . "\s+" . // match (62373)
44 self::ISO_DATE . "\s+" . // match (2006-06-09 12:06)
45 self::FILENAME . // match (apple)
47 $line, $matches, PREG_SET_ORDER
52 $chunks = array_shift($matches);
54 if (7 !== count($chunks)) {
58 $date = \DateTime::createFromFormat("Y-m-d H:i", $chunks[5]);
60 if (false === $date) {
61 throw new RuntimeException(sprintf('Failed to parse mtime date from %s', $line));
65 'location' => $chunks[6],
68 'is_dir' => 'd' === $chunks[1][0]
78 public function parseInflatorVersion($output)
80 $chunks = explode(' ', $output, 3);
82 if (2 > count($chunks)) {
86 list(, $version) = $chunks;
94 public function parseDeflatorVersion($output)
96 return $this->parseInflatorVersion($output);