note 80705 modified in ref.zip by danbrown
| From: | danbrown@php.net | Date: | Wed, 29 Jun 2011 15:31:00 +0000 |
| Subject: | note 80705 modified in ref.zip by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181060@lists.php.net to get a copy of this message | ||
Here's a more simple extended class to add a whole directory recursively, keeping the same
structure. It uses SPL.
<?php
class MyZipArchive extends ZipArchive
{
/**
*
* Adds a directory recursively.
*
* @param string $filename The path to the file to add.
*
* @param string $localname Local name inside ZIP archive.
*
*/
public function addDir($filename, $localname)
{
$this->addEmptyDir($localname);
$iter = new RecursiveDirectoryIterator($filename, FilesystemIterator::SKIP_DOTS);
foreach ($iter as $fileinfo) {
if (! $fileinfo->isFile() && !$fileinfo->isDir()) {
continue;
}
$method = $fileinfo->isFile() ? 'addFile' : 'addDir';
$this->$method($fileinfo->getPathname(), $localname . '/' .
$fileinfo->getFilename());
}
}
}
?>
[EDIT BY danbrown AT php DOT net: Contains a bugfix suggested by (bart AT blueberry DOT nl) on
29-JUN-2011 with the following message. "Fix for the endless Iteratore add the flag
FilesystemIterator::SKIP_DOTS"]
--was--
Here's a more simple extended class to add a whole directory recursively, keeping the same
structure. It uses SPL.
<?php
class MyZipArchive extends ZipArchive
{
/**
*
* Adds a directory recursively.
*
* @param string $filename The path to the file to add.
*
* @param string $localname Local name inside ZIP archive.
*
*/
public function addDir($filename, $localname)
{
$this->addEmptyDir($localname);
$iter = new RecursiveDirectoryIterator($filename);
foreach ($iter as $fileinfo) {
if (! $fileinfo->isFile() && !$fileinfo->isDir()) {
continue;
}
$method = $fileinfo->isFile() ? 'addFile' : 'addDir';
$this->$method($fileinfo->getPathname(), $localname . '/' .
$fileinfo->getFilename());
}
}
}
?>
http://php.net/manual/en/ref.zip.php