在PHP中创建tar时保留文件信息


Preserving file info while creating tar in PHP

在PHP中,我使用Phar对象创建了tar文件,但它不保存文件信息,如修改的时间、权限等。

有没有办法将这些信息保存在创建的tar文件中?

我尝试过buildFromDirectory()、addFile(),但都不起作用。

phar文件格式实际上包含文件的"元数据"-请参阅http://php.net/manual/en/phar.fileformat.manifestfile.php

默认情况下,不会在文件中存储元数据。您可以使用PHP发行版zips:中提供的phar工具进行检查

$ phar meta-get -f phorkie-0.5.0.phar -e www/www-security.php
No Metadata

但既然你问了如何添加它们:使用meta-set:

$ php -dphar.readonly=0 `which phar` meta-set -f phorkie-0.5.0.phar'
      -e www/www-security.php -m 's:8:"hi there";'
$ phar meta-get -f phorkie-0.5.0.phar -e www/www-security.php
s:8:"hi there";

通过编程,您可以使用PharFileInfo::setMetadata()。