我用荏苒沒有荏苒包括當前目錄
//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
如何要做到這一點?
,但我需要的子目錄被收錄,如在包括Sub_Dir的例子,確實你的代碼支持這個? – william007 2013-03-20 11:10:22
@ william007是的,它遞歸地將目錄內容添加到存檔。如果你調用addDir(),你會得到你的第一個示例佈局,如果你調用addDirContents(),你將得到第二個(期望的)佈局 – DaveRandom 2013-03-20 11:11:58