2012-06-10 55 views
3

我正在使用php脚本来解压ZIP文件。但这个脚本解压缩目录的只有一个级别,而不提取该文件 的子目录脚本:在PHP中提取ZIP文件的子文件夹

$zip = new ZipArchive; 
if ($zip->open('test.zip') === TRUE) { 
    $zip->extractTo('/my/destination/dir/'); 
    $zip->close(); 
    echo 'ok'; 
} else { 
    echo 'failed'; 
} 

例如:如果test.zip包含2个文件夹:文件夹1 \ file.png,文件夹2 \ folder3 \ file3.png

解压缩此ZIP文件后,我只能看到folder1 *。*和folder2 *。*,但没有子目录folder3。

我该如何改进?

+0

这是否有帮助:http://stackoverflow.com/a/2707050/377628? – Hassan

回答

7

我觉得这个PHP手册将有助于您 http://php.net/manual/en/ref.zip.php

<?php 
$file = "2537c61ef7f47fc3ae919da08bcc1911.zip"; 
$dir = getcwd(); 
function Unzip($dir, $file, $destiny="") 
{ 
    $dir .= DIRECTORY_SEPARATOR; 
    $path_file = $dir . $file; 
    $zip = zip_open($path_file); 
    $_tmp = array(); 
    $count=0; 
    if ($zip) 
    { 
     while ($zip_entry = zip_read($zip)) 
     { 
      $_tmp[$count]["filename"] = zip_entry_name($zip_entry); 
      $_tmp[$count]["stored_filename"] = zip_entry_name($zip_entry); 
      $_tmp[$count]["size"] = zip_entry_filesize($zip_entry); 
      $_tmp[$count]["compressed_size"] = zip_entry_compressedsize($zip_entry); 
      $_tmp[$count]["mtime"] = ""; 
      $_tmp[$count]["comment"] = ""; 
      $_tmp[$count]["folder"] = dirname(zip_entry_name($zip_entry)); 
      $_tmp[$count]["index"] = $count; 
      $_tmp[$count]["status"] = "ok"; 
      $_tmp[$count]["method"] = zip_entry_compressionmethod($zip_entry); 

      if (zip_entry_open($zip, $zip_entry, "r")) 
      { 
       $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 
       if($destiny) 
       { 
        $path_file = str_replace("/",DIRECTORY_SEPARATOR, $destiny . zip_entry_name($zip_entry)); 
       } 
       else 
       { 
        $path_file = str_replace("/",DIRECTORY_SEPARATOR, $dir . zip_entry_name($zip_entry)); 
       } 
       $new_dir = dirname($path_file); 

       // Create Recursive Directory (if not exist) 
       if (!file_exists($new_dir)) { 
        mkdir($new_dir, 0700); 
       } 

       $fp = fopen($dir . zip_entry_name($zip_entry), "w"); 
       fwrite($fp, $buf); 
       fclose($fp); 

       zip_entry_close($zip_entry); 
      } 
      echo "\n</pre>"; 
      $count++; 
     } 

     zip_close($zip); 
    } 
} 
Unzip($dir,$file); 
?> 
-1

试试这个简单的方法:

system('unzip my_zip_file.zip'); 
0

这个脚本提取所有文件的zip文件递归。这会将它们提取到当前目录中。

<?php 
    set_time_limit(0); 

    echo "HI<br><br>"; 
    //---------------- 
    //UNZIP a zip file 
    //---------------- 

    $zipfilename = "site.zip"; 

    //---------------- 

    function unzip($file){ 

     $zip=zip_open(realpath(".")."/".$file); 
     if(!$zip) {return("Unable to proccess file '{$file}'");} 

     $e=''; 

     while($zip_entry=zip_read($zip)) { 
      $zdir=dirname(zip_entry_name($zip_entry)); 
      $zname=zip_entry_name($zip_entry); 

      if(!zip_entry_open($zip,$zip_entry,"r")) {$e.="Unable to proccess file '{$zname}'";continue;} 
      if(!is_dir($zdir)) mkdirr($zdir,0777); 

      #print "{$zdir} | {$zname} \n"; 

      $zip_fs=zip_entry_filesize($zip_entry); 
      if(empty($zip_fs)) continue; 

      $zz=zip_entry_read($zip_entry,$zip_fs); 

      $z=fopen($zname,"w"); 
      fwrite($z,$zz); 
      fclose($z); 
      zip_entry_close($zip_entry); 

     } 
     zip_close($zip); 

     return($e); 
    } 

    function mkdirr($pn,$mode=null) { 

     if(is_dir($pn)||empty($pn)) return true; 
     $pn=str_replace(array('/', ''),DIRECTORY_SEPARATOR,$pn); 

     if(is_file($pn)) {trigger_error('mkdirr() File exists', E_USER_WARNING);return false;} 

     $next_pathname=substr($pn,0,strrpos($pn,DIRECTORY_SEPARATOR)); 
     if(mkdirr($next_pathname,$mode)) {if(!file_exists($pn)) {return mkdir($pn,$mode);} } 
     return false; 
    } 

    unzip($zipfilename); 

    ?>