Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Tar decompress works even if archive is not provided
Browse files Browse the repository at this point in the history
We do not need provide archive on initialisation Tar object, as we can
provide link to the file to decompress directly in filter method.

Fixes #41
  • Loading branch information
michalbundyra committed Aug 17, 2019
1 parent a870115 commit ac70c53
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Compress/Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ public function compress($content)
public function decompress($content)
{
$archive = $this->getArchive();
if (empty($archive) || ! file_exists($archive)) {
if (file_exists($content)) {
$archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($content));
} elseif (empty($archive) || ! file_exists($archive)) {
throw new Exception\RuntimeException('Tar Archive not found');
}

$archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($content));
$archive = new Archive_Tar($archive, $this->getMode());
$target = $this->getTarget();
if (! is_dir($target)) {
Expand Down
28 changes: 28 additions & 0 deletions test/Compress/TarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,32 @@ public function testTarToString()
$filter = new TarCompression();
$this->assertEquals('Tar', $filter->toString());
}

/**
* @see https://github.com/zendframework/zend-filter/issues/41
*/
public function testDecompressionDoesNotRequireArchive()
{
$filter = new TarCompression([
'archive' => $this->tmp . '/compressed.tar',
'target' => $this->tmp . '/zipextracted.txt',
]);

$content = 'compress me ' . microtime(true);
$compressed = $filter->compress($content);

self::assertSame($this->tmp . DIRECTORY_SEPARATOR . 'compressed.tar', $compressed);

$target = $this->tmp;
$filter = new TarCompression([
'target' => $target,
]);

$decompressed = $filter->decompress($compressed);
self::assertSame($target, $decompressed);
// per documentation, tar includes full path
$file = $target . DIRECTORY_SEPARATOR . $target . DIRECTORY_SEPARATOR . '/zipextracted.txt';
self::assertFileExists($file);
self::assertSame($content, file_get_contents($file));
}
}

0 comments on commit ac70c53

Please sign in to comment.