使用PHP列出.7z,.rar和.tar档案中的文件
我想在档案中列出文件,而不需要提取.
我感兴趣的档案类型: > .7z(7-Zip) 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,但也需要它们.
这是以前出现的(由于各种原因,如
this和
this和
the 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 对于其他格式,您可以查看PHP文档中的示例和用法的详细信息: > .rar有自己的official PECL extension 由于所有这些都涉及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一样处理它)与 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |