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

Inprove extaction #38

Closed
beinnova opened this issue Aug 12, 2016 · 2 comments
Closed

Inprove extaction #38

beinnova opened this issue Aug 12, 2016 · 2 comments

Comments

@beinnova
Copy link

Hi,
For my application I nead to extract only one file from my archive. I've seen it's impossible with Zip adapter now. But in local I've modified the Adapter and seems to works.

I have just made this modification:

On line 187

$archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($this->getArchive()));

I've also added this lines

if(!empty($content) && !is_array($content)) { $content = [$content]; }
and finally pass the array to the extrac method.

$res = $zip->extractTo($target, $content);

Of Course it's not the best solution, it's only a test, but maybe you can takes this example for improve the library.

@adamlundrigan
Copy link
Contributor

Could you provide a short example to illustrate your use case?

I applied your suggested changes to my local branch:

diff --git a/src/Compress/Zip.php b/src/Compress/Zip.php
index 9967d2a..5d7ff1f 100644
--- a/src/Compress/Zip.php
+++ b/src/Compress/Zip.php
@@ -184,7 +184,9 @@ class Zip extends AbstractCompressionAlgorithm
      */
     public function decompress($content)
     {
-        $archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($content));
+        if(!empty($content) && !is_array($content)) { $content = [$content]; }
+
+        $archive = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($this->getArchive()));

         if (empty($archive) || !file_exists($archive)) {
             throw new Exception\RuntimeException('ZIP Archive not found');
@@ -210,7 +212,7 @@ class Zip extends AbstractCompressionAlgorithm
             throw new Exception\RuntimeException($this->errorString($res));
         }

-        $res = $zip->extractTo($target);
+        $res = $zip->extractTo($target, $content);
         if ($res !== true) {
             throw new Exception\RuntimeException($this->errorString($res));
         }

and it caused a number of failures in existing tests

PHPUnit output

There were 6 errors:

1) ZendTest\Filter\Compress\ZipTest::testBasicUsage
Zend\Filter\Exception\RuntimeException: Unknown error within ZIP Archive

/var/www/zendframework/zend-filter/src/Compress/Zip.php:217
/var/www/zendframework/zend-filter/test/Compress/ZipTest.php:114

2) ZendTest\Filter\Compress\ZipTest::testZipCompressFile
Zend\Filter\Exception\RuntimeException: Unknown error within ZIP Archive

/var/www/zendframework/zend-filter/src/Compress/Zip.php:217
/var/www/zendframework/zend-filter/test/Compress/ZipTest.php:193

3) ZendTest\Filter\Compress\ZipTest::testCompressNonExistingTargetFile
Zend\Filter\Exception\RuntimeException: Unknown error within ZIP Archive

/var/www/zendframework/zend-filter/src/Compress/Zip.php:217
/var/www/zendframework/zend-filter/test/Compress/ZipTest.php:220

4) ZendTest\Filter\Compress\ZipTest::testZipCompressDirectory
Zend\Filter\Exception\RuntimeException: Unknown error within ZIP Archive

/var/www/zendframework/zend-filter/src/Compress/Zip.php:217
/var/www/zendframework/zend-filter/test/Compress/ZipTest.php:247

5) ZendTest\Filter\Compress\ZipTest::testDecompressWillThrowExceptionWhenDecompressingWithNoTarget
Zend\Filter\Exception\RuntimeException: Unknown error within ZIP Archive

/var/www/zendframework/zend-filter/src/Compress/Zip.php:217
/var/www/zendframework/zend-filter/test/Compress/ZipTest.php:294

6) ZendTest\Filter\Compress\ZipTest::testDecompressWhenNoArchieveInClass
Zend\Filter\Exception\RuntimeException: Unknown error within ZIP Archive

/var/www/zendframework/zend-filter/src/Compress/Zip.php:212
/var/www/zendframework/zend-filter/test/Compress/ZipTest.php:326

FAILURES!
Tests: 767, Assertions: 1586, Errors: 6, Skipped: 21, Incomplete: 6.

@michalbundyra
Copy link
Member

Won't work. As the $content we must provide archive to extract, not entires to extract from archive provided in the options.

Closing due to inactivity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants