forked from ttodua/useful-php-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip-folder.php
40 lines (35 loc) · 1.48 KB
/
zip-folder.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
// =========== https://github.com/tazotodua/useful-php-scripts ==========
// USAGE:
// new GoodZipArchive('path/to/input/folder', 'path/to/output_zip_file.zip') ;
// ======================================================================
class GoodZipArchive extends ZipArchive
{
//@author Nicolas Heimann
public function __construct($a=false, $b=false) { $this->create_func($a, $b); }
public function create_func($input_folder=false, $output_zip_file=false)
{
if($input_folder && $output_zip_file)
{
$res = $this->open($output_zip_file, ZipArchive::CREATE);
if($res === TRUE) { $this->addDir($input_folder, basename($input_folder)); $this->close(); }
else { echo 'Could not create a zip archive. Contact Admin.'; }
}
}
// Add a Dir with Files and Subdirs to the archive
public function addDir($location, $name) {
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
}
// Add Files & Dirs to archive
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: GoodZipArchive::addDir(), else ::File();
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
}
}