[英]In PHP is it possible to inspect content of a Zip file without extracting its content first?
I have seen the ZipArchive class in PHP which lets you read zip files. 我在PHP中看到了ZipArchive类,它允许您读取zip文件。 But I'm wondering if there is a way to iterate though its content without extracting the file first 但我想知道是否有办法迭代其内容而不先提取文件
As found as a comment on http://www.php.net/ziparchive : 作为对http://www.php.net/ziparchive的评论:
The following code can be used to get a list of all the file names in a zip file. 以下代码可用于获取zip文件中所有文件名的列表。
<?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 ); } ?>
http://www.php.net/manual/en/function.zip-entry-read.php http://www.php.net/manual/en/function.zip-entry-read.php
<?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);
}
?>
I solved the problem like this. 我解决了这个问题。
$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);
Repeated Question. 重复的问题。 Search before posting. 发布前搜索。 PHP library that can list contents of zip / rar files 可以列出zip / rar文件内容的PHP库
<?php
$rar_file = rar_open('example.rar') or die("Can't open Rar archive");
$entries = rar_list($rar_file);
foreach ($entries as $entry) {
echo 'Filename: ' . $entry->getName() . "\n";
echo 'Packed size: ' . $entry->getPackedSize() . "\n";
echo 'Unpacked size: ' . $entry->getUnpackedSize() . "\n";
$entry->extract('/dir/extract/to/');
}
rar_close($rar_file);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.