php 7z,使用PHP列出.7z,.rar和.tar档案中的文件

这是以前出现的(由于各种原因,如

this和

this和

the one with broken links in the answer).

通常,目前的普遍意见是创建一个依赖于服务器上可以访问的7-zip二进制(可执行文件)的包装(或DIY或使用a library),并使用exec()将调用包装到二进制文件,而不是一个纯PHP解决方案.

根据你的需要动机,这让你有:

>将7-zip二进制文件添加到您的服务器,并使用包装库,无论是您自己的还是someone else’s

>安装和使用非官方的PECL extension

>勇敢地将LZMA SDK自动移植到PHP(并希望可以将其作为开源!)

对于其他格式,您可以查看PHP文档中的示例和用法的详细信息:

由于所有这些都涉及PECL扩展,如果您以某种方式受到您的webhost的限制,并且需要纯PHP解决方案,那么可能更容易转移到更易于使用的Webhost.

为了防止压缩弹,您可以按照this answer(打包大小除以打包的大小除以某个阈值以外的任何东西都视为无效),建议的压缩比来看,尽管拉链炸弹谈到了answer to one of the linked questions,这表明这可以对多层拉链炸弹无效.对于那些您需要查看您列出的文件是否为归档文件,确保您不进行任何类型的递归提取,然后将包含归档的归档视为无效.

为了完整性,官方PECL扩展的一些用法示例:

RAR:

// open the archive file

$archive = RarArchive::open('archive.rar');

// make sure it's valid

if ($archive === false) return;

// retrieve a list of entries in the archive

$entries = $archive->getEntries();

// make sure the entry list is valid

if ($entries === false) return;

// example output of entry count

echo "Found ".count($entries)." entries.\n";

// loop over entries

foreach ($entries as $e) {

echo $e->getName()."\n";

}

// close the archive file

$archive->close();

?>

柏油:

// open the archive file

try {

$archive = new PharData('archive.tar');

}

// make sure it's valid

catch (UnexpectedValueException $e) {

return;

}

// make sure the entry list is valid

if ($archive->count() === 0) return;

// example output of entry count

echo "Found ".$archive->count()." entries.\n";

// loop over entries (PharData is already a list of entries in the archive)

foreach ($archive as $entry) {

echo $entry."\n";

}

// no need to close a PharData

?>

ZIP(从OP的问题改编自here):

// open the archive file

$archive = new ZipArchive;

$valid = $archive->open('archive.zip');

// make sure it's valid (if not ZipArchive::open() returns varIoUs error codes)

if ($valid !== true) return;

// make sure the entry list is valid

if ($archive->numFiles === 0) return;

// example output of entry count

echo "Found ".$archive->numFiles." entries.\n";

// loop over entries

for ($i = 0; $i < $archive->numFiles; $i++) {

$e = $archive->statIndex($i);

echo $e['name']."\n";

}

// close the archive file (redundant as called automatically at the end of the script)

$archive->close();

?>

GZ:

由于gz(gnu Zlib)是一种压缩机制而不是归档格式,因此在PHP中是不同的.如果您打开一个.gz文件(而不是像.tar一样处理它)与gzopen(),它的任何读取都被透明地解压缩.由于这是最常见的.tar.gz,您可以像上面那样对待它(见this answer on another question).或者您可以在this answer on another question中提取PharData::decompress()的焦油.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值