如何使用PHP读取.tar.gz文件? [英] How can I read a .tar.gz file with PHP?
问题描述
我正在构建一个供人们上传PHP中的.tar(和.tar.gz,.tar.bz2,.zip等)文件的系统.上载文件是可以的,但是在上载档案后,我想列出档案中包含的文件.
I am building a system for people to upload .tar (and .tar.gz, .tar.bz2, .zip, etc) files in PHP. Uploading the files is fine, but I would like to list files contained in the archive after it has been uploaded.
有人可以推荐一个可以读取文件存档的优秀PHP库吗?
Can someone recommend a good PHP library that can read file archives?
我在Pear上发现了 File_Archive ,但是几年来它没有更新. ZipArchive非常适合.zip文件,但我需要一些可以处理更多文件类型的文件.
I found File_Archive on Pear but it hasn't been updated in a few years. ZipArchive works great for .zip files, but I need something that can handle more file types.
更新我正在RHEL6,PHP 5.2和Apache 2.2上运行.
update I'm running on RHEL6, PHP 5.2, and Apache 2.2.
推荐答案
You can do this with the PharData
class:
// Example: list files
$archive = new PharData('/some/file.tar.gz');
foreach($archive as $file) {
echo "$file\n";
}
这甚至可以与phar://
流包装一起使用:
This even works with the phar://
stream wrapper:
$list = scandir('phar:///some/file.tar.gz');
$fd = fopen('phar:///some/file.tar.gz/some/file/in/the/archive', 'r');
$contents = file_get_contents('phar:///some/file.tar.gz/some/file/in/the/archive');
如果您没有Phar,请检查仅PHP实现,或 pecl扩展名.
If you don't have Phar, check the PHP-only implementation, or the pecl extension.
这篇关于如何使用PHP读取.tar.gz文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!