/** * Adds a folder to the archive * @param string $path The folder which will be copied into the archive * @param string $name The entry name * @return $this * @throws */ public function addFolder($path, $name = null) { $fs = new Filesystem(); //check the folder exists if (!is_dir($path)) { throw new \InvalidArgumentException("Folder \"{$path}\" not found."); } $path = rtrim($path, '\\//'); //set the default name if (is_null($name)) { $name = basename($path); if ($name == '.') { $name = basename(dirname($path)); } } // @see http://us.php.net/manual/en/ziparchive.addfile.php#89813 // @see http://stackoverflow.com/questions/4620205/php-ziparchive-corrupt-in-windows $name = str_replace('\\', '/', ltrim($name, '\\/')); if (!empty($name) && $this->archive->statName($name) === false) { if ($this->archive->addEmptyDir($name) === false) { throw new \RuntimeException("Unable to add folder \"{$path}\" to ZIP archive as \"{$name}\"."); } } //** I had to use \DirectoryIterator instead of \deit\filesystem\Finder because I kept hitting the directory not empty when trying to remove files after this method $it = new \FilesystemIterator($path); foreach ($it as $p) { if (empty($name)) { $n = $fs->getRelativePath($p->getPathname(), $path); } else { $n = $name . '/' . $fs->getRelativePath($p->getPathname(), $path); } $this->add($p->getPathname(), $n); } return $this; }
/** * Deletes the files and folders * @return $this */ public function remove() { $fs = new Filesystem(); foreach (iterator_to_array($this->getIterator()) as $srcPath) { $fs->remove($srcPath); } return $this; }