2013-07-31 2 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!'; 
이 아카이브 내용 인 경우 예를 들어

,