List Info

Thread: note 78510 added to function.ziparchive-addemptydir




note 78510 added to function.ziparchive-addemptydir
user name
2007-10-15 15:20:35
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');
?>
----
Server IP: 209.41.74.194
Probable Submitter: 64.222.201.233
----
Manual Page -- http://www.php.net/manual/en/function.ziparchive
-addemptydir.php
Edit        -- https://master
.php.net/note/edit/78510
Del: integrated  -- h
ttps://master.php.net/note/delete/78510/integrated
Del: useless     -- http
s://master.php.net/note/delete/78510/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78510/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78510/spam
Del: non-english -- 
https://master.php.net/note/delete/78510/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78510/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78510
Reject      -- https://mast
er.php.net/note/reject/78510
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )