-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uncompress bz2 files using the bzip2 extension
- Loading branch information
1 parent
c980880
commit a34ae08
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |