diff --git a/src/ContainerProvider.php b/src/ContainerProvider.php index d37dad9..0f4b533 100644 --- a/src/ContainerProvider.php +++ b/src/ContainerProvider.php @@ -45,6 +45,7 @@ public function __construct( Method\Command\GnuGzip::getClass(), Method\Command\GnuTar::getClass(), Method\Command\Unrar::getClass(), + Method\Command\Unshar::getClass(), Method\Command\Unzip::getClass(), Method\Command\x7zip::getClass(), Method\Command\Xz::getClass(), diff --git a/src/Format/Simple/Cpio.php b/src/Format/Simple/Cpio.php index 6b56902..19a8ed3 100644 --- a/src/Format/Simple/Cpio.php +++ b/src/Format/Simple/Cpio.php @@ -21,7 +21,7 @@ class Cpio extends AbstractFormat */ public function getCompressionRatioLevel() { - return FormatInterface::RATIO_LEVEL_MIDDLE; + return FormatInterface::RATIO_LEVEL_LOWEST; } /** diff --git a/src/Format/Simple/Shar.php b/src/Format/Simple/Shar.php index ed02489..5870601 100644 --- a/src/Format/Simple/Shar.php +++ b/src/Format/Simple/Shar.php @@ -21,7 +21,7 @@ class Shar extends AbstractFormat */ public function getCompressionRatioLevel() { - return FormatInterface::RATIO_LEVEL_MIDDLE; + return FormatInterface::RATIO_LEVEL_LOWEST; } /** diff --git a/src/Method/Command/Unshar.php b/src/Method/Command/Unshar.php new file mode 100644 index 0000000..c281a60 --- /dev/null +++ b/src/Method/Command/Unshar.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Distill\Method\Command; + +use Distill\Exception; +use Distill\Format; + +/** + * Extracts files from shar archives. + * + * @author Raul Fraile + */ +class Unshar extends AbstractCommandMethod +{ + + /** + * {@inheritdoc} + */ + public function extract($file, $target, Format\FormatInterface $format) + { + $this->checkSupport($format); + + $this->getFilesystem()->mkdir($target); + + $command = sprintf('cd %s && sh %s', escapeshellarg($target), escapeshellarg($file)); + + $exitCode = $this->executeCommand($command); + + return $this->isExitCodeSuccessful($exitCode); + } + + /** + * {@inheritdoc} + */ + public function isSupported() + { + if (null === $this->supported) { + $this->supported = $this->existsCommand('sh'); + } + + return $this->supported; + } + + /** + * {@inheritdoc} + */ + public static function getClass() + { + return get_class(); + } + + /** + * {@inheritdoc} + */ + public function isFormatSupported(Format\FormatInterface $format = null) + { + return $format instanceof Format\Simple\Shar; + } +} diff --git a/tests/Format/Simple/SharTest.php b/tests/Format/Simple/SharTest.php new file mode 100644 index 0000000..508e23b --- /dev/null +++ b/tests/Format/Simple/SharTest.php @@ -0,0 +1,19 @@ +format = new Shar(); + } + + public function testCompressionRatioLevelIsValid() + { + $this->assertLevelValid($this->format->getCompressionRatioLevel()); + } +} diff --git a/tests/Method/Command/UnsharTest.php b/tests/Method/Command/UnsharTest.php new file mode 100644 index 0000000..49ef826 --- /dev/null +++ b/tests/Method/Command/UnsharTest.php @@ -0,0 +1,34 @@ +method = new Method\Command\Unshar(); + + if (false === $this->method->isSupported()) { + $this->markTestSkipped('The unshar command is not installed'); + } + + parent::setUp(); + } + + public function testExtractCorrectSharFile() + { + $target = $this->getTemporaryPath(); + $this->clearTemporaryPath(); + + $response = $this->extract('file_ok.shar', $target, new Format\Simple\Shar()); + + $this->assertTrue($response); + $this->assertUncompressed($target, 'file_ok.shar'); + $this->clearTemporaryPath(); + } + +} diff --git a/tests/Resources/files/file_fake.shar b/tests/Resources/files/file_fake.shar new file mode 100644 index 0000000..fc15c24 Binary files /dev/null and b/tests/Resources/files/file_fake.shar differ diff --git a/tests/Resources/files/file_ok.shar b/tests/Resources/files/file_ok.shar new file mode 100644 index 0000000..13ba38b --- /dev/null +++ b/tests/Resources/files/file_ok.shar @@ -0,0 +1,25 @@ +# This is a shell archive. Save it in a file, remove anything before +# this line, and then unpack it by entering "sh file". Note, it may +# create directories; files and directories will be owned by you and +# have default permissions. +# +# This archive contains: +# +# 1.txt +# 2.txt +# 3.txt +# +echo x - 1.txt +sed 's/^X//' >1.txt << 'END-of-1.txt' +X1.txt file +END-of-1.txt +echo x - 2.txt +sed 's/^X//' >2.txt << 'END-of-2.txt' +X2.txt file +END-of-2.txt +echo x - 3.txt +sed 's/^X//' >3.txt << 'END-of-3.txt' +X3.txt file +END-of-3.txt +exit + diff --git a/tests/Resources/files/file_ok.shar.key b/tests/Resources/files/file_ok.shar.key new file mode 100644 index 0000000..0f0245c --- /dev/null +++ b/tests/Resources/files/file_ok.shar.key @@ -0,0 +1,3 @@ +/1.txt|1.txt file +/2.txt|2.txt file +/3.txt|3.txt file \ No newline at end of file diff --git a/tests/Resources/scripts/main.sh b/tests/Resources/scripts/main.sh index 9dfcfa8..7950c66 100755 --- a/tests/Resources/scripts/main.sh +++ b/tests/Resources/scripts/main.sh @@ -30,4 +30,6 @@ $DIR/epub.sh $DIR/jar.sh $DIR/dmg.sh $DIR/iso.sh -$DIR/ar+deb.sh \ No newline at end of file +$DIR/ar+deb.sh +$DIR/cpio.sh +$DIR/shar.sh \ No newline at end of file diff --git a/tests/Resources/scripts/shar.sh b/tests/Resources/scripts/shar.sh new file mode 100755 index 0000000..666deff --- /dev/null +++ b/tests/Resources/scripts/shar.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +################################################################################ +# Initial configuration +################################################################################ +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +FILES_DIR="$DIR/../files" + +################################################################################ +# Clean files +################################################################################ +rm -f $FILES_DIR/file_ok.shar $FILES_DIR/file_fake.shar + +################################################################################ +# Generate files +################################################################################ + +# shar: fake file +dd if=/dev/urandom of=$FILES_DIR/file_fake.shar bs=1 count=1240 + +# shar: regular file +cd $FILES_DIR/uncompressed +shar 1.txt 2.txt 3.txt > ../file_ok.shar +printf "\1.txt|1.txt file\n\2.txt|2.txt file\n\3.txt|3.txt file" > ../file_ok.shar.key