In PHP is it possible to inspect content of a Zip file without extracting its content first?

后端 未结 4 1656
情歌与酒
情歌与酒 2020-11-30 04:02

I have seen the ZipArchive class in PHP which lets you read zip files. But I\'m wondering if there is a way to iterate though its content without extracting the file first

相关标签:
4条回答
  • 2020-11-30 04:29

    As found as a comment on http://www.php.net/ziparchive:

    The following code can be used to get a list of all the file names in a zip file.

    <?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 ); 
    }
    ?>
    
    0 讨论(0)
  • 2020-11-30 04:30

    Repeated Question. Search before posting. PHP library that can list contents of zip / rar files

        <?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);
    
     ?>
    
    0 讨论(0)
  • 2020-11-30 04:37

    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);
    }
    ?>
    
    0 讨论(0)
  • 2020-11-30 04:46

    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);
    
    0 讨论(0)
提交回复
热议问题