public function createAction() { //ini_set ( "memory_limit", "256M" ); // Sean Smith: 10/26/2011 - Reworked app packaging to handle multiple // sites calling this function at once $this->init(); // Check for existing site directory, copy package files from code // source location (/app/desktop/distro) to site dir if (!$this->_prepareSiteDirectory()) { // If dir structure not right, or a file copy failed, // bail and render error page (download.phtml) $this->view->assign('error_message', 'Could not create package. The error was: ' . $this->error_message); return; } // Populate sqlite db from mysql db $this->dbAction(); //zip up require_once 'app/desktop/Zip.php'; $file_collection = array(); $zipNameLen = strlen($this->zip_name); unlink($this->package_dir . '/' . $this->zip_name); $archive = new Archive_Zip($this->package_dir . '/' . $this->zip_name); // Gather up all files in distro directory // initialize an iterator pass it the directory to be processed $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(Globals::$BASE_PATH . 'app/desktop/distro')); // iterate over the directory add each file found to the archive foreach ($iterator as $key => $value) { // Exclude the zip file itself (if exists from a previous creation) if (substr($key, $zipNameLen * -1, $zipNameLen) != $this->zip_name) { $core_file_collection[] = realpath($key); } } // Gather up the site specific files (data/settings.xml and data/sqlite.db) $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->package_dir)); foreach ($iterator as $key => $value) { // Exclude the zip file itself if (substr($key, $zipNameLen * -1, $zipNameLen) != $this->zip_name) { $site_file_collection[] = realpath($key); } } // Files added in two stages because there are two paths to remove // (app/desktop/distro and package_dir) but zip procs will only take one path to remove per call $archive->create($site_file_collection, array('remove_path' => $this->package_dir, 'add_path' => 'TS')); $archive->add($core_file_collection, array('remove_path' => Globals::$BASE_PATH . 'app/desktop/distro', 'add_path' => 'TS')); }
/** * @param string The name of the archive * @param mixed The name of a single file or an array of files * @param string The compression for the archive * @param string Path to add within the archive * @param string Path to remove within the archive * @param boolean Automatically append the extension for the archive * @param boolean Remove for source files */ function create($archive, $files, $compress = 'tar', $addPath = '', $removePath = '', $autoExt = false, $cleanUp = false) { $compress = strtolower($compress); if ($compress == 'tgz' || $compress == 'tbz' || $compress == 'tar') { require_once _EXT_PATH . '/libraries/Tar.php'; if (is_string($files)) { $files = array($files); } if ($autoExt) { $archive .= '.' . $compress; } if ($compress == 'tgz') { $compress = 'gz'; } if ($compress == 'tbz') { $compress = 'bz2'; } $tar = new Archive_Tar($archive, $compress); $tar->setErrorHandling(PEAR_ERROR_PRINT); $result = $tar->addModify($files, $addPath, $removePath); return $result; } if ($compress == 'zip') { /*require_once( _EXT_PATH.'/libraries/lib_zip.php' ); $zip = new ZipFile(); $zip->addFileList($files, $removePath ); return $zip->save($archive); */ require_once _EXT_PATH . '/libraries/Zip.php'; $zip = new Archive_Zip($archive); $result = $zip->add($files, array('add_path' => $addPath, 'remove_path' => $removePath)); /*require_once( _EXT_PATH.'/libraries/pclzip.lib.php' ); $zip = new PclZip($archive); $result = $zip->add($files, PCLZIP_OPT_ADD_PATH, $addPath, PCLZIP_OPT_REMOVE_PATH, $removePath ); */ if ($result == 0) { return new PEAR_Error('Unrecoverable error "' . $zip->errorInfo(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()); } // add the main fileset to the list of filesets to process. $mainFileSet = new FileSet($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) { $ds = $fs->getDirectoryScanner($this->project); $files = $ds->getIncludedFiles(); 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) { $ds = $fs->getDirectoryScanner($this->project); $files = $ds->getIncludedFiles(); // FIXME // Current model is only adding directories implicitly. This // won't add any empty directories. Perhaps modify FileSet::getFiles() // to also include empty directories. Not high priority, since non-inclusion // of empty dirs is probably not unexpected behavior for ZipTask. $fsBasedir = $fs->getDir($this->project); $filesToZip = array(); for ($i = 0, $fcount = count($files); $i < $fcount; $i++) { $f = new PhingFile($fsBasedir, $files[$i]); $filesToZip[] = $f->getAbsolutePath(); } $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 ($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; }