首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在PHP中,可以在不先提取内容的情况下检查Zip文件的内容吗?

在PHP中,可以在不先提取内容的情况下检查Zip文件的内容吗?
EN

Stack Overflow用户
提问于 2012-03-22 14:34:25
回答 3查看 38.8K关注 0票数 35

我见过PHP中的ZipArchive类,它可以让你读取压缩文件。但我想知道是否有一种方法可以在不先提取文件的情况下遍历其内容

EN

回答 3

Stack Overflow用户

发布于 2013-11-26 08:38:19

http://www.php.net/manual/en/function.zip-entry-read.php

代码语言:javascript
复制
<?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);
}
?>
票数 17
EN

Stack Overflow用户

发布于 2018-08-16 15:02:20

我这样解决了这个问题。

代码语言:javascript
复制
$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);
票数 0
EN

Stack Overflow用户

发布于 2012-03-22 14:38:37

重复问题。在发帖前进行搜索。PHP library that can list contents of zip / rar files

代码语言:javascript
复制
    <?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);

 ?>
票数 -6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9817525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档