2013-03-20 73 views
1

我用荏苒沒有荏苒包括當前目錄

//http://ninad.pundaliks.in/blog/2011/05/recursively-zip-a-directory-with-php/ 

class FlxZipArchive extends ZipArchive { 

    public function addDir($location, $name) { 
     $this->addEmptyDir($name);  
     $this->addDirDo($location, $name); 
    } // EO addDir 

    private function addDirDo($location, $name) { 
     $name .= '/'; 
     $location .= '/'; 

     // Read all Files in Dir 
     $dir = opendir ($location); 
     while ($file = readdir($dir)) 
     { 
      if ($file == '.' || $file == '..') continue; 

      // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File(); 
      $do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile'; 
      $this->$do($location . $file, $name . $file); 
     } 
    } // EO addDirDo(); 
} 


function zipIt($source, $target){ 
    $za = new FlxZipArchive; 

    $res = $za->open($target, ZipArchive::CREATE); 

    if($res === TRUE) { 
     $za->addDir($source, basename($source)); 
     $za->close(); 
    } 
    else 
     echo 'Could not create a zip archive'; 

} 

$the_folder = './Sales report'; 
$zip_file_name = './Sales report.docx'; 
//Don't forget to remove the trailing slash in folder 
zipIt($the_folder,$zip_file_name,false); 

的問題是,它包含了當前目錄下,

Sales report/ 
    file1.html 
    file2.html 
    Sub_Dir/ 
     file19.html 

但我只想

file1.html 
    file2.html 
    Sub_Dir/ 
     file19.html 

如何要做到這一點?

回答

1

下面是我用什麼:

/** 
* Add a directory and its contents to the archive 
* 
* @param string $dir  The local filesystem path to the directory 
* @param string $localName The archive filesystem path to the directory 
* 
* @throws \RuntimeException When adding an object to the archive fails 
*/ 
public function addDir($dirPath, $localName = NULL) 
{ 
    if ($localName === NULL) { 
     $localName = basename($dirPath); 
    } 
    if (!$this->addEmptyDir($localName)) { 
     throw new \RuntimeException('Error adding directory '.$dirPath.' to archive'); 
    } 

    $this->addDirContents($dirPath, $localName); 
} 

/** 
* Add the contents of a directory to the archive 
* 
* @param string $dir  The local filesystem path to the directory 
* @param string $localName The archive filesystem path to the directory 
* 
* @throws \RuntimeException When adding an object to the archive fails 
*/ 
public function addDirContents($dirPath, $localName = '') 
{ 
    $base = ltrim($localName.'/', '/'); 

    foreach (glob("$dirPath/*") as $file) { 
     if (is_dir($file)) { 
      $this->addDir($file, $base.basename($file)); 
     } else { 
      if (!$this->addFile($file, $base.basename($file))) { 
       throw new \RuntimeException('Error adding file '.$file.' to archive'); 
      } 
     } 
    } 
} 

就像你的代碼,這些方法在擴展\ZipArchive類屬。 addDir()添加一個目錄和它的內容,addDirContents()只是添加內容而不在存檔中創建父目錄(因此做你想做的)。

我通常不喜歡只給出沒有解釋的免費工作代碼,但它恰好如此發生,我確切地知道你需要在我的編輯器中打開什麼。

+0

,但我需要的子目錄被收錄,如在包括Sub_Dir的例子,確實你的代碼支持這個? – william007 2013-03-20 11:10:22

+0

@ william007是的,它遞歸地將目錄內容添加到存檔。如果你調用addDir(),你會得到你的第一個示例佈局,如果你調用addDirContents(),你將得到第二個(期望的)佈局 – DaveRandom 2013-03-20 11:11:58

0

我用這個代碼最後,爲選擇提供了一個選擇或不包括當前目錄:

<?php 
//http://ninad.pundaliks.in/blog/2011/05/recursively-zip-a-directory-with-php/ 



class FlxZipArchive extends ZipArchive { 

    public function addDir1($location, $name, $includeCurrDir) { 
     if($includeCurrDir) 
     $this->addEmptyDir($name);  
     $this->addDirDo1($location, $name, $includeCurrDir); 
    } // EO addDir 

    private function addDirDo1($location, $name, $includeCurrDir) { 
     if($includeCurrDir) 
     $name .= '/'; 
     else 
     $name = ''; 

     $location .= '/'; 

     // Read all Files in Dir 
     $dir = opendir ($location); 
     while ($file = readdir($dir)) 
     { 
      if ($file == '.' || $file == '..') continue; 

      // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File(); 
      $do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile'; 
      $this->$do($location . $file, $name . $file); 
     } 
    } // EO addDirDo(); 

    public function addDir($location, $name) { 
     $this->addEmptyDir($name);  
     $this->addDirDo($location, $name); 
    } // EO addDir 

    private function addDirDo($location, $name) { 
     $name .= '/'; 
     $location .= '/'; 

     // Read all Files in Dir 
     $dir = opendir ($location); 
     while ($file = readdir($dir)) 
     { 
      if ($file == '.' || $file == '..') continue; 

      // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File(); 
      $do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile'; 
      $this->$do($location . $file, $name . $file); 
     } 
    } // EO addDirDo(); 
} 


function zipIt($source, $target, $includeCurrDir){ 
    $za = new FlxZipArchive; 

    $res = $za->open($target, ZipArchive::CREATE); 

    if($res === TRUE) { 
     $za->addDir1($source, basename($source),$includeCurrDir); 
     $za->close(); 
    } 
    else 
     echo 'Could not create a zip archive'; 

} 
$the_folder = 'Sales report'; 
$zip_file_name = 'Sales report.docx'; 
//Don't forget to remove the trailing slash in folder 
zipIt($the_folder,$zip_file_name,false); 


?>