2011-03-10 53 views
0
$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文件到一個文件夾)

我怎樣才能解決這個...

回答

2

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

0

我會做的第一件事是這樣的:

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

,看看你會得到什麼

1

見手冊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

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

正確

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

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

0

我用很簡單的方法來做到這一點

system('unzip assets_04_02_2015.zip');