Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extended ZipArchive - simply way to create zip with subdirectories
<?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