public function testAutoRollback() { // Setup $this->autoRollbackHelper(); $this->backupFile->buildFromDirectory($this->archivedDir); $this->backupFile->addEmptyDir("testDirectory"); $this->backupFile->compress(\Phar::GZ, '.tgz'); $newFile = $this->backupPath . '/' . uniqid() . '_code.tgz'; copy($this->backupFileName, $newFile); if (file_exists($this->backupFileName)) { unset($this->backupFile); unlink($this->backupFileName); } $gtzFile = str_replace('tar', 'tgz', $this->backupFileName); if (file_exists($gtzFile)) { unlink($gtzFile); } $this->backupFileName = $newFile; // Change the contents of a.txt $this->autoRollbackHelper(1); $this->assertEquals('foo changed', file_get_contents($this->archivedDir . 'a.txt')); $this->backupInfo->expects($this->once())->method('getBlacklist')->willReturn(['excluded']); // Rollback process $this->rollBack->execute($this->backupFileName); // Assert that the contents of a.txt has been restored properly $this->assertEquals('foo', file_get_contents($this->archivedDir . 'a.txt')); }
public static function createArchive($arg, $origin_dir, $archive_file, $overwrite = false) { if (!extension_loaded('phar')) { throw new pakeException(__CLASS__ . ' module requires "phar" extension'); } if (false === $overwrite and file_exists($archive_file)) { return true; } if (self::endsWith($archive_file, '.tar.gz')) { $archive_file = substr($archive_file, 0, -3); $compress = Phar::GZ; $extension = '.tar.gz'; if (!extension_loaded('zlib')) { throw new pakeException('GZip compression method is not available on this system (install "zlib" extension)'); } } elseif (self::endsWith($archive_file, '.tgz')) { $archive_file = substr($archive_file, 0, -3) . 'tar'; $compress = Phar::GZ; $extension = '.tgz'; if (!extension_loaded('zlib')) { throw new pakeException('GZip compression method is not available on this system (install "zlib" extension)'); } } elseif (self::endsWith($archive_file, '.tar.bz2')) { $archive_file = substr($archive_file, 0, -4); $compress = Phar::BZ2; $extension = '.tar.bz2'; if (!extension_loaded('bz2')) { throw new pakeException('BZip2 compression method is not available on this system (install "bzip2" extension)'); } } elseif (self::endsWith($archive_file, '.tar') or self::endsWith($archive_file, '.zip')) { $compress = Phar::NONE; } else { throw new pakeException("Only .zip, .tar, .tar.gz and .tar.bz2 archives are supported"); } $files = pakeFinder::get_files_from_argument($arg, $origin_dir, true); pake_echo_action('file+', $archive_file); try { $arc = new PharData($archive_file); foreach ($files as $file) { $full_path = $origin_dir . '/' . $file; pake_echo_action('archive', '-> ' . $file); if (is_dir($full_path)) { $arc->addEmptyDir($file); } else { $arc->addFile($full_path, $file); } } if (Phar::NONE !== $compress) { $new_name = substr($archive_file, 0, -4) . $extension; pake_echo_action('file+', $new_name); $arc->compress($compress, $extension); unset($arc); pake_remove($archive_file, '/'); } } catch (PharException $e) { unset($arc); pake_remove($archive_file); throw $e; } }
/** * @param $directoryPath * @return $this * @throws \ErrorException * * This method creates an empty directory */ public function addDirectory($directoryPath) { $directoryInArchive = $this->formatDirectoryPath($directoryPath); if (!empty($directoryInArchive)) { try { $this->tar->addEmptyDir($directoryInArchive); } catch (\Exception $e) { throw new \ErrorException($this->translator->trans("The directory %dir has not been created in the archive", ["%dir" => $directoryInArchive])); } } return $this; }
public function create($execution, $format, $hrefs) { $this->dirs = array(); $this->files = array(); $this->sc401 = false; $this->add_hrefs($hrefs); if ($this->sc401) { return 401; } else { if (count($this->dirs) === 0 && count($this->files) === 0) { return 404; } } $target = H5ai::normalize_path(sys_get_temp_dir(), true) . "h5ai-selection-" . microtime(true) . rand() . "." . $format; try { if ($execution === "shell") { if ($format === "tar") { $cmd = Archive::$TAR_CMD; } else { if ($format === "zip") { $cmd = Archive::$ZIP_CMD; } else { return null; } } $cmd = str_replace("[ROOTDIR]", "\"" . $this->h5ai->getRootAbsPath() . "\"", $cmd); $cmd = str_replace("[TARGET]", "\"" . $target . "\"", $cmd); $cmd = str_replace("[DIRS]", count($this->dirs) ? "\"" . implode("\" \"", array_values($this->dirs)) . "\"" : "", $cmd); $cmd = str_replace("[FILES]", count($this->files) ? "\"" . implode("\" \"", array_values($this->files)) . "\"" : "", $cmd); `{$cmd}`; } else { if ($execution === "php") { $archive = new PharData($target); foreach ($this->dirs as $archivedDir) { $archive->addEmptyDir($archivedDir); } foreach ($this->files as $realFile => $archivedFile) { $archive->addFile($realFile, $archivedFile); // very, very slow :/ } } } } catch (Exeption $err) { return 500; } return @filesize($target) ? $target : null; }
public function create($execution, $format, $hrefs) { $this->dirs = array(); $this->files = array(); $this->add_hrefs($hrefs); if (count($this->dirs) === 0 && count($this->files) === 0) { return 404; } $target = $this->app->get_cache_abs_path() . "/package-" . sha1(microtime(true) . rand()) . "." . $format; try { if ($execution === "shell") { if ($format === "tar") { $cmd = Archive::$TAR_CMD; } else { if ($format === "zip") { $cmd = Archive::$ZIP_CMD; } else { return null; } } // $cmd = str_replace("[ROOTDIR]", "\"" . $this->app->get_root_abs_path() . "\"", $cmd); $cmd = str_replace("[ROOTDIR]", "\"" . $this->app->get_abs_path() . "\"", $cmd); $cmd = str_replace("[TARGET]", "\"" . $target . "\"", $cmd); $cmd = str_replace("[DIRS]", count($this->dirs) ? "\"" . implode("\" \"", array_values($this->dirs)) . "\"" : "", $cmd); $cmd = str_replace("[FILES]", count($this->files) ? "\"" . implode("\" \"", array_values($this->files)) . "\"" : "", $cmd); shell_exec($cmd); } else { if ($execution === "php") { $archive = new PharData($target); foreach ($this->dirs as $archived_dir) { $archive->addEmptyDir($archived_dir); } foreach ($this->files as $real_file => $archived_file) { $archive->addFile($real_file, $archived_file); // very, very slow :/ } } } } catch (Exeption $err) { return 500; } return @filesize($target) ? $target : null; }
/** * テンプレート一覧からのダウンロード * * @param Application $app * @param Request $request * @param $id */ public function download(Application $app, Request $request, $id) { /** @var $Template \Eccube\Entity\Template */ $Template = $app['eccube.repository.template']->find($id); if (!$Template) { throw new NotFoundHttpException(); } // 該当テンプレートのディレクトリ $config = $app['config']; $templateCode = $Template->getCode(); $targetRealDir = $config['root_dir'] . '/app/template/' . $templateCode; $targetHtmlRealDir = $config['root_dir'] . '/html/template/' . $templateCode; // 一時ディレクトリ $uniqId = sha1(Str::random(32)); $tmpDir = $config['template_temp_realdir'] . '/' . $uniqId; $appDir = $tmpDir . '/app'; $htmlDir = $tmpDir . '/html'; // ファイル名 $tarFile = $config['template_temp_realdir'] . '/' . $uniqId . '.tar'; $tarGzFile = $tarFile . '.gz'; $downloadFileName = $Template->getCode() . '.tar.gz'; // 該当テンプレートを一時ディレクトリへコピーする. $fs = new Filesystem(); $fs->mkdir(array($appDir, $htmlDir)); $fs->mirror($targetRealDir, $appDir); $fs->mirror($targetHtmlRealDir, $htmlDir); // tar.gzファイルに圧縮する. $phar = new \PharData($tarFile); $phar->buildFromDirectory($tmpDir); // appディレクトリがない場合は, 空ディレクトリを追加 // @see https://github.com/EC-CUBE/ec-cube/issues/742 if (empty($phar['app'])) { $phar->addEmptyDir('app'); } $phar->compress(\Phar::GZ); // ダウンロード完了後にファイルを削除する. // http://stackoverflow.com/questions/15238897/removing-file-after-delivering-response-with-silex-symfony $app->finish(function (Request $request, Response $response, \Silex\Application $app) use($tmpDir, $tarFile, $tarGzFile) { $app['monolog']->addDebug('remove temp file: ' . $tmpDir); $app['monolog']->addDebug('remove temp file: ' . $tarFile); $app['monolog']->addDebug('remove temp file: ' . $tarGzFile); $fs = new Filesystem(); $fs->remove($tmpDir); $fs->remove($tarFile); $fs->remove($tarGzFile); }); return $app->sendFile($tarGzFile)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $downloadFileName); }
public function tarData($tarFile, $cron) { $log = vglobal('log'); $log->info('BackUp - Start ZipArchive'); $dbFiles = $this->getFilesStructure(); $destination = $this->tempDir . '/' . $tarFile . '.tar'; $tar = new PharData($destination); $count = 0; foreach ($dbFiles as $id => $path) { if (is_dir($path)) { $tar->addEmptyDir($path . '/'); } elseif (is_file($path)) { $tar->addFile($path, $path); } $this->markFile($id); if ($count == $this->ajaxFilesLimit && $cron == FALSE) { $percentage = $this->getPercentage(); exit(json_encode(['percentage' => $percentage])); } $count++; } //$tar->compress(Phar::GZ); }
<?php $fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.tar'; $fname2 = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.2.tar'; $phar = new PharData($fname); $phar->addEmptyDir('blah/'); $phar->addFromString('test/', ''); copy($fname, $fname2); $phar = new PharData($fname2); var_dump($phar['blah']->isDir(), $phar['test']->isDir()); ?> ===DONE=== <?php error_reporting(0); unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.tar'); unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.2.tar'); __halt_compiler(); ?>
$fname = IA_ROOT . "/data/tmp.tar"; $phar = new PharData($fname); $phar->addFromString('/manifest.xml', $manifest); if ($mDefine) { $phar->addFromString('/module.php', $mDefine); } if ($pDefine) { $phar->addFromString('/processor.php', $pDefine); } if ($rDefine) { $phar->addFromString('/receiver.php', $rDefine); } if ($sDefine) { $phar->addFromString('/site.php', $sDefine); } $phar->addEmptyDir('/template'); if ($m['application']['setting']) { $phar->addFromString("/template/setting.html", "{template 'common/header'}\r\n这里定义页面内容\r\n{template 'common/footer'}"); } if ($m['icon']) { $phar->addFile($m['icon'], '/icon.jpg'); @unlink($m['icon']); } if ($m['preview']) { $phar->addFile($m['preview'], '/preview.jpg'); @unlink($m['preview']); } header('content-type: application/tar'); header('content-disposition: attachment; filename="' . $ident . '.tar"'); readfile($fname); unset($phar);
/** * Archiving the file * * @param unknown $filePath * @param string $preFix * @param string $debug * @throws Exception */ private static function _zipFile($files, $preFix = '', $debug = false) { $tarFilePath = self::$_outputFileDir . '/' . self::FILE_NAME . '.tar'; $start = self::_log('== Archiving the file: ' . $tarFilePath, __CLASS__ . '::' . __FUNCTION__, $preFix); $csvFilePath = '/tmp/' . md5('ProductToMagento_CSV_' . trim(UDate::now())) . '.csv'; $tarFile = new PharData($tarFilePath); //add csv file self::_log('Generating the CSV file: ' . $csvFilePath, '', $preFix . self::TAB); $objWriter = PHPExcel_IOFactory::createWriter($files['phpExcel'], 'CSV'); $objWriter->save($csvFilePath); self::_log('Adding the CSV file to: ' . $tarFilePath, '', $preFix . self::TAB); $tarFile->addFile($csvFilePath, self::FILE_NAME . '.csv'); //add image files if (isset($files['imageFiles']) && count($files['imageFiles']) > 0) { $imageDir = self::$_imageDirName; $tarFile->addEmptyDir($imageDir); foreach ($files['imageFiles'] as $index => $imageFile) { self::_log('Processing file: ' . $index, '', $preFix . self::TAB . self::TAB); if (!isset($imageFile['filePath'])) { self::_log('No File Path SET. SKIP ', '', $preFix . self::TAB . self::TAB . self::TAB); continue; } if (!is_file($imageFile['filePath'])) { self::_log('File NOT FOUND: ' . $imageFile['filePath'], '', $preFix . self::TAB . self::TAB . self::TAB); continue; } $tarFile->addFile($imageFile['filePath'], $imageDir . '/' . $imageFile['fileName']); self::_log('Added File:' . $imageFile['fileName'] . ', from path: ' . $imageFile['filePath'], '', $preFix . self::TAB . self::TAB . self::TAB); } } else { self::_log('No image files to add.', '', $preFix . self::TAB); } // COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz self::_log('Compressing file: ' . $tarFilePath, '', $preFix . self::TAB . self::TAB . self::TAB); $tarFile->compress(Phar::GZ); // NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar self::_log('REMOVING the orginal file: ' . $tarFilePath, '', $preFix . self::TAB); unlink($tarFilePath); self::_log('REMOVED', '', $preFix . self::TAB . self::TAB); //remving temp csv file self::_log('REMOVING the tmp csv file: ' . $csvFilePath, '', $preFix . self::TAB); unlink($csvFilePath); self::_log('REMOVED', '', $preFix . self::TAB . self::TAB); self::_log('== Archived', __CLASS__ . '::' . __FUNCTION__, $preFix, $start); }