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

前端之家收集整理的这篇文章主要介绍了使用PHP列出.7z,.rar和.tar档案中的文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在档案中列出文件,而不需要提取.

我感兴趣的档案类型:

> .7z(7-Zip)
> .rar(WinRAR)
> .tar(POSIX,例如GNU tar).
> .zip(ISO标准,例如WinZip)

For .zip files,我已经能够实现这一点:

<?PHP
    $za = new ZipArchive();
    $za->open('theZip.zip');
    for ($i = 0; $i < $za->numFiles; $i++) {
        $stat = $za->statIndex($i);
        print_r(basename($stat['name']) . PHP_EOL);
    }
?>

但是,我并没有为.7z文件做同样的事情.没有测试过.rar和.tar,但也需要它们.

这是以前出现的(由于各种原因,如 thisthisthe one with broken links in the answer).

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

7zip format supports a variety of compression algorithms以来,我假设你可能想要一个纯粹的PHP实现来读/解压缩LZMA格式.虽然LZMA SDKs available for C,C++,C# and Java有人已经做了一个PHP Extension for LZMA2(和fork for LZMA),尽管甚至有兴趣on the 7-zip forums相当一段时间,没有人似乎已经把它作为一个全面的PECL扩展或纯PHP尚未移植.

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

>将7-zip二进制文件添加到您的服务器,并使用包装库,无论是您自己的还是someone else’s
>安装和使用非官方的PECL extension
>勇敢地将LZMA SDK自动移植到PHP(并希望可以将其作为开源!)

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

> .rar有自己的official PECL extension
> .tar可以由Phar PECL extention提取(另见SO的例子)
> .zip有一个官方PECL extension
> .gz有一个官方PECL exension
>和couple of other formats

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

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

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

RAR:

<?PHP
// 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();
?>

柏油:

<?PHP
// 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):

<?PHP
// 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()的焦油.

原文链接:https://www.f2er.com/php/131478.html

猜你在找的PHP相关文章