-
-
Save emresaracoglu/ea3b19a1327ad3444b17d72d870855ae to your computer and use it in GitHub Desktop.
Extended ZipArchive - simply way to create zip with subdirectories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FlxZipArchive extends ZipArchive | |
{ | |
/** | |
* Add a Dir with Files and Subdirs to the archive | |
* | |
* @param string $location Real Location | |
* @param string $name Name in Archive | |
* */ | |
public function addDir($location, $name) | |
{ | |
$this->addEmptyDir($name); | |
$this->addDirDo($location, $name); | |
} | |
/** | |
* Add Files & Dirs to archive. | |
* | |
* @param string $location Real Location | |
* @param string $name Name in 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: FlxZipArchive::addDir(), else ::File(); | |
$do = (filetype($location . $file) == 'dir') ? 'addDir' : 'addFile'; | |
$this->$do($location . $file, $name . $file); | |
} | |
} | |
} | |
// usage | |
$zip = new FlxZipArchive(); | |
$res = $zip->open($zipDirectory . '.zip', ZipArchive::CREATE); | |
if ($res === TRUE) { | |
$zip->addDir($zipDirectory, basename($zipDirectory)); | |
$zip->close(); | |
} else { | |
echo 'error while creating zip'; exit; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment