Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 /**
  * 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());
     }
     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
         if ($this->areFilesetsUpToDate()) {
             $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);
         }
         if ($this->comment !== '') {
             $isCommented = $zip->setArchiveComment($this->comment);
             if ($isCommented === false) {
                 $this->log("Could not add a comment for the Archive.", Project::MSG_INFO);
             }
         }
         $this->addFilesetsToArchive($zip);
         $zip->close();
     } catch (IOException $ioe) {
         $msg = "Problem creating ZIP: " . $ioe->getMessage();
         throw new BuildException($msg, $ioe, $this->getLocation());
     }
 }
Пример #3
0
 /**
  * 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;
 }