瀏覽代碼

Fix bug add files from directory iterator.

Ne-Lexa 9 年之前
父節點
當前提交
1e4b14177a
共有 3 個文件被更改,包括 32 次插入13 次删除
  1. 16 2
      .travis.yml
  2. 2 1
      composer.json
  3. 14 10
      src/PhpZip/ZipFile.php

+ 16 - 2
.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
+  - composer validate --no-check-lock
+  - vendor/bin/phpunit -v -c bootstrap.xml --coverage-clover build/logs/clover.xml
+
+after_success:
+  - vendor/bin/test-reporter
+

+ 2 - 1
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": [

+ 14 - 10
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;
     }
 
     /**