Skip to content

Commit 0fd6a5d

Browse files
committed
Support for .shar files
1 parent 514b837 commit 0fd6a5d

File tree

11 files changed

+179
-3
lines changed

11 files changed

+179
-3
lines changed

src/ContainerProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function __construct(
4545
Method\Command\GnuGzip::getClass(),
4646
Method\Command\GnuTar::getClass(),
4747
Method\Command\Unrar::getClass(),
48+
Method\Command\Unshar::getClass(),
4849
Method\Command\Unzip::getClass(),
4950
Method\Command\x7zip::getClass(),
5051
Method\Command\Xz::getClass(),

src/Format/Simple/Cpio.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Cpio extends AbstractFormat
2121
*/
2222
public function getCompressionRatioLevel()
2323
{
24-
return FormatInterface::RATIO_LEVEL_MIDDLE;
24+
return FormatInterface::RATIO_LEVEL_LOWEST;
2525
}
2626

2727
/**

src/Format/Simple/Shar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Shar extends AbstractFormat
2121
*/
2222
public function getCompressionRatioLevel()
2323
{
24-
return FormatInterface::RATIO_LEVEL_MIDDLE;
24+
return FormatInterface::RATIO_LEVEL_LOWEST;
2525
}
2626

2727
/**

src/Method/Command/Unshar.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Distill package.
5+
*
6+
* (c) Raul Fraile <raulfraile@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Distill\Method\Command;
13+
14+
use Distill\Exception;
15+
use Distill\Format;
16+
17+
/**
18+
* Extracts files from shar archives.
19+
*
20+
* @author Raul Fraile <raulfraile@gmail.com>
21+
*/
22+
class Unshar extends AbstractCommandMethod
23+
{
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function extract($file, $target, Format\FormatInterface $format)
29+
{
30+
$this->checkSupport($format);
31+
32+
$this->getFilesystem()->mkdir($target);
33+
34+
$command = sprintf('cd %s && sh %s', escapeshellarg($target), escapeshellarg($file));
35+
36+
$exitCode = $this->executeCommand($command);
37+
38+
return $this->isExitCodeSuccessful($exitCode);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function isSupported()
45+
{
46+
if (null === $this->supported) {
47+
$this->supported = $this->existsCommand('sh');
48+
}
49+
50+
return $this->supported;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public static function getClass()
57+
{
58+
return get_class();
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function isFormatSupported(Format\FormatInterface $format = null)
65+
{
66+
return $format instanceof Format\Simple\Shar;
67+
}
68+
}

tests/Format/Simple/SharTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Distill\Tests\Format\Simple;
4+
5+
use Distill\Format\Simple\Shar;
6+
use Distill\Tests\Format\AbstractFormatTest;
7+
8+
class SharTest extends AbstractFormatTest
9+
{
10+
public function setUp()
11+
{
12+
$this->format = new Shar();
13+
}
14+
15+
public function testCompressionRatioLevelIsValid()
16+
{
17+
$this->assertLevelValid($this->format->getCompressionRatioLevel());
18+
}
19+
}

tests/Method/Command/UnsharTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Distill\Tests\Method\Command;
4+
5+
use Distill\Method;
6+
use Distill\Format;
7+
use Distill\Tests\Method\AbstractMethodTest;
8+
9+
class UnsharTest extends AbstractMethodTest
10+
{
11+
public function setUp()
12+
{
13+
$this->method = new Method\Command\Unshar();
14+
15+
if (false === $this->method->isSupported()) {
16+
$this->markTestSkipped('The unshar command is not installed');
17+
}
18+
19+
parent::setUp();
20+
}
21+
22+
public function testExtractCorrectSharFile()
23+
{
24+
$target = $this->getTemporaryPath();
25+
$this->clearTemporaryPath();
26+
27+
$response = $this->extract('file_ok.shar', $target, new Format\Simple\Shar());
28+
29+
$this->assertTrue($response);
30+
$this->assertUncompressed($target, 'file_ok.shar');
31+
$this->clearTemporaryPath();
32+
}
33+
34+
}

tests/Resources/files/file_fake.shar

1.21 KB
Binary file not shown.

tests/Resources/files/file_ok.shar

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This is a shell archive. Save it in a file, remove anything before
2+
# this line, and then unpack it by entering "sh file". Note, it may
3+
# create directories; files and directories will be owned by you and
4+
# have default permissions.
5+
#
6+
# This archive contains:
7+
#
8+
# 1.txt
9+
# 2.txt
10+
# 3.txt
11+
#
12+
echo x - 1.txt
13+
sed 's/^X//' >1.txt << 'END-of-1.txt'
14+
X1.txt file
15+
END-of-1.txt
16+
echo x - 2.txt
17+
sed 's/^X//' >2.txt << 'END-of-2.txt'
18+
X2.txt file
19+
END-of-2.txt
20+
echo x - 3.txt
21+
sed 's/^X//' >3.txt << 'END-of-3.txt'
22+
X3.txt file
23+
END-of-3.txt
24+
exit
25+
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/1.txt|1.txt file
2+
/2.txt|2.txt file
3+
/3.txt|3.txt file

tests/Resources/scripts/main.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ $DIR/epub.sh
3030
$DIR/jar.sh
3131
$DIR/dmg.sh
3232
$DIR/iso.sh
33-
$DIR/ar+deb.sh
33+
$DIR/ar+deb.sh
34+
$DIR/cpio.sh
35+
$DIR/shar.sh

tests/Resources/scripts/shar.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
################################################################################
4+
# Initial configuration
5+
################################################################################
6+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7+
FILES_DIR="$DIR/../files"
8+
9+
################################################################################
10+
# Clean files
11+
################################################################################
12+
rm -f $FILES_DIR/file_ok.shar $FILES_DIR/file_fake.shar
13+
14+
################################################################################
15+
# Generate files
16+
################################################################################
17+
18+
# shar: fake file
19+
dd if=/dev/urandom of=$FILES_DIR/file_fake.shar bs=1 count=1240
20+
21+
# shar: regular file
22+
cd $FILES_DIR/uncompressed
23+
shar 1.txt 2.txt 3.txt > ../file_ok.shar
24+
printf "\1.txt|1.txt file\n\2.txt|2.txt file\n\3.txt|3.txt file" > ../file_ok.shar.key

0 commit comments

Comments
 (0)