Skip to content

Commit

Permalink
Uncompress bz2 files using the bzip2 extension
Browse files Browse the repository at this point in the history
  • Loading branch information
raulfraile committed Dec 14, 2014
1 parent c980880 commit a34ae08
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(
Method\Command\Xz::getClass(),
Method\Command\Gnome\Gcab::getClass(),
Method\Extension\Pear\ArchiveTar::getClass(),
Method\Extension\Bzip2::getClass(),
Method\Extension\Phar::getClass(),
Method\Extension\PharData::getClass(),
Method\Extension\Rar::getClass(),
Expand Down
97 changes: 97 additions & 0 deletions src/Method/Extension/Bzip2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of the Distill package.
*
* (c) Raul Fraile <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Distill\Method\Extension;

use Distill\Exception;
use Distill\Format;
use Distill\Method\AbstractMethod;
use Distill\Method\MethodInterface;

/**
* Extracts files from bz2 archives using the bzip2 extension.
*
* @author Raul Fraile <[email protected]>
*/
class Bzip2 extends AbstractMethod
{
/**
* {@inheritdoc}
*/
public function extract($file, $target, Format\FormatInterface $format)
{
$this->checkSupport($format);

$basename = pathinfo($file, PATHINFO_FILENAME);

if (false === $this->isValid($file)) {
throw new Exception\IO\Input\FileCorruptedException($file, Exception\IO\Input\FileCorruptedException::SEVERITY_HIGH);
}

$source = bzopen($file, 'r');

@mkdir($target);
$destination = fopen($target . DIRECTORY_SEPARATOR . $basename, 'w');

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

bzclose($source);
fclose($destination);

return $bytes > 0;
}

/**
* {@inheritdoc}
*/
public function isSupported()
{
if (null === $this->supported) {
$this->supported = extension_loaded('bz2');
}

return $this->supported;
}

/**
* {@inheritdoc}
*/
public static function getClass()
{
return get_class();
}

/**
* {@inheritdoc}
*/
public static function getUncompressionSpeedLevel(Format\FormatInterface $format = null)
{
return MethodInterface::SPEED_LEVEL_LOW;
}

public function isFormatSupported(Format\FormatInterface $format)
{
return $format instanceof Format\Simple\Bz2;
}

protected function isValid($file)
{
$fileHandler = fopen($file, 'rb');
if (false === $fileHandler) {
return false;
}

$magicNumber = bin2hex(fread($fileHandler, 2));
fclose($fileHandler);

return '425a' === $magicNumber;
}
}
3 changes: 3 additions & 0 deletions src/Method/Extension/Zlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function extract($file, $target, Format\FormatInterface $format)

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

gzclose($source);
fclose($destination);

return $bytes > 0;
}

Expand Down
58 changes: 58 additions & 0 deletions tests/Method/Extension/Bzip2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Distill\Tests\Method\Extension;

use Distill\Method;
use Distill\Format;
use Distill\Tests\Method\AbstractMethodTest;

class Bzip2Test extends AbstractMethodTest
{
public function setUp()
{
$this->method = new Method\Extension\Bzip2();

if (false === $this->method->isSupported()) {
$this->markTestSkipped('The bzip2 extension method is not available');
}

parent::setUp();
}

public function testExtractCorrectBz2File()
{
$target = $this->getTemporaryPath();
$this->clearTemporaryPath();

$response = $this->extract('file_ok.bz2', $target, new Format\Simple\Bz2());

$this->assertTrue($response);
//$this->assertUncompressed($target, 'file_ok.gz');
$this->clearTemporaryPath();
}

public function testExtractFakeBz2File()
{
$this->setExpectedException('Distill\\Exception\\IO\\Input\\FileCorruptedException');

$target = $this->getTemporaryPath();
$this->clearTemporaryPath();

$this->extract('file_fake.bz2', $target, new Format\Simple\Bz2());

$this->clearTemporaryPath();
}

public function testExtractNoBz2File()
{
$this->setExpectedException('Distill\\Exception\\Method\\FormatNotSupportedInMethodException');

$target = $this->getTemporaryPath();
$this->clearTemporaryPath();

$this->extract('file_ok.rar', $target, new Format\Simple\Rar());

$this->clearTemporaryPath();
}

}

0 comments on commit a34ae08

Please sign in to comment.