Procházet zdrojové kódy

Fix support entry name '0'

Ne-Lexa před 9 roky
rodič
revize
015166d165
2 změnil soubory, kde provedl 30 přidání a 17 odebrání
  1. 1 1
      src/PhpZip/ZipFile.php
  2. 29 16
      src/PhpZip/ZipOutputFile.php

+ 1 - 1
src/PhpZip/ZipFile.php

@@ -401,7 +401,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
      */
      */
     public static function openFromString($data)
     public static function openFromString($data)
     {
     {
-        if (empty($data)) {
+        if (null === $data || strlen($data) === 0) {
             throw new IllegalArgumentException("Data not available");
             throw new IllegalArgumentException("Data not available");
         }
         }
         if (!($handle = fopen('php://temp', 'r+b'))) {
         if (!($handle = fopen('php://temp', 'r+b'))) {

+ 29 - 16
src/PhpZip/ZipOutputFile.php

@@ -269,7 +269,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
      */
      */
     public function setComment($comment)
     public function setComment($comment)
     {
     {
-        if (null !== $comment && !empty($comment)) {
+        if (null !== $comment && strlen($comment) !== 0) {
             $comment = (string)$comment;
             $comment = (string)$comment;
             $length = strlen($comment);
             $length = strlen($comment);
             if (0x0000 > $length || $length > 0xffff) {
             if (0x0000 > $length || $length > 0xffff) {
@@ -292,10 +292,10 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
     public function addFromString($entryName, $data, $compressionMethod = ZipEntry::METHOD_DEFLATED)
     public function addFromString($entryName, $data, $compressionMethod = ZipEntry::METHOD_DEFLATED)
     {
     {
         $entryName = (string)$entryName;
         $entryName = (string)$entryName;
-        if ($data === null) {
-            throw new IllegalArgumentException("data is null");
+        if ($data === null || strlen($data) === 0) {
+            throw new IllegalArgumentException("Data is empty");
         }
         }
-        if (empty($entryName)) {
+        if ($entryName === null || strlen($entryName) === 0) {
             throw new IllegalArgumentException("Incorrect entry name " . $entryName);
             throw new IllegalArgumentException("Incorrect entry name " . $entryName);
         }
         }
         $this->validateCompressionMethod($compressionMethod);
         $this->validateCompressionMethod($compressionMethod);
@@ -343,7 +343,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
     )
     )
     {
     {
         $inputDir = (string)$inputDir;
         $inputDir = (string)$inputDir;
-        if (empty($inputDir)) {
+        if ($inputDir === null || strlen($inputDir) === 0) {
             throw new IllegalArgumentException('Input dir empty');
             throw new IllegalArgumentException('Input dir empty');
         }
         }
         if (!is_dir($inputDir)) {
         if (!is_dir($inputDir)) {
@@ -367,8 +367,12 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
         foreach ($files as $file) {
         foreach ($files as $file) {
             $filename = str_replace($inputDir, $moveToPath, $file);
             $filename = str_replace($inputDir, $moveToPath, $file);
             $filename = ltrim($filename, '/');
             $filename = ltrim($filename, '/');
-            is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
-            is_file($file) && $this->addFromFile($file, $filename, $compressionMethod);
+            if(is_dir($file)){
+                FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
+            }
+            elseif(is_file($file)){
+                $this->addFromFile($file, $filename, $compressionMethod);
+            }
         }
         }
         return $this->count() > $count;
         return $this->count() > $count;
     }
     }
@@ -392,7 +396,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
     public function addEmptyDir($dirName)
     public function addEmptyDir($dirName)
     {
     {
         $dirName = (string)$dirName;
         $dirName = (string)$dirName;
-        if (empty($dirName)) {
+        if (strlen($dirName) === 0) {
             throw new IllegalArgumentException("dirName null or not string");
             throw new IllegalArgumentException("dirName null or not string");
         }
         }
         $dirName = rtrim($dirName, '/') . '/';
         $dirName = rtrim($dirName, '/') . '/';
@@ -447,7 +451,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
             throw new IllegalArgumentException("stream is not resource");
             throw new IllegalArgumentException("stream is not resource");
         }
         }
         $entryName = (string)$entryName;
         $entryName = (string)$entryName;
-        if (empty($entryName)) {
+        if (strlen($entryName) === 0) {
             throw new IllegalArgumentException("Incorrect entry name " . $entryName);
             throw new IllegalArgumentException("Incorrect entry name " . $entryName);
         }
         }
         $this->validateCompressionMethod($compressionMethod);
         $this->validateCompressionMethod($compressionMethod);
@@ -486,7 +490,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
         if (!is_dir($inputDir)) {
         if (!is_dir($inputDir)) {
             throw new IllegalArgumentException('Directory ' . $inputDir . ' can\'t exists');
             throw new IllegalArgumentException('Directory ' . $inputDir . ' can\'t exists');
         }
         }
-        if (null === $globPattern || !is_string($globPattern)) {
+        if (null === $globPattern || strlen($globPattern) === 0) {
             throw new IllegalArgumentException("globPattern null");
             throw new IllegalArgumentException("globPattern null");
         }
         }
         if (empty($globPattern)) {
         if (empty($globPattern)) {
@@ -514,8 +518,12 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
         foreach ($filesFound as $file) {
         foreach ($filesFound as $file) {
             $filename = str_replace($inputDir, $moveToPath, $file);
             $filename = str_replace($inputDir, $moveToPath, $file);
             $filename = ltrim($filename, '/');
             $filename = ltrim($filename, '/');
-            is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
-            is_file($file) && $this->addFromFile($file, $filename, $compressionMethod);
+            if(is_dir($file)){
+                FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
+            }
+            elseif(is_file($file)){
+                $this->addFromFile($file, $filename, $compressionMethod);
+            }
         }
         }
         return $this->count() > $count;
         return $this->count() > $count;
     }
     }
@@ -571,8 +579,12 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
         foreach ($files as $file) {
         foreach ($files as $file) {
             $filename = str_replace($inputDir, $moveToPath, $file);
             $filename = str_replace($inputDir, $moveToPath, $file);
             $filename = ltrim($filename, '/');
             $filename = ltrim($filename, '/');
-            is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
-            is_file($file) && $this->addFromFile($file, $filename, $compressionMethod);
+            if(is_dir($file)){
+                FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
+            }
+            elseif(is_file($file)){
+                $this->addFromFile($file, $filename, $compressionMethod);
+            }
         }
         }
         return $this->count() > $count;
         return $this->count() > $count;
     }
     }
@@ -1268,7 +1280,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
     public function outputAsAttachment($outputFilename, $mimeType = null)
     public function outputAsAttachment($outputFilename, $mimeType = null)
     {
     {
         $outputFilename = (string)$outputFilename;
         $outputFilename = (string)$outputFilename;
-        if (empty($outputFilename)) {
+        if (strlen($outputFilename) === 0) {
             throw new IllegalArgumentException("Output filename is empty.");
             throw new IllegalArgumentException("Output filename is empty.");
         }
         }
         if (empty($mimeType) || !is_string($mimeType)) {
         if (empty($mimeType) || !is_string($mimeType)) {
@@ -1360,7 +1372,8 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
      */
      */
     public function offsetSet($entryName, $uncompressedDataContent)
     public function offsetSet($entryName, $uncompressedDataContent)
     {
     {
-        if (empty($entryName)) {
+        $entryName = (string)$entryName;
+        if (strlen($entryName) === 0) {
             throw new IllegalArgumentException('Entry name empty');
             throw new IllegalArgumentException('Entry name empty');
         }
         }
         if ($entryName[strlen($entryName) - 1] === '/') {
         if ($entryName[strlen($entryName) - 1] === '/') {