Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Zip File Functions : ZipArchive::addEmptyDir

ZipArchive::addEmptyDir

Add a new directory ()
bool ZipArchive::addEmptyDir ( string dirname )

Example 2692. 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';
}
?>

Code Examples / Notes » ziparchive_addemptydir

jerome

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');
?>


nheimann

Extension for ZipArchive. Now you can Add dirs with the Zip Class!
<?php
/**
* FlxZipArchive, Extends ZipArchiv.
* Add Dirs with Files and Subdirs.
*
* <code>
*  $archive = new FlxZipArchive;
*  // .....
*  $archive->addDir( 'test/blub', 'blub' );
*/
class FlxZipArchive extends ZipArchive {
/**
* Add a Dir with Files and Subdirs to the archive
*
* @param string $location Real Location
* @param string $name Name in Archive
* @author Nicolas Heimann
* @access private
**/
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
// } // EO addDir;
/**
* Add Files & Dirs to archive.
*
* @param string $location Real Location
* @param string $name Name in Archive
* @author Nicolas Heimann
* @access private
**/
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() == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
} // EO addDirDo();
}
?>


Change Language


Follow Navioo On Twitter
zip_close
zip_entry_close
zip_entry_compressedsize
zip_entry_compressionmethod
zip_entry_filesize
zip_entry_name
zip_entry_open
zip_entry_read
zip_open
zip_read
ZipArchive::addEmptyDir
ZipArchive::addFile
ZipArchive::addFromString
ZipArchive::close
ZipArchive::deleteIndex
ZipArchive::deleteName
ZipArchive::extractTo
ZipArchive::getArchiveComment
ZipArchive::getCommentIndex
ZipArchive::getCommentName
ZipArchive::getFromIndex
ZipArchive::getFromName
ZipArchive::getNameIndex
ZipArchive::getStream
ZipArchive::locateName
ZipArchive::open
ZipArchive::renameIndex
ZipArchive::renameName
ZipArchive::setArchiveComment
ZipArchive::setCommentIndex
ZipArchive::setCommentName
ZipArchive::statIndex
ZipArchive::statName
ZipArchive::unchangeAll
ZipArchive::unchangeArchive
ZipArchive::unchangeIndex
ZipArchive::unchangeName
eXTReMe Tracker