使用不同的文件名将文件添加到 PHP 中的 Tar 存档 [英] Adding files to a Tar Archive in PHP with different filenames
问题描述
我目前正在使用 Archive-Tar Pear 扩展
用于 PHP 将文件集合添加到 Tar 存档中.
I'm currently using the Archive-Tar Pear extension
for PHP to add a collection of files into a Tar Archive.
这些文件存储在带有额外扩展名的文件管理器中例如
These files are stored on a filer with an extra extension e.g.
filename.tgz.104850209.t or filename2.doc.2154395.t
我想在添加文件时删除这个额外的扩展名,以便我的 Tar Archive 包含以下文件:filename.tgz 和 filename2.doc
I'd like to remove this extra extension while adding the files so that my Tar Archive would have the files: filename.tgz and filename2.doc
有没有办法在添加到存档之前不必先复制/重命名源文件?
Is there a way of doing that without having to copy/rename the source files first before adding to the Archive?
谢谢,标记.
推荐答案
Archive_Tar
的最新版本尚不支持这种开箱即用的功能.部分功能在 _addFile()
中,另一部分在 _addString()
中.
Archive_Tar
in its latest version does not yet support such a functionality out of the box. Part of the functionality is in _addFile()
and the other part in _addString()
.
最简单的方法可能是从 Archive_Tar
扩展并将所有调用代理到 _writeHeaderBlock()
这是公共的,在文件名参数上应用映射,以便在写入标题时重命名它.
Most easy is probably to extend from Archive_Tar
and proxy all calls to _writeHeaderBlock()
which is public, applying a map on the filename parameter so to rename it when written into headers.
class Patched_Archive_Tar extends Archive_Tar
{
var $renameMap = array();
function _writeHeaderBlock($p_filename, $p_size, $p_mtime=0, $p_perms=0,
$p_type='', $p_uid=0, $p_gid=0)
{
return parent::_writeHeaderBlock($this->_translateFilename($p_filename),
$p_size, $p_mtime=0, $p_perms=0,
$p_type='', $p_uid=0, $p_gid=0);
}
function _translateFilename($orignal)
{
$map = $this->renameMap;
if (isset($map[$orignal])) {
return $map[$orignal];
}
return $original;
}
}
用法:
$obj = new Patched_Archive_Tar('dummy.tar'); // name of archive
$files = array('mystuff/ad.gif',
'mystuff/alcon.doc.t',
'mystuff/alcon.xls.t'); // files to store in archive
$obj->renameMap = array(
'mystuff/alcon.doc.t' => 'mystuff/alcon.doc',
'mystuff/alcon.xls.t' => 'mystuff/alcon.xls',
) // files to rename
if ($obj->create($files)) {
echo 'Created successfully!';
} else {
echo 'Error in file creation';
}
这又快又脏,但希望有用.为了更好地查看我在开头注意到的函数 _addFile()
和 _addString()
,您基本上想要另一个能够添加文件的函数(与 _addFile()
) 通过指定文件名(与 _addString()
一样).
This is quick and dirty but hopefully worky. For something better see the function I noticed at the beginning _addFile()
and _addString()
, you basically want another one that is able to add a file (as with _addFile()
) by specifiying the filename (as with _addString()
).
这篇关于使用不同的文件名将文件添加到 PHP 中的 Tar 存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!