There is kind of a bug in the method
ZipArchive::addFile
which affects the class ZipFolder below.
It is related to the numer of max filehandles of the OS.
As workaround add a file-counter to the class and close + reopen the archive if a certain number of files (directories count as files!) is reached.
For more details see here:
http://de.php.net/manual/en/function.ziparchive-addfile.php
or go directly here
http://bugs.php.net/bug.php?id=40494
or here
http://pecl.php.net/bugs/bug.php?id=9443
ZipArchive::addEmptyDir
(No version information available, might be only in CVS)
ZipArchive::addEmptyDir — Add a new directory
Описание
bool ZipArchive::addEmptyDir
( string $dirname
)
Adds an empty directory in the archive.
Список параметров
- dirname
-
The directory to add.
Возвращаемые значения
Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.
Примеры
Пример #1 Create a new directory in an archive
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
if($zip->addEmptyDir('newDirectory')) {
echo 'Created a new root directory';
} else {
echo 'Could not create the directory';
}
$zip->close();
} else {
echo 'failed';
}
?>
ZipArchive::addEmptyDir
benjamin dot seiller at antwerpes dot de
21-Jul-2008 04:37
21-Jul-2008 04:37
jerome at buttered-cat dot com
15-Oct-2007 01:20
15-Oct-2007 01:20
Here's some code I wrote to add a NON-empty directory to ZipArchive, it's done pretty quickly but so far works great.
<?php
class ZipFolder {
protected $zip;
protected $root;
protected $ignored_names;
function __construct($file, $folder, $ignored=null) {
$this->zip = new ZipArchive();
$this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array($ignored) : array();
if ($this->zip->open($file, ZIPARCHIVE::CREATE)!==TRUE) {
throw new Exception("cannot open <$file>\n");
}
$folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder)-1) : $folder;
if(strstr($folder, '/')) {
$this->root = substr($folder, 0, strrpos($folder, '/')+1);
$folder = substr($folder, strrpos($folder, '/')+1);
}
$this->zip($folder);
$this->zip->close();
}
function zip($folder, $parent=null) {
$full_path = $this->root.$parent.$folder;
$zip_path = $parent.$folder;
$this->zip->addEmptyDir($zip_path);
$dir = new DirectoryIterator($full_path);
foreach($dir as $file) {
if(!$file->isDot()) {
$filename = $file->getFilename();
if(!in_array($filename, $this->ignored_names)) {
if($file->isDir()) {
$this->zip($filename, $zip_path.'/');
}
else {
$this->zip->addFile($full_path.'/'.$filename, $zip_path.'/'.$filename);
}
}
}
}
}
}
// full path used to demonstrate it's root-path stripping ability
$zip = new ZipFolder('/tmp/test.zip', dirname(__FILE__).'/templates/', '.svn');
?>
