在PHP中,是否可以检查Zip文件的内容而不首先提取其内容?
我见过PHP中的ZipArchive类,它允许您读取zip文件。但是我想知道是否有一种方法可以在不首先提取文件的情况下迭代它的内容。发布前搜索。在PHP中,是否可以检查Zip文件的内容而不首先提取其内容?,php,zip,Php,Zip,我见过PHP中的ZipArchive类,它允许您读取zip文件。但是我想知道是否有一种方法可以在不首先提取文件的情况下迭代它的内容。发布前搜索。 作为对以下内容的注释: 下面的代码可用于获取中所有文件名的列表 压缩文件 <?php $za = new ZipArchive(); $za->open('theZip.zip'); for( $i = 0; $i < $za->numFiles; $i++ ){ $stat = $za->statIn
作为对以下内容的注释:
下面的代码可用于获取中所有文件名的列表
压缩文件
<?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 );
}
?>
我这样解决了这个问题
$zip = new \ZipArchive();
$zip->open(storage_path('app/'.$request->vrfile));
$name = '';
//looped through the zip files and got each index name of the files
//since I only wanted the first name which is the folder name I break the loop
//after updating the variable $name with the index name and that's it
for( $i = 0; $i < $zip->numFiles; $i++ ){
$filename = $zip->getNameIndex($i);
var_dump($filename);
$name = $filename;
if ($i == 1){
break;
}
}
var_dump($name);
$zip=new\ZipArchive();
$zip->open(存储路径('app/'.$request->vrfile));
$name='';
//循环浏览zip文件并获得文件的每个索引名
//因为我只想要第一个名字,也就是文件夹名,所以我打破了循环
//在用索引名更新变量$name之后,仅此而已
对于($i=0;$i<$zip->numFiles;$i++){
$filename=$zip->getNameIndex($i);
变量转储($filename);
$name=$filename;
如果($i==1){
打破
}
}
var_dump($name);
lol所以你会因为重复的问题而责骂,然后用答案来补贴你的行为。尼斯:)投票结束重复的问题,而不是重复的答案-你应该在宣讲之前阅读圣经。可能重复感谢你的答案。在查看ZipArchive文档时,我一定错过了numFiles字段。ZipArchive对象的接口很奇怪。@flu:这正是我现在的想法。看起来比没有类的普通老PHP更糟糕。$za在我的例子中只包含true。。。默认情况下,oOzip\u条目\u read只能从文件中读取1024字节。。因此,内容不是文件的完整内容,而是仅前1024个字节..警告:此函数已被弃用,为什么不使用$zip->getNameIndex(0)代码>没有任何循环和中断等?更重要的是,目前这将获得第二个条目。。。
<?php
$zip = zip_open("test.zip");
if (is_resource($zip))
{
while ($zip_entry = zip_read($zip))
{
echo "<p>";
echo "Name: " . zip_entry_name($zip_entry) . "<br />";
if (zip_entry_open($zip, $zip_entry))
{
echo "File Contents:<br/>";
$contents = zip_entry_read($zip_entry);
echo "$contents<br />";
zip_entry_close($zip_entry);
}
echo "</p>";
}
zip_close($zip);
}
?>
$zip = new \ZipArchive();
$zip->open(storage_path('app/'.$request->vrfile));
$name = '';
//looped through the zip files and got each index name of the files
//since I only wanted the first name which is the folder name I break the loop
//after updating the variable $name with the index name and that's it
for( $i = 0; $i < $zip->numFiles; $i++ ){
$filename = $zip->getNameIndex($i);
var_dump($filename);
$name = $filename;
if ($i == 1){
break;
}
}
var_dump($name);