2013-07-31 65 views
0

我需要將zip壓縮文件中的目錄內容提取到輸出目錄中。PHP Zip:提取目錄中的內容

zip內的目錄名稱可以是任何東西。但它將是zip壓縮文件的唯一目錄。儘管如此,目錄中可能有許多文件在zip壓縮文件中。

拉鍊會沿着內部的文件結構提綱行:

- d0001 
    - My Folder 
    - view.php 
    - tasks.txt 
    - file1.txt 
    - picture1.png 
    - document.doc 

輸出目錄的內容必須是這樣的:

- My Folder 
    - view.php 
    - tasks.txt 
- file1.txt 
- picture1.png 
- document.doc 

我目前擁有的代碼刪除內容的輸出目錄並將整個zip壓縮文件解壓縮到目錄中:

function Unzip($source, $destination) { 
    $zip = new ZipArchive; 
    $res = $zip->open($source); 
    if($res === TRUE) { 
     $zip->extractTo($destination); 
     $zip->close(); 
     return true; 
    } else { 
     return false; 
    } 
} 
function rrmdir($dir, $removebase = true) { 
    if(is_dir($dir)) { 
     $objects = scandir($dir); 
     foreach($objects as $object) { 
      if($object != "." && $object != "..") { 
       if(filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
      } 
     } 
     reset($objects); 
     if($removebase == true) 
      rmdir($dir); 
    } 
} 

$filename = '/home/files.zip'; 
$dest = '/home/myfiles/'; 

if(is_dir($dest)) { 
    rrmdir($dest, false); 

    $unzip = Unzip($filename, $dest); 
    if($unzip === true) { 
    echo 'Success'; 
    } else 
    echo 'Extraction of zip failed.'; 
} else 
    echo 'The output directory does not exist!'; 

所有功能rrmdir()確實是刪除輸出目錄的內容。

回答

0

我設法通過手動處理文件流而不是使用extractTo()來實現它。

該腳本將檔案庫底部的所有文件和檔案庫底部的目錄中的所有文件抽取到輸出文件夾中。

例如,如果是這樣的檔案內容:

- d0001 
    - My Folder 
    - view.php 
    - tasks.txt 
    - file1.txt 
    - picture1.png 
    - document.doc 
- d2 
    - another1.png 
    - pic2.gif 
- doc1.txt 
- mylist.txt 

輸出目錄的內容將是這樣的:

- My Folder 
    - view.php 
    - tasks.txt 
- file1.txt 
- picture1.png 
- document.doc 
- another1.png 
- pic.gif 
- doc1.txt 
- mylist.txt 

代碼:

function rrmdir($dir, $removebase = true) { 
    if(is_dir($dir)) { 
     $objects = scandir($dir); 
     foreach($objects as $object) { 
      if($object != "." && $object != "..") { 
       if(filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
      } 
     } 
     reset($objects); 
     if($removebase == true) 
      rmdir($dir); 
    } 
} 

$filename = '/home/files.zip'; 
$dest = '/home/myfiles/'; 

if(is_dir($dest)) { 
    // Remove directory's contents 
    rrmdir($dest, false); 

    // Load up the zip 
    $zip = new ZipArchive; 
    $unzip = $zip->open($filename); 
    if($unzip === true) { 
    for($i=0; $i<$zip->numFiles; $i++) { 
     $name = $zip->getNameIndex($i); 

     // Remove the first directory in the string if necessary 
     $parts = explode('/', $name); 
     if(count($parts) > 1) { 
     array_shift($parts); 
     } 
     $file = $dest . '/' . implode('/', $parts); 

     // Create the directories if necessary 
     $dir = dirname($file); 
     if(!is_dir($dir)) 
     mkdir($dir, 0777, true); 

     // Check if $name is a file or directory 
     if(substr($file, -1) == "/") { 
     // $name is a directory 
     // Create the directory 
     if(!is_dir($file)) 
      mkdir($file, 0777, true); 
     } else { 
     // $name is a file 
     // Read from Zip and write to disk 
     $fpr = $zip->getStream($name); 
     $fpw = fopen($file, 'w'); 
     while($data = fread($fpr, 1024)) { 
      fwrite($fpw, $data); 
     } 
     fclose($fpr); 
     fclose($fpw); 
     } 
    } 
    echo 'Success'; 
    } else 
    echo 'Extraction of zip failed.'; 
} else 
    echo 'The output directory does not exist!';