-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmzipper.php
79 lines (73 loc) · 1.74 KB
/
fmzipper.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php namespace ProcessFileManager;
if(!defined("PROCESSWIRE")) die();
class FM_Zipper {
private $zip;
public function __construct() {
$this->zip = new \ZipArchive();
}
public function create($filename, $files) {
$res = $this->zip->open($filename, \ZipArchive::CREATE);
if ($res !== true) {
return false;
}
if (is_array($files)) {
foreach ($files as $f) {
if (!$this->addFileOrDir($f)) {
$this->zip->close();
return false;
}
}
$this->zip->close();
return true;
} else {
if ($this->addFileOrDir($files)) {
$this->zip->close();
return true;
}
return false;
}
}
public function unzip($filename, $path) {
$res = $this->zip->open($filename);
if ($res !== true) {
return false;
}
if ($this->zip->extractTo($path)) {
$this->zip->close();
return true;
}
return false;
}
private function addFileOrDir($filename) {
if (is_file($filename)) {
return $this->zip->addFile($filename);
} elseif (is_dir($filename)) {
return $this->addDir($filename);
}
return false;
}
private function addDir($path) {
if (!$this->zip->addEmptyDir($path)) {
return false;
}
$objects = scandir($path);
if (is_array($objects)) {
foreach ($objects as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($path.'/'.$file)) {
if (!$this->addDir($path.'/'.$file)) {
return false;
}
} elseif (is_file($path.'/'.$file)) {
if (!$this->zip->addFile($path.'/'.$file)) {
return false;
}
}
}
}
return true;
}
return false;
}
}
?>