Completed
Push — master ( a72e6a...adf7e7 )
by Lukas
08:39
created

Archive::open()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Felix Moeller <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2016, ownCloud, Inc.
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\Archive;
32
33
abstract class Archive{
34
	/**
35
	 * Open any of the supported archive types
36
	 *
37
	 * @param string $path
38
	 * @return Archive|void
39
	 */
40
	public static function open($path) {
41
		$mime = \OC::$server->getMimeTypeDetector()->detect($path);
42
43
		switch($mime) {
44
			case 'application/zip':
45
				return new ZIP($path);
46
			case 'application/x-gzip':
47
				return new TAR($path);
48
			case 'application/x-bzip2':
49
				return new TAR($path);
50
		}
51
	}
52
53
	/**
54
	 * @param $source
55
	 */
56
	abstract function __construct($source);
57
	/**
58
	 * add an empty folder to the archive
59
	 * @param string $path
60
	 * @return bool
61
	 */
62
	abstract function addFolder($path);
63
	/**
64
	 * add a file to the archive
65
	 * @param string $path
66
	 * @param string $source either a local file or string data
67
	 * @return bool
68
	 */
69
	abstract function addFile($path, $source='');
70
	/**
71
	 * rename a file or folder in the archive
72
	 * @param string $source
73
	 * @param string $dest
74
	 * @return bool
75
	 */
76
	abstract function rename($source, $dest);
77
	/**
78
	 * get the uncompressed size of a file in the archive
79
	 * @param string $path
80
	 * @return int
81
	 */
82
	abstract function filesize($path);
83
	/**
84
	 * get the last modified time of a file in the archive
85
	 * @param string $path
86
	 * @return int
87
	 */
88
	abstract function mtime($path);
89
	/**
90
	 * get the files in a folder
91
	 * @param string $path
92
	 * @return array
93
	 */
94
	abstract function getFolder($path);
95
	/**
96
	 * get all files in the archive
97
	 * @return array
98
	 */
99
	abstract function getFiles();
100
	/**
101
	 * get the content of a file
102
	 * @param string $path
103
	 * @return string
104
	 */
105
	abstract function getFile($path);
106
	/**
107
	 * extract a single file from the archive
108
	 * @param string $path
109
	 * @param string $dest
110
	 * @return bool
111
	 */
112
	abstract function extractFile($path, $dest);
113
	/**
114
	 * extract the archive
115
	 * @param string $dest
116
	 * @return bool
117
	 */
118
	abstract function extract($dest);
119
	/**
120
	 * check if a file or folder exists in the archive
121
	 * @param string $path
122
	 * @return bool
123
	 */
124
	abstract function fileExists($path);
125
	/**
126
	 * remove a file or folder from the archive
127
	 * @param string $path
128
	 * @return bool
129
	 */
130
	abstract function remove($path);
131
	/**
132
	 * get a file handler
133
	 * @param string $path
134
	 * @param string $mode
135
	 * @return resource
136
	 */
137
	abstract function getStream($path, $mode);
138
	/**
139
	 * add a folder and all its content
140
	 * @param string $path
141
	 * @param string $source
142
	 * @return boolean|null
143
	 */
144
	function addRecursive($path, $source) {
145
		$dh = opendir($source);
146
		if(is_resource($dh)) {
147
			$this->addFolder($path);
148
			while (($file = readdir($dh)) !== false) {
149
				if($file=='.' or $file=='..') {
150
					continue;
151
				}
152
				if(is_dir($source.'/'.$file)) {
153
					$this->addRecursive($path.'/'.$file, $source.'/'.$file);
154
				}else{
155
					$this->addFile($path.'/'.$file, $source.'/'.$file);
156
				}
157
			}
158
		}
159
	}
160
}
161