note 91221 added to function.ziparchive-addemptydir
| From: | thomas dot rubbert at yahoo dot de | Date: | Sun, 31 May 2009 00:19:04 +0000 |
| Subject: | note 91221 added to function.ziparchive-addemptydir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156192@lists.php.net to get a copy of this message | ||
Here's a stack-based way to zip a directory. No recursion, no function, no class:
<?php
$zip = new ZipArchive();
$zip->open('test.zip', ZipArchive::CREATE);
$dirName = 'test';
if (!is_dir($dirName)) {
throw new Exception('Directory ' . $dirName . ' does not exist');
}
$dirName = realpath($dirName);
if (substr($dirName, -1) != '/') {
$dirName.= '/';
}
$dirStack = array($dirName);
//Find the index where the last dir starts
$cutFrom = strrpos(substr($dirName, 0, -1), '/')+1;
while (!empty($dirStack)) {
$currentDir = array_pop($dirStack);
$filesToAdd = array();
$dir = dir($currentDir);
while (false !== ($node = $dir->read())) {
if (($node == '..') || ($node == '.')) {
continue;
}
if (is_dir($currentDir . $node)) {
array_push($dirStack, $currentDir . $node . '/');
}
if (is_file($currentDir . $node)) {
$filesToAdd[] = $node;
}
}
$localDir = substr($currentDir, $cutFrom);
$zip->addEmptyDir($localDir);
foreach ($filesToAdd as $file) {
$zip->addFile($currentDir . $file, $localDir . $file);
}
}
$zip->close();
?>
----
Server IP: 212.124.37.9
Probable Submitter: 212.101.201.49
----
Manual Page -- http://www.php.net/manual/en/function.ziparchive-addemptydir.php
Edit -- https://master.php.net/note/edit/91221
Del: integrated -- https://master.php.net/note/delete/91221/integrated
Del: useless -- https://master.php.net/note/delete/91221/useless
Del: bad code -- https://master.php.net/note/delete/91221/bad+code
Del: spam -- https://master.php.net/note/delete/91221/spam
Del: non-english -- https://master.php.net/note/delete/91221/non-english
Del: in docs -- https://master.php.net/note/delete/91221/in+docs
Del: other reasons-- https://master.php.net/note/delete/91221
Reject -- https://master.php.net/note/reject/91221
Search -- https://master.php.net/manage/user-notes.php