Skip to content

Instantly share code, notes, and snippets.

@emresaracoglu
Forked from stajnert/FlxZipArchive.class.php
Created September 20, 2016 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emresaracoglu/ea3b19a1327ad3444b17d72d870855ae to your computer and use it in GitHub Desktop.
Save emresaracoglu/ea3b19a1327ad3444b17d72d870855ae to your computer and use it in GitHub Desktop.
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