From 1e4b14177a51e1515a0bb41aef4b77b248b04f19 Mon Sep 17 00:00:00 2001 From: Ne-Lexa Date: Mon, 13 Mar 2017 19:58:51 +0300 Subject: [PATCH] Fix bug add files from directory iterator. --- .travis.yml | 18 ++++++++++++++++-- composer.json | 3 ++- src/PhpZip/ZipFile.php | 24 ++++++++++++++---------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index e6b889d..bc35c18 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,13 +7,27 @@ php: - hhvm - nightly +# cache vendor dirs +cache: + directories: + - vendor + - $HOME/.composer/cache + addons: code_climate: repo_token: 486a09d58d663450146c53c81c6c64938bcf3bb0b7c8ddebdc125fe97c18213a +install: + - travis_retry composer self-update && composer --version + - travis_retry composer install --prefer-dist --no-interaction + before_script: - sudo apt-get install p7zip-full - - composer install script: - vendor/bin/phpunit -v -c bootstrap.xml \ No newline at end of file + - composer validate --no-check-lock + - vendor/bin/phpunit -v -c bootstrap.xml --coverage-clover build/logs/clover.xml + +after_success: + - vendor/bin/test-reporter + diff --git a/composer.json b/composer.json index 2b8d2ac..16e73ed 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "zipalign" ], "require-dev": { - "phpunit/phpunit": "4.8" + "phpunit/phpunit": "4.8", + "codeclimate/php-test-reporter": "^0.4.4" }, "license": "MIT", "authors": [ diff --git a/src/PhpZip/ZipFile.php b/src/PhpZip/ZipFile.php index bb07cc3..173394a 100644 --- a/src/PhpZip/ZipFile.php +++ b/src/PhpZip/ZipFile.php @@ -296,7 +296,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator * @param string $destination Location where to extract the files. * @param array|string|null $entries The entries to extract. It accepts either * a single entry name or an array of names. - * @return bool + * @return ZipFile * @throws ZipException */ public function extractTo($destination, $entries = null) @@ -335,7 +335,6 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator $zipEntries = $this->centralDirectory->getEntries(); } - $extract = 0; foreach ($zipEntries as $entry) { $file = $destination . DIRECTORY_SEPARATOR . $entry->getName(); if ($entry->isDirectory()) { @@ -357,12 +356,11 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator touch($dir, $entry->getTime()); } if (file_put_contents($file, $entry->getEntryContent()) === false) { - return false; + throw new ZipException('Can not extract file '.$entry->getName()); } touch($file, $entry->getTime()); - $extract++; } - return $extract > 0; + return $this; } /** @@ -652,7 +650,6 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator $files = []; foreach ($iterator as $file) { if ($file instanceof \SplFileInfo) { - empty($path) and $path = rtrim($file->getPath(), '/'); if ('..' === $file->getBasename()) { continue; } @@ -663,7 +660,12 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator } } } + if(empty($files)){ + return $this; + } + natcasesort($files); + $path = array_shift($files); foreach ($files as $file) { $relativePath = str_replace($path, $localPath, $file); $relativePath = ltrim($relativePath, '/'); @@ -905,7 +907,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator * Delete entries by glob pattern. * * @param string $globPattern Glob pattern - * @return bool + * @return ZipFile * @throws InvalidArgumentException * @sse https://en.wikipedia.org/wiki/Glob_(programming) Glob pattern syntax */ @@ -915,14 +917,15 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator throw new InvalidArgumentException("Glob pattern is empty"); } $globPattern = '~' . FilesUtil::convertGlobToRegEx($globPattern) . '~si'; - return $this->deleteFromRegex($globPattern); + $this->deleteFromRegex($globPattern); + return $this; } /** * Delete entries by regex pattern. * * @param string $regexPattern Regex pattern - * @return bool + * @return ZipFile * @throws InvalidArgumentException */ public function deleteFromRegex($regexPattern) @@ -930,7 +933,8 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator if ($regexPattern === null || !is_string($regexPattern) || empty($regexPattern)) { throw new InvalidArgumentException("Regex pattern is empty."); } - return $this->centralDirectory->deleteEntriesFromRegex($regexPattern); + $this->centralDirectory->deleteEntriesFromRegex($regexPattern); + return $this; } /**