本文整理汇总了PHP中PharData::addFile方法的典型用法代码示例。如果您正苦于以下问题:PHP PharData::addFile方法的具体用法?PHP PharData::addFile怎么用?PHP PharData::addFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PharData
的用法示例。
在下文中一共展示了PharData::addFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addFile
/**
* @inheritDoc
*/
public function addFile($file, $local_name)
{
try {
$this->phar->addFile($file, $local_name);
} catch (\PharException $e) {
return false;
}
return true;
}
示例2: createArchive
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;
}
}
示例3: addFile
/**
* @param string $filePath It is the path to access the file.
* @param string $directoryInArchive This is the directory where it will be stored in the archive
* @param null|string $name The name of the file in the archive. if it null or empty, it keeps the same name
* @param bool $isOnline
* @return $this
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\FileNotReadableException
* @throws \ErrorException
*
* This methods adds a file in the archive.
* If the file is local, $isOnline must be false,
* If the file online, $filePath must be an URL.
*/
public function addFile($filePath, $directoryInArchive = "/", $name = null, $isOnline = false)
{
if (!empty($name)) {
$dirName = dirname($name);
if ($dirName == ".") {
$dirName = "";
}
$directoryInArchive .= '/' . $dirName;
}
if (empty($name) || !is_scalar($name)) {
$name = basename($filePath);
} else {
$name = basename($name);
}
/**
* Download the file if it is online
* If it's local check if the file exists and if it is redable
*/
$fileDownloadCache = $this->cacheDir . DS . md5(uniqid()) . ".tmp";
$this->copyFile($filePath, $fileDownloadCache, $isOnline);
/**
* Then write the file in the archive
*/
$directoryInArchive = $this->formatDirectoryPath($directoryInArchive);
if (!empty($directoryInArchive)) {
$name = $this->formatFilePath($directoryInArchive . $name);
}
$this->tar->addFile($filePath, $name);
/**
* And clear the download temp file
*/
unlink($fileDownloadCache);
return $this;
}
示例4: create
/**
* Create package.
*/
public function create(array $args = array())
{
$archBasename = $this->pkg->getSimpleName() . '-' . $this->pkg->getPrettyVersion();
/* Work around bug #67417 [NEW]: ::compress modifies archive basename
creates temp file and rename it */
$tempName = getcwd() . '/pkl-tmp.tar';
if (file_exists($tempName)) {
unlink($tempName);
}
$arch = new \PharData($tempName);
$pkgDir = $this->pkg->getRootDir();
foreach ($this->pkg->getFiles() as $file) {
if (is_file($file)) {
$name = str_replace($pkgDir, '', $file);
$arch->addFile($file, $name);
}
}
if (file_exists($tempName)) {
@unlink($tempName . '.gz');
}
$arch->compress(\Phar::GZ);
unset($arch);
rename($tempName . '.gz', $archBasename . '.tgz');
unlink($tempName);
if ($this->cb) {
$cb = $this->cb;
$cb($this->pkg);
}
}
示例5: imgCreate
public function imgCreate($tag, $dkFileData)
{
file_put_contents('Dockerfile', $dkFileData);
$dkfiletar = new PharData('Dockerfile.tar');
$dkfiletar->addFile('Dockerfile');
//执行此行代码后生成Dockerfile.tar.gz文件
$dkfiletar->compress(Phar::GZ);
$this->cmObj->curlPostFile(DOCKER_URL . '/build?t=' . $tag . '&nocache=1', 'Dockerfile.tar.gz', array('Content-Type:application/tar'));
return $this->cmObj->curlHttpCode();
}
示例6: compress
/**
* Create tgz archive.
*/
public function compress()
{
$tarFile = $this->rootDir . '/' . $this->tmpName . '.tar';
$tgzFile = $this->rootDir . '/' . $this->tmpName . '.tgz';
$phar = new \PharData($tarFile);
foreach ($this->files as $file) {
$phar->addFile($file[0], $file[1]);
}
// Compress .tar file to .tgz
$phar->compress(\Phar::GZ, '.tgz');
rename($tgzFile, $this->rootDir . '/' . $this->name);
// Both files (.tar and .tgz) exist. Remove the temporary .tar archive
unlink($tarFile);
}
示例7: create
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;
}
示例8: _zipFile
/**
* 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);
}
示例9: setUpBeforeClass
/**
* Create an empty.box in the temp dir that we can use to test
* compression/decompression and adding metadata.json to it.
*
* PHP's PharData does NOT work with vfsStream so we're required
* to put this on the actual filesystem.
*/
public static function setUpBeforeClass()
{
$fixturesPath = $GLOBALS['PHPUNIT_FIXTURES_DIR'] . DIRECTORY_SEPARATOR . 'empty.box';
$p = new \PharData("tmp.tar");
$emptyBoxFiles = array('box.ovf', 'box-disk1.vmdk', 'metadata.json', 'Vagrantfile');
foreach ($emptyBoxFiles as $file) {
$p->addFile("{$fixturesPath}/{$file}", basename($file));
}
$p->compress(\Phar::GZ);
unset($p);
// Windows: make $p release the tmp.tar file
if (!unlink("tmp.tar")) {
fwrite(STDERR, "unlink('tmp.tar') failed in " . getcwd() . "\n");
system('ls -la');
}
rename("tmp.tar.gz", "empty.box");
self::$emptyPackageBox = file_get_contents("empty.box");
}
示例10: clean
public function clean()
{
# Maximum size of log file allowed, in bytes ( 100000000 = 100 MB)
$max_size = 100000000;
chdir(LOG_PATH);
foreach (glob("*.log") as $_file) {
if (filesize($_file) >= $max_size) {
$tar = new \PharData(basename($_file, ".log") . '-error-log-archive.tar');
$tar->addFile($_file);
$tar->compress(\Phar::GZ);
# Move tarball to archives folder once complete
if (is_readable('archive/' . $_file . '-error-log-archive.tar')) {
rename($_file . '-error-log-archive.tar', 'archive/' . $_file . '-error-log-archive.tar');
} else {
rename($_file . '-error-log-archive.tar', 'archive/' . $_file . '_' . time() . '-error-log-archive.tar');
}
}
}
}
示例11: create
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;
}
示例12: getTar
private function getTar($filesToInclude, $docId, $filesPath, $tempPath)
{
$tarball = $tempPath . uniqid($docId, true) . '.tar';
$phar = null;
try {
$phar = new PharData($tarball);
} catch (UnexpectedValueException $e) {
$this->logErrorMessage('could not create tarball archive file ' . $tarball . ' due to insufficient file system permissions: ' . $e->getMessage());
throw new Oai_Model_Exception('error while creating tarball container: could not open tarball');
}
foreach ($filesToInclude as $file) {
$filePath = $filesPath . $docId . DIRECTORY_SEPARATOR;
try {
$phar->addFile($filePath . $file->getPathName(), $file->getPathName());
} catch (Exception $e) {
$this->logErrorMessage('could not add ' . $file->getPathName() . ' to tarball archive file: ' . $e->getMessage());
throw new Oai_Model_Exception('error while creating tarball container: could not add file to tarball');
}
}
return $tarball;
}
示例13: downloadFolder
/**
* Download Folder
*
* @param array $params
*
* @return void
* @throws \Exception
*/
public function downloadFolder(array $params)
{
try {
if (extension_loaded('Phar')) {
/**
* Decode Path
*/
$base64 = base64_decode($params['dir']);
/**
* Path to File on Disk
*/
$path = $this->getSearchPath() . '/' . $base64;
clearstatcache();
if (is_dir($path)) {
$array = $this->directoryListing($base64);
if (is_array($array) && count($array) > '0') {
unset($array['totalLength']);
unset($array['totalFileSize']);
$filename = $path . '.' . $params['format'];
$phar = new \PharData($filename);
foreach ($array as $value) {
$phar->addFile($value['fullPath'], $value['name']);
}
switch ($params['format']) {
case 'tar':
header('Content-Type: application/x-tar');
break;
case 'zip':
header('Content-Type: application/zip');
break;
case 'bz2':
header('Content-Type: application/x-bzip2');
break;
case 'rar':
header('Content-Type: x-rar-compressed');
break;
}
header('Content-disposition: attachment; filename=' . basename($filename));
header('Content-Length: ' . filesize($filename));
readfile($filename);
unlink($filename);
exit;
} else {
throw new \Exception($this->translate->translate('Something went wrong and we cannot download this folder', 'mp3'));
}
} else {
throw new \Exception($path . ' ' . $this->translate->translate('was not found', 'mp3'));
}
} else {
throw new \Exception($this->translate->translate('Phar Extension is not loaded'));
}
} catch (\Exception $e) {
throw $e;
}
}
示例14: caZipDirectory
function caZipDirectory($ps_directory, $ps_name, $ps_output_file)
{
$va_files_to_zip = caGetDirectoryContentsAsList($ps_directory);
$vs_tmp_name = caGetTempFileName('caZipDirectory', 'zip');
$o_phar = new PharData($vs_tmp_name, null, null, Phar::ZIP);
foreach ($va_files_to_zip as $vs_file) {
$vs_name = str_replace($ps_directory, $ps_name, $vs_file);
$o_phar->addFile($vs_file, $vs_name);
}
copy($vs_tmp_name, $ps_output_file);
unlink($vs_tmp_name);
return true;
}
示例15: addExtraFiles
/**
* Add the extra files to the package.
* @throws HttpDeployerException
*/
private function addExtraFiles()
{
$this->logger->info("Adding the extra files into the package.");
try {
$phar = new \PharData($this->package);
foreach ($this->getExtraFiles() as $from => $to) {
$phar->addFile($from, $to);
}
} catch (\Exception $e) {
throw new HttpDeployerException("An error ocurred while adding the extra files to the package.", 0, $e);
}
}