解压使用PHP文件(折叠ZIP文件到一个文件夹)

问题描述:

$file_name = 'New Folder.zip' 
    $zip = new ZipArchive; 
    $result = $zip->open($target_path.$file_name); 
    if ($result === TRUE) { 
     for($i = 0; $i < $zip->numFiles; $i++) { 
     $filename = $zip->getNameIndex($i); 
     $fileinfo = pathinfo($filename); 
     copy("zip://".$file_name."#".$filename, $target_path.$fileinfo['basename']); 
     } 
    } 

当我运行这段代码我得到这个错误Warning: copy(zip://New Folder.zip#New Folder/icon_android.png) [function.copy]: failed to open stream: operation failed in...解压使用PHP文件(折叠ZIP文件到一个文件夹)

我怎样才能解决这个...

PHP's doc

$zip = new ZipArchive; 
if ($zip->open('test.zip') === TRUE) { 
    $zip->extractTo($your_desired_dir); 
    $zip->close(); 
    foreach (glob($your_desired_dir . DIRECTORY_SEPARATOR . 'New Folder') as $file) { 
     $finfo = pathinfo($file); 
     rename($file, $your_desired_dir . DIRECTORY_SEPARATOR . $finfo['basename']); 
    } 
    unlink($your_desired_dir . DIRECTORY_SEPARATOR . 'New Folder'); 
    echo 'ok'; 
} else { 
    echo 'failed'; 
} 

不知道为什么你使用流。

+0

这就是我想要的:当我解压“新Folder.zip',我想将.zip文件的内容提取到'/ targetfolder/[contents]'。如果我使用'$ zip-> extractTo('/ my/destination/dir /'),那么我得到'/ targetfolder/New Folder/[contents]'。这是我的问题 ' – Maggie 2011-03-10 07:22:57

+0

也许你的原始邮编包含该目录? PHP不会仅仅因为'创建目录。 – fabrik 2011-03-10 07:24:12

+0

它包含该目录(最糟糕的情况),我无法控制'如何创建.zip文件'。如果上传的.zip文件包含该目录,那么我正面临着这个问题,其他的extractTo()可以很好地工作。 – Maggie 2011-03-10 07:30:04

我会做的第一件事是这样的:

echo "FROM - zip://".$file_name."#".$filename; 
echo "<BR>TO - " . $target_path.$fileinfo['basename']; 

,看看你会得到什么

见手册http://php.net/manual/en/book.zip.php

unzip.php (sample code) 

// the first argument is the zip file 
$in_file = $_SERVER['argv'][1]; 

// any other arguments are specific files in the archive to unzip 
if ($_SERVER['argc'] > 2) { 
    $all_files = 0; 
    for ($i = 2; $i < $_SERVER['argc']; $i++) { 
     $out_files[$_SERVER['argv'][$i]] = true; 
    } 
} else { 
    // if no other files are specified, unzip all files 
    $all_files = true; 
} 

$z = zip_open($in_file) or die("can't open $in_file: $php_errormsg"); 
while ($entry = zip_read($z)) { 

    $entry_name = zip_entry_name($entry); 

    // check if all files should be unzipped, or the name of 
    // this file is on the list of specific files to unzip 
    if ($all_files || $out_files[$entry_name]) { 

     // only proceed if the file is not 0 bytes long 
     if (zip_entry_filesize($entry)) { 
      $dir = dirname($entry_name); 

      // make all necessary directories in the file's path 
      if (! is_dir($dir)) { pc_mkdir_parents($dir); } 

      $file = basename($entry_name); 

      if (zip_entry_open($z,$entry)) { 
       if ($fh = fopen($dir.'/'.$file,'w')) { 
        // write the entire file 
        fwrite($fh, 
          zip_entry_read($entry,zip_entry_filesize($entry))) 
         or error_log("can't write: $php_errormsg"); 
        fclose($fh) or error_log("can't close: $php_errormsg"); 
       } else { 
        error_log("can't open $dir/$file: $php_errormsg"); 
       } 
       zip_entry_close($entry); 
      } else { 
       error_log("can't open entry $entry_name: $php_errormsg"); 
      } 
     } 
    } 
} 

http://www.java-samples.com/showtutorial.php?tutorialid=985

+0

你可以添加更多的细节呢?这里有什么'$ _SERVER ['argv']'? – fabrik 2011-03-10 07:59:24

+0

$ z = zip_open($ in_file)或死(“无法打开$ in_file:$ php_errormsg”); 打开文件,然后写入 – Efazati 2011-03-10 08:07:45

+0

所以你完全没有关于这里发生了什么的概念。 – fabrik 2011-03-10 08:16:33

copy("zip://".$file_name."#".$filename, $target_path.$fileinfo['basename']);} 

正确

copy("zip://".dirname(__FILE__).'/'.$file_name."#".$filename, $target_path.$fileinfo['basename']);} 

完整路径需要使用ZIP://流

我用很简单的方法来做到这一点

system('unzip assets_04_02_2015.zip');