/** * @return array * @throws BuildException */ public function areFilesetsUpToDate() { foreach ($this->filesets as $fs) { $files = $fs->getFiles($this->project, $this->includeEmpty); if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { return false; } for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { if ($this->zipFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) { throw new BuildException("A zip file cannot include itself", $this->getLocation()); } } } return true; }
/** * do the work * @throws BuildException */ public function main() { if ($this->zipFile === null) { throw new BuildException("zipfile attribute must be set!", $this->getLocation()); } if ($this->zipFile->exists() && $this->zipFile->isDirectory()) { throw new BuildException("zipfile is a directory!", $this->getLocation()); } if ($this->zipFile->exists() && !$this->zipFile->canWrite()) { throw new BuildException("Can not write to the specified zipfile!", $this->getLocation()); } // shouldn't need to clone, since the entries in filesets // themselves won't be modified -- only elements will be added $savedFileSets = $this->filesets; try { if ($this->baseDir !== null) { if (!$this->baseDir->exists()) { throw new BuildException("basedir does not exist!", $this->getLocation()); } if (empty($this->filesets)) { // add the main fileset to the list of filesets to process. $mainFileSet = new ZipFileSet($this->fileset); $mainFileSet->setDir($this->baseDir); $this->filesets[] = $mainFileSet; } } if (empty($this->filesets)) { throw new BuildException("You must supply either a basedir " . "attribute or some nested filesets.", $this->getLocation()); } // check if zip is out of date with respect to each // fileset $upToDate = true; foreach ($this->filesets as $fs) { $files = $fs->getFiles($this->project, $this->includeEmpty); if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { $upToDate = false; } for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { if ($this->zipFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) { throw new BuildException("A zip file cannot include itself", $this->getLocation()); } } } if ($upToDate) { $this->log("Nothing to do: " . $this->zipFile->__toString() . " is up to date.", Project::MSG_INFO); return; } $this->log("Building zip: " . $this->zipFile->__toString(), Project::MSG_INFO); $zip = new Archive_Zip($this->zipFile->getAbsolutePath()); foreach ($this->filesets as $fs) { $files = $fs->getFiles($this->project, $this->includeEmpty); $fsBasedir = null != $this->baseDir ? $this->baseDir : $fs->getDir($this->project); $filesToZip = array(); for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { $f = new PhingFile($fsBasedir, $files[$i]); $filesToZip[] = $f->getAbsolutePath(); $this->log("Adding " . $f->getPath() . " to archive.", Project::MSG_VERBOSE); } $zip->add($filesToZip, array('remove_path' => $fsBasedir->getCanonicalPath())); } } catch (IOException $ioe) { $msg = "Problem creating ZIP: " . $ioe->getMessage(); $this->filesets = $savedFileSets; throw new BuildException($msg, $ioe, $this->getLocation()); } $this->filesets = $savedFileSets; }
/** * do the work * @throws BuildException */ public function main() { if (!extension_loaded('zip')) { throw new BuildException("Zip extension is required"); } if ($this->zipFile === null) { throw new BuildException("zipfile attribute must be set!", $this->getLocation()); } if ($this->zipFile->exists() && $this->zipFile->isDirectory()) { throw new BuildException("zipfile is a directory!", $this->getLocation()); } if ($this->zipFile->exists() && !$this->zipFile->canWrite()) { throw new BuildException("Can not write to the specified zipfile!", $this->getLocation()); } // shouldn't need to clone, since the entries in filesets // themselves won't be modified -- only elements will be added $savedFileSets = $this->filesets; try { if ($this->baseDir !== null) { if (!$this->baseDir->exists()) { throw new BuildException("basedir '" . (string) $this->baseDir . "' does not exist!", $this->getLocation()); } if (empty($this->filesets)) { // add the main fileset to the list of filesets to process. $mainFileSet = new ZipFileSet($this->fileset); $mainFileSet->setDir($this->baseDir); $this->filesets[] = $mainFileSet; } } if (empty($this->filesets)) { throw new BuildException("You must supply either a basedir " . "attribute or some nested filesets.", $this->getLocation()); } // check if zip is out of date with respect to each // fileset $upToDate = true; foreach ($this->filesets as $fs) { $files = $fs->getFiles($this->project, $this->includeEmpty); if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { $upToDate = false; } for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { if ($this->zipFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) { throw new BuildException("A zip file cannot include itself", $this->getLocation()); } } } if ($upToDate) { $this->log("Nothing to do: " . $this->zipFile->__toString() . " is up to date.", Project::MSG_INFO); return; } $this->log("Building zip: " . $this->zipFile->__toString(), Project::MSG_INFO); $zip = new ZipArchive(); $res = $zip->open($this->zipFile->getAbsolutePath(), ZIPARCHIVE::CREATE); if ($res !== true) { throw new Exception("ZipArchive::open() failed with code " . $res); } foreach ($this->filesets as $fs) { $fsBasedir = null != $this->baseDir ? $this->baseDir : $fs->getDir($this->project); $files = $fs->getFiles($this->project, $this->includeEmpty); $filesToZip = array(); for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { $f = new PhingFile($fsBasedir, $files[$i]); $pathInZip = $this->prefix . $f->getPathWithoutBase($fsBasedir); $pathInZip = str_replace('\\', '/', $pathInZip); if ($f->isDirectory()) { if ($pathInZip != '.') { $zip->addEmptyDir($pathInZip); } } else { $zip->addFile($f->getPath(), $pathInZip); } $this->log("Adding " . $f->getPath() . " as " . $pathInZip . " to archive.", Project::MSG_VERBOSE); } } $zip->close(); } catch (IOException $ioe) { $msg = "Problem creating ZIP: " . $ioe->getMessage(); $this->filesets = $savedFileSets; throw new BuildException($msg, $ioe, $this->getLocation()); } $this->filesets = $savedFileSets; }
private function checkFilename($filename, $dir = null) { if ($dir !== null) { $f = new PhingFile($dir, $filename); } else { $f = new PhingFile($filename); } if (!$f->exists()) { $this->log("File " . (string) $f . " does not exist.", Project::MSG_ERR); return false; } if ($this->to !== null && $f->equals($this->to)) { throw new BuildException("Input file \"" . $f . "\" " . "is the same as the output file."); } if ($this->to !== null && !$this->overwrite && $this->to->exists() && $f->lastModified() > $this->to->lastModified()) { $this->log((string) $this->to . " is up-to-date.", Project::MSG_VERBOSE); return false; } return true; }