Skip to content

Commit a34ae08

Browse files
committed
Uncompress bz2 files using the bzip2 extension
1 parent c980880 commit a34ae08

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

src/ContainerProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
Method\Command\Xz::getClass(),
4949
Method\Command\Gnome\Gcab::getClass(),
5050
Method\Extension\Pear\ArchiveTar::getClass(),
51+
Method\Extension\Bzip2::getClass(),
5152
Method\Extension\Phar::getClass(),
5253
Method\Extension\PharData::getClass(),
5354
Method\Extension\Rar::getClass(),

src/Method/Extension/Bzip2.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\Extension;
13+
14+
use Distill\Exception;
15+
use Distill\Format;
16+
use Distill\Method\AbstractMethod;
17+
use Distill\Method\MethodInterface;
18+
19+
/**
20+
* Extracts files from bz2 archives using the bzip2 extension.
21+
*
22+
* @author Raul Fraile <raulfraile@gmail.com>
23+
*/
24+
class Bzip2 extends AbstractMethod
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function extract($file, $target, Format\FormatInterface $format)
30+
{
31+
$this->checkSupport($format);
32+
33+
$basename = pathinfo($file, PATHINFO_FILENAME);
34+
35+
if (false === $this->isValid($file)) {
36+
throw new Exception\IO\Input\FileCorruptedException($file, Exception\IO\Input\FileCorruptedException::SEVERITY_HIGH);
37+
}
38+
39+
$source = bzopen($file, 'r');
40+
41+
@mkdir($target);
42+
$destination = fopen($target . DIRECTORY_SEPARATOR . $basename, 'w');
43+
44+
$bytes = stream_copy_to_stream($source, $destination);
45+
46+
bzclose($source);
47+
fclose($destination);
48+
49+
return $bytes > 0;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function isSupported()
56+
{
57+
if (null === $this->supported) {
58+
$this->supported = extension_loaded('bz2');
59+
}
60+
61+
return $this->supported;
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public static function getClass()
68+
{
69+
return get_class();
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public static function getUncompressionSpeedLevel(Format\FormatInterface $format = null)
76+
{
77+
return MethodInterface::SPEED_LEVEL_LOW;
78+
}
79+
80+
public function isFormatSupported(Format\FormatInterface $format)
81+
{
82+
return $format instanceof Format\Simple\Bz2;
83+
}
84+
85+
protected function isValid($file)
86+
{
87+
$fileHandler = fopen($file, 'rb');
88+
if (false === $fileHandler) {
89+
return false;
90+
}
91+
92+
$magicNumber = bin2hex(fread($fileHandler, 2));
93+
fclose($fileHandler);
94+
95+
return '425a' === $magicNumber;
96+
}
97+
}

src/Method/Extension/Zlib.php

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function extract($file, $target, Format\FormatInterface $format)
4343

4444
$bytes = stream_copy_to_stream($source, $destination);
4545

46+
gzclose($source);
47+
fclose($destination);
48+
4649
return $bytes > 0;
4750
}
4851

tests/Method/Extension/Bzip2Test.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Distill\Tests\Method\Extension;
4+
5+
use Distill\Method;
6+
use Distill\Format;
7+
use Distill\Tests\Method\AbstractMethodTest;
8+
9+
class Bzip2Test extends AbstractMethodTest
10+
{
11+
public function setUp()
12+
{
13+
$this->method = new Method\Extension\Bzip2();
14+
15+
if (false === $this->method->isSupported()) {
16+
$this->markTestSkipped('The bzip2 extension method is not available');
17+
}
18+
19+
parent::setUp();
20+
}
21+
22+
public function testExtractCorrectBz2File()
23+
{
24+
$target = $this->getTemporaryPath();
25+
$this->clearTemporaryPath();
26+
27+
$response = $this->extract('file_ok.bz2', $target, new Format\Simple\Bz2());
28+
29+
$this->assertTrue($response);
30+
//$this->assertUncompressed($target, 'file_ok.gz');
31+
$this->clearTemporaryPath();
32+
}
33+
34+
public function testExtractFakeBz2File()
35+
{
36+
$this->setExpectedException('Distill\\Exception\\IO\\Input\\FileCorruptedException');
37+
38+
$target = $this->getTemporaryPath();
39+
$this->clearTemporaryPath();
40+
41+
$this->extract('file_fake.bz2', $target, new Format\Simple\Bz2());
42+
43+
$this->clearTemporaryPath();
44+
}
45+
46+
public function testExtractNoBz2File()
47+
{
48+
$this->setExpectedException('Distill\\Exception\\Method\\FormatNotSupportedInMethodException');
49+
50+
$target = $this->getTemporaryPath();
51+
$this->clearTemporaryPath();
52+
53+
$this->extract('file_ok.rar', $target, new Format\Simple\Rar());
54+
55+
$this->clearTemporaryPath();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)