aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/ExtendedZip.php
blob: a40110c557255e73f9072eedae20016a2f61175b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Zotlabs\Lib;

/**
 * Description of ExtendedZip
 *
 * @author andrew
 */
class ExtendedZip extends \ZipArchive {
		
		// Member function to add a whole file system subtree to the archive
		public function addTree($dirname, $localname = '') {
				if ($localname)
						$this->addEmptyDir($localname);
				$this->_addTree($dirname, $localname);
		}

		// Internal function, to recurse
		protected function _addTree($dirname, $localname) {
				$dir = opendir($dirname);
				while ($filename = readdir($dir)) {
						// Discard . and ..
						if ($filename == '.' || $filename == '..')
								continue;

						// Proceed according to type
						$path = $dirname . '/' . $filename;
						$localpath = $localname ? ($localname . '/' . $filename) : $filename;
						if (is_dir($path)) {
								// Directory: add & recurse
								$this->addEmptyDir($localpath);
								$this->_addTree($path, $localpath);
						}
						else if (is_file($path)) {
								// File: just add
								$this->addFile($path, $localpath);
						}
				}
				closedir($dir);
		}

		// Helper function
		public static function zipTree($dirname, $zipFilename, $flags = 0, $localname = '') {
				$zip = new self();
				$zip->open($zipFilename, $flags);
				$zip->addTree($dirname, $localname);
				$zip->close();
		}
		
}