Skip to content

Commit a26651b

Browse files
committed
Merge pull request #25 from raulfraile/fix/current_dir
Fix extracting in the current directory
2 parents 13b3994 + 67d9516 commit a26651b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Distill.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Distill;
1313

14+
use Distill\Exception\IO\Output\TargetDirectoryNotWritableException;
1415
use Distill\Extractor\ExtractorInterface;
1516
use Distill\Format\FormatChain;
1617
use Distill\Format\FormatChainInterface;
1718
use Distill\Format\FormatInterface;
1819
use Distill\Extractor\Util\Filesystem;
20+
use Symfony\Component\Filesystem\Filesystem as SfFilesystem;
1921
use Distill\Strategy\StrategyInterface;
2022
use Pimple\Container;
2123

@@ -220,8 +222,25 @@ public function extractWithoutRootDirectory($file, $path, FormatInterface $forma
220222
throw new Exception\IO\Output\NotSingleDirectoryException($file);
221223
}
222224

223-
$this->filesystem->remove($path);
224-
$this->filesystem->rename($singleRootDirectoryName, $path);
225+
$workingDirectory = getcwd();
226+
if ($workingDirectory === realpath($path)) {
227+
if (dirname($workingDirectory) === $workingDirectory) {
228+
// root directory
229+
throw new TargetDirectoryNotWritableException($workingDirectory);
230+
}
231+
232+
chdir(dirname($workingDirectory));
233+
234+
$sfFilesystem = new SfFilesystem();
235+
$filesRemove = new \FilesystemIterator($workingDirectory, \FilesystemIterator::SKIP_DOTS);
236+
$sfFilesystem->remove($filesRemove);
237+
$sfFilesystem->mirror($singleRootDirectoryName, $workingDirectory);
238+
239+
chdir($workingDirectory);
240+
} else {
241+
$this->filesystem->remove($path);
242+
$this->filesystem->rename($singleRootDirectoryName, $path);
243+
}
225244

226245
return true;
227246
}

tests/DistillTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,20 @@ public function testCannotExtractWithoutRootDirectoryNoDirectoryZipFiles()
405405
}
406406
}
407407

408+
public function testCanExtractWithoutRootDirectoryInCurrentDirectory()
409+
{
410+
$target = $this->getTemporaryPath();
411+
$this->clearTemporaryPath();
412+
mkdir($target);
413+
chdir($target);
414+
415+
$response = $this->distill->extractWithoutRootDirectory($this->filesPath.'file_ok_dir.zip', '.', new Format\Simple\Zip());
416+
417+
$this->assertTrue($response);
418+
$this->assertUncompressed($target, 'file_ok_dir.zip', false, '/uncompressed');
419+
$this->clearTemporaryPath();
420+
}
421+
408422
public function testFormatIsNotSupportedAfterDisablingFormat()
409423
{
410424
$this->setExpectedException('Distill\\Exception\\IO\\Input\\FileFormatNotSupportedException');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Distill\Format;
77
use Distill\Tests\Method\AbstractMethodTest;
88

9-
class X7zipTest extends AbstractMethodTest
9+
class X7ZipTest extends AbstractMethodTest
1010
{
1111
public function setUp()
1212
{

0 commit comments

Comments
 (0)