PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ZipArchive::addFile> <zip_read
Last updated: Fri, 10 Oct 2008

view this page in

ZipArchive::addEmptyDir

(No version information available, might be only in CVS)

ZipArchive::addEmptyDir新しいディレクトリを追加する

説明

bool ZipArchive::addEmptyDir ( string $dirname )

空のディレクトリをアーカイブに追加します。

パラメータ

dirname

追加するディレクトリ。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 アーカイブ内での新規ディレクトリの作成

<?php
$zip 
= new ZipArchive;
if (
$zip->open('test.zip') === TRUE) {
    if(
$zip->addEmptyDir('newDirectory')) {
        echo 
'新しいディレクトリを作成しました';
    } else {
        echo 
'ディレクトリが作成できませんでした';
    }
    
$zip->close();
} else {
    echo 
'失敗しました';
}
?>


ZipArchive::addFile> <zip_read
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
ZipArchive::addEmptyDir
MangaII
12-Oct-2008 07:12
In the function of D.Jann, addEmptyDir is not needed !

It can cause error ...

Juste comment :
$zipArchive->addEmptyDir($dir);
D.Jann
07-Oct-2008 05:26
I've brought a little modification to dayjo's code, so it wouldn't re-create the whole structure of your working drive in the zip file:

<?php
// Function to recursively add a directory,
// sub-directories and files to a zip archive
function addFolderToZip($dir, $zipArchive, $zipdir = ''){
    if (
is_dir($dir)) {
        if (
$dh = opendir($dir)) {

           
//Add the directory
           
$zipArchive->addEmptyDir($dir);
          
           
// Loop through all the files
           
while (($file = readdir($dh)) !== false) {
          
               
//If it's a folder, run the function again!
               
if(!is_file($dir . $file)){
                   
// Skip parent and root directories
                   
if( ($file !== ".") && ($file !== "..")){
                       
addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file . "/");
                    }
                  
                }else{
                   
// Add the files
                   
$zipArchive->addFile($dir . $file, $zipdir . $file);
                  
                }
            }
        }
    }
}
?>

If you don't specify the third parameter (zipdir), the directory you're adding will be added at the root of the zip file.

D.Jann
pagetronic
12-Sep-2008 03:53
addEmptyDir doesn't exist now,
use $zip->addFile($file, $dir.$fichier);

nothing on the net about this,

addEmptyDir work on Ubuntu but not on Debian Etch
Anonymous
24-Aug-2008 03:28
Here's a simple recurring function to add a directory, all sub-directories and all files to an already created zip file;

<?php
// Function to recursively add a directory,
// sub-directories and files to a zip archive
function addFolderToZip($dir, $zipArchive){
    if (
is_dir($dir)) {
        if (
$dh = opendir($dir)) {

           
//Add the directory
           
$zipArchive->addEmptyDir($dir);
           
           
// Loop through all the files
           
while (($file = readdir($dh)) !== false) {
           
               
//If it's a folder, run the function again!
               
if(!is_file($dir . $file)){
                   
// Skip parent and root directories
                   
if( ($file !== ".") && ($file !== "..")){
                       
addFolderToZip($dir . $file . "/", $zipArchive);
                    }
                   
                }else{
                   
// Add the files
                   
$zipArchive->addFile($dir . $file);
                   
                }
            }
        }
    }
}
?>

Would be nice to see more input on these functions :)

Dayjo
benjamin dot seiller at antwerpes dot de
21-Jul-2008 04:37
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
jerome at buttered-cat dot com
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');
?>

ZipArchive::addFile> <zip_read
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites