Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can exclude root dir #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions zip-folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,44 @@

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 !== false && $output_zip_file !== false)
{
$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.'; }
}
}

//@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 !== false && $output_zip_file !== false) {
$res = $this->open($output_zip_file, ZipArchive::CREATE);
if ($res === TRUE) {
$this->addDir($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);
public function addDir($location, $name = '')
{
if (!empty($name)) $this->addEmptyDir($name);
$this->addDirTo($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)) {
private function addDirTo($location, $name)
{
if (!empty($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';
// Rekursiv, If dir: GoodZipArchive::addDir(), else ::File();
$do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
}
}
}