简体   繁体   English

从 PHP 中的 Phar 存档复制特定目录

[英]Copy a specific directory from Phar archive in PHP

All is in the header.一切都在标题中。 My application is stored in Phar.我的应用程序存储在 Phar 中。 My workflow is really simple.我的工作流程非常简单。

I work with PHP 5.6 .我使用PHP 5.6

I do some stuff in my application and at a moment, I would like to copy a specific directory stored inside my Phar archive into a specific folder outside the archive.我在我的应用程序中做了一些事情,我想将存储在我的 Phar 存档中的特定目录复制到存档外的特定文件夹中。

I read lots of things about Phar::extractTo method, but it does not work if I select specific directory.我阅读了很多关于Phar::extractTo方法的内容,但如果我选择特定目录,它就不起作用。 For example:例如:

$phar->extractTo("/external_path", "folder_inside_phar");

shouts me that folder_inside_phar is non-existent file in Phar archive.告诉我folder_inside_phar是 Phar 存档中不存在的文件。 This function has been declared has buggy because it does not understand folder.此函数已被声明有错误,因为它不了解文件夹。 It only accepts files for the second argument.它只接受第二个参数的文件。

I precise that if I do not mention specific folder, it copies all content of the archive.我确切地说,如果我不提及特定文件夹,它会复制存档的所有内容。 There is not any problem about Phar access or readonly flag... Phar访问或只读标志没有任何问题......

So, is there any workaround for that, or a class developed by someone as open-source content to resolve this issue ?那么,是否有任何解决方法或由某人开发的类作为开源内容来解决此问题?

Edit I change the word "extract" by "copy"编辑我将“提取”一词改为“复制”

Indeed extractTo does not work with reading from a Phar directory directly - so, I'd like to share a potential work around here, basically using the Phar Stream Wrapper phar:// which allows to apply any PHP file function ( file_get_contents , scandir , ...) to Phar archives as well.实际上, extractTo不能直接从 Phar 目录中读取 - 所以,我想在这里分享一个潜在的工作,基本上使用 Phar Stream Wrapper phar://它允许应用任何 PHP 文件函数( file_get_contentsscandir , ...) 到 Phar 档案也是如此。

Let's assume the Phar builder scripts looks like this (I used addFromString to have a simple example):让我们假设 Phar 构建器脚本如下所示(我使用addFromString有一个简单的例子):

<?php
$phar = new Phar('bundle.phar');
$phar->startBuffering();
$phar->addFromString('a/a1.txt', 'contents of a/a1.txt');
$phar->addFromString('a/a2.txt', 'contents of a/a2.txt');
$phar->addFromString('b/b.txt', 'contents of b/b.txt');
$phar->setStub('<?php __HALT_COMPILER();');
$phar->stopBuffering();

Extracting files in directory a/ now could look like the following:现在在目录a/提取文件可能如下所示:

<?php
$targetDirectory = __DIR__ . '/target';
if (!is_dir($targetDirectory)) {
  mkdir($targetDirectory);
}
// read directory using Phar Stream Wrapper
// resolves to `['a1.txt', 'a2.txt']`
$files = scandir('phar://./bundle.phar/a/');
// prefix each file with local path `a/`, e.g. `a1.txt` -> `a/a1.txt`
// resolves to `['a/a1.txt', 'a/a2.txt']`
$files = array_map(function($file) { return 'a/' . $file; }, $files);
// actually extract resolved files to `$targetDirectory`
$phar = new \Phar('./bundle.phar');
$phar->extractTo($targetDirectory, $files);

Thus, the main aspect here is that any file function can be used for stream wrappers as well - eg the following would work as well:因此,这里的主要方面是任何文件函数也可以用于流包装器 - 例如以下也可以工作:

copy('phar://./bundle.phar/b/b.txt', './target/b.txt');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM