public static function copyDirToArchive($dir, $archive, $basename = null, $excludeDirs = null) { $dir = rtrim($dir, '/\\'); $basename = $basename ?: basename($dir); rex_dir::create(dirname($archive)); $files = array(); $iterator = rex_finder::factory($dir)->recursive()->filesOnly(); if ($excludeDirs) { $iterator->ignoreDirs($excludeDirs, false); } foreach ($iterator as $path => $file) { $subpath = str_replace($dir, $basename, $path); $subpath = str_replace('\\', '/', $subpath); $files[$subpath] = $path; } if (class_exists('ZipArchive')) { $zip = new ZipArchive(); $zip->open($archive, ZipArchive::CREATE); foreach ($files as $path => $realpath) { $zip->addFile($realpath, $path); } $zip->close(); } else { $phar = new PharData($archive, 0, null, Phar::ZIP); $phar->buildFromIterator(new ArrayIterator($files)); $phar->compressFiles(Phar::GZ); foreach ($files as $path => $realpath) { if (filesize($realpath) == 0) { $phar[$path]->decompress(); } } } }
/** * @param string $project * @return array $return */ public function backupArchive($project) { // Exception if not defined if (!isset($this->config[$project]['archive'])) { throw new \Exception('No "Archive config" for this project', 1); } // Get backup settings $settings = $this->getArchiveSettings($project); // Get backup file prefix $backupFilePrefix = ''; if (!empty($settings['backup_file_prefix'])) { $backupFilePrefix = $settings['backup_file_prefix']; } // Set filename $filename = $backupFilePrefix . date('Ymd_His') . '.tar'; // Data required to backup Database $path = $this->getBackupPath($project, 'archive'); // Create Archive $phar = new \PharData($path . '/' . $filename); $phar->buildFromIterator(new \ArrayIterator($this->getArchiveFilesList($project))); // Compress and unlink none compress archive $phar->compress(\Phar::GZ); unlink($path . '/' . $filename); return $this->getBackupInfo($path, $filename); }
/** * {@inheritdoc} */ public function archive($sources, $target, $format, array $excludes = array()) { $sources = realpath($sources); // Phar would otherwise load the file which we don't want if (file_exists($target)) { unlink($target); } try { $filename = substr($target, 0, strrpos($target, $format) - 1); // Check if compress format if (isset(static::$compressFormats[$format])) { // Current compress format supported base on tar $target = $filename . '.tar'; } $phar = new \PharData($target, null, null, static::$formats[$format]); $files = new ArchivableFilesFinder($sources, $excludes); $phar->buildFromIterator($files, $sources); if (isset(static::$compressFormats[$format])) { // Check can be compressed? if (!$phar->canCompress(static::$compressFormats[$format])) { throw new \RuntimeException(sprintf('Can not compress to %s format', $format)); } // Delete old tar unlink($target); // Compress the new tar $phar->compress(static::$compressFormats[$format]); // Make the correct filename $target = $filename . '.' . $format; } return $target; } catch (\UnexpectedValueException $e) { $message = sprintf("Could not create archive '%s' from '%s': %s", $target, $sources, $e->getMessage()); throw new \RuntimeException($message, $e->getCode(), $e); } }
function run_with_iterator(Iterator $it, $root = '') { $tmpnam = tempnam('/tmp', 'testzip'); unlink($tmpnam); $tmpnam .= '.zip'; $pd = new PharData($tmpnam); var_dump($pd->buildFromIterator($it, $root)); $z = zip_open($tmpnam); while ($entry = zip_read($z)) { var_dump([zip_entry_name($entry), zip_entry_filesize($entry)]); } }
public function archive($sources, $target, $format, array $excludes = array()) { $sources = realpath($sources); if (file_exists($target)) { unlink($target); } try { $phar = new \PharData($target, null, null, static::$formats[$format]); $files = new ArchivableFilesFinder($sources, $excludes); $phar->buildFromIterator($files, $sources); return $target; } catch (\UnexpectedValueException $e) { $message = sprintf("Could not create archive '%s' from '%s': %s", $target, $sources, $e->getMessage()); throw new \RuntimeException($message, $e->getCode(), $e); } }
function run_with_iterator(Iterator $it, $root = '') { $tmpnam = tempnam('/tmp', 'testtar'); unlink($tmpnam); $tmpnam .= '.tar'; $pd = new PharData($tmpnam); var_dump($pd->buildFromIterator($it, $root)); // Re-create for read $pd = new PharData($tmpnam); $out = array(); $it = new RecursiveIteratorIterator($pd); foreach ($it as $fname => $data) { $out[$fname] = array(get_class($data), $data->getFilename(), $data->getPathname(), (string) $data); } ksort($out); var_dump($out); }
public function cmd_artifact(array $params = array()) { \System\Directory::check(BASE_DIR . static::DIR_PACKAGES); $target = BASE_DIR . static::DIR_PACKAGES . '/artifact.tar'; $result = $target . '.gz'; if (isset($params[0])) { $target = $params[0]; } if (file_exists($target)) { unlink($target); } if (file_exists($result)) { unlink($result); } $iter = new \RecursiveDirectoryIterator(BASE_DIR); $iter->setFlags(\FileSystemIterator::SKIP_DOTS); $iter = new ProjectDirectoryRecursiveIterator($iter); $iter = new \RecursiveIteratorIterator($iter); $archive = new \PharData($target); $archive->buildFromIterator($iter, BASE_DIR); $archive->compress(\Phar::GZ); unlink($target); }
/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $res = true; $taskRunner = $this->taskRunner; $toBeAdded = []; $srcList = !empty($cmdParams[0]) ? $cmdParams[0] : []; $destFile = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : ''; $srcBaseDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : ''; $format = !empty($cmdParams[3]) ? strtolower($cmdParams[3]) : self::CMP_GZ; $options = !empty($cmdParams[4]) ? $cmdParams[4] : []; if (!empty($srcBaseDir) && !is_dir($srcBaseDir)) { Log::throwException('compress: srcBaseDir has to be a directory'); } if (empty($destFile) || file_exists($destFile) && is_dir($destFile)) { Log::throwException('compress: Destination has to be a file'); } switch ($format) { case self::CMP_NONE: $extension = '.tar'; $compression = \Phar::NONE; $doCompress = false; break; case self::CMP_GZ: $extension = '.tar.gz'; $compression = \Phar::GZ; $doCompress = true; break; case self::CMP_BZ2: $extension = '.tar.bz2'; $compression = \Phar::BZ2; $doCompress = true; break; default: $extension = ''; $compression = false; $doCompress = false; Log::throwException('compress: Invalid format specified: ' . $format); break; } // if $srcList is specified but it is a string, we convert it to an array if (!empty($srcList) && is_string($srcList)) { $srcList = explode(',', $srcList); } foreach ($srcList as $srcPath) { $parsedPath = $taskRunner->parseStringAliases(trim($srcPath)); if (!empty($srcBaseDir)) { $toBeAdded[$parsedPath] = $srcBaseDir . DIRECTORY_SEPARATOR . $parsedPath; } else { $toBeAdded[] = $parsedPath; } } $this->controller->stdout("Creating archive: "); $this->controller->stdout($destFile, Console::FG_BLUE); $archive = null; $destDir = dirname($destFile); $destBaseFile = $destDir . DIRECTORY_SEPARATOR . basename($destFile, '.tar'); if (!$this->controller->dryRun) { if (!is_dir($destDir)) { FileHelper::createDirectory($destDir); } @unlink($destFile); @unlink($destBaseFile); try { $archive = new \PharData($destFile); } catch (\Exception $e) { Log::throwException($e->getMessage()); } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); $this->controller->stdout("Adding to archive: "); foreach ($toBeAdded as $srcRelPath => $srcFullPath) { if (file_exists($srcFullPath)) { $this->controller->stdout("\n - " . $srcFullPath); if (!$this->controller->dryRun) { if (is_dir($srcFullPath)) { $files = FileHelper::findFiles($srcFullPath, $options); $archive->buildFromIterator(new ArrayIterator($files), !empty($srcBaseDir) ? $srcBaseDir : $srcFullPath); } elseif (FileHelper::filterPath($srcFullPath, $options)) { $archive->addFile($srcFullPath, !empty($srcBaseDir) ? $srcRelPath : basename($srcFullPath)); } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } } else { $this->controller->stderr("\n{$srcFullPath} does not exists!\n", Console::FG_RED); } } $this->controller->stdout("\n"); if ($doCompress) { $this->controller->stdout("Compressing archive: "); $this->controller->stdout($destBaseFile . $extension, Console::FG_CYAN); if (!$this->controller->dryRun) { @unlink($destBaseFile . $extension); try { $archive->compress($compression, $extension); } catch (\Exception $e) { Log::throwException($e->getMessage()); } @unlink($destFile); } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); } return $res; }
/** * @throws BuildException */ public function main() { $this->checkPreconditions(); try { $this->log('Building archive: ' . $this->destinationFile->__toString(), Project::MSG_INFO); /** * Delete old archive, if exists. */ if ($this->destinationFile->exists()) { $isDeleted = $this->destinationFile->delete(); if (!$isDeleted) { $this->log("Could not delete destination file {$this->destinationFile}", Project::MSG_WARN); } } $pharData = new PharData($this->baseDirectory->getPath() . '/' . $this->destinationFile->getName()); foreach ($this->filesets as $fileset) { $this->log('Adding specified files in ' . $fileset->getDir($this->project) . ' to archive', Project::MSG_VERBOSE); $pharData->buildFromIterator($fileset->getIterator(), $fileset->getDir($this->project)); } if ($this->compression !== PHAR::NONE && $pharData->canCompress($this->compression)) { try { $pharData->compress($this->compression); } catch (UnexpectedValueException $uve) { $pharData->compressFiles($this->compression); } unset($pharData); } } catch (Exception $e) { throw new BuildException('Problem creating archive: ' . $e->getMessage(), $e, $this->getLocation()); } }
/** * Create PHAR archive * * @param string &$name Pack name * @param \Iterator $iterator Directory iterator * @param array $metadata Archive description OPTIONAL * @param boolean $compress Flag OPTIONAL * * @return \Phar */ protected static function pack(&$name, \Iterator $iterator, array $metadata = array(), $compress = true) { // To prevent existsing files usage \Includes\Utils\FileManager::deleteFile($name); $phar = new \PharData($name); // Files $phar->buildFromIterator($iterator, LC_DIR_ROOT); // Metadata $phar->setMetadata($metadata); // File hashes static::addPackHash($phar, $iterator); // GZ compression return $compress ? static::compress($phar, $name) : $phar; }
/** * Will cleanup log files based on the value set for their maximal number * * @return void */ protected function cleanupFiles() { // skip GC of old logs if files are unlimited if (0 === $this->maxFiles) { return; } $logFiles = glob($this->getGlobPattern()); if ($this->maxFiles >= count($logFiles)) { // no files to remove return; } // Sorting the files by name to remove the older ones usort($logFiles, function ($a, $b) { return strcmp($b, $a); }); // collect the files we have to archive and clean and prepare the archive's internal mapping $oldFiles = array(); foreach (array_slice($logFiles, $this->maxFiles) as $oldFile) { $oldFiles[basename($oldFile)] = $oldFile; } // create an archive from the old files $dateTime = new \DateTime(); $currentTime = $dateTime->format($this->getDateFormat()); $phar = new \PharData($this->originalLogFile . $currentTime . '.tar'); $phar->buildFromIterator(new \ArrayIterator($oldFiles)); // finally delete them as we got them in the archive foreach ($oldFiles as $oldFile) { if (is_writable($oldFile)) { unlink($oldFile); } } }
/** * Rempli une archive déjà créée avec le contenu d'un dossier. * Si erreur, renvoie JSON_FILE_SYSTEM_ERROR et exit. * * @param PharData $phar L'archive à remplir * @param string $include Le dossier à inclure * @param string $basePath Le chemin à partir duquel l'arborescence doit être copiée */ function fillArchive($phar, $include, $basePath) { try { $phar->buildFromIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($include, FilesystemIterator::SKIP_DOTS)), $basePath); } catch (UnexpectedValueException $e) { sendJson(JSON_FILE_SYSTEM_ERROR); } }