/** * 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; }
/** * Copies the files and folders to the destination folder * @param string $dest The destination directory * @return $this * @throws */ public function copyTo($dest) { $fs = new Filesystem(); //check the destination directory exists if (!is_dir($dest)) { throw new \RuntimeException("Destination directory \"{$dest}\" does not exist."); } foreach ($this->getIterator() as $srcPath) { //create the destination path $destPath = $dest . DIRECTORY_SEPARATOR . $fs->getRelativePath($srcPath, $this->path); //create the destination parent if it doesn't already exist $parentPath = dirname($destPath); if (!is_dir($parentPath)) { $fs->mkdir($parentPath); } if ($srcPath->isDir()) { $fs->mkdir($destPath); } else { $fs->copy($srcPath, $destPath); } } return $this; }