Last active
March 24, 2017 11:18
-
-
Save argentinaluiz/d2a6a4c5d220642101218fe78372c537 to your computer and use it in GitHub Desktop.
ExtendedZip
This file contains hidden or 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 | |
use ZipArchive; | |
class ExtendedZip extends ZipArchive | |
{ | |
// Member function to add a whole file system subtree to the archive | |
public function addTree($dirname, $localname = '') | |
{ | |
if ($localname) { | |
$this->addEmptyDir($localname); | |
} | |
$this->_addTree($dirname, $localname); | |
} | |
// Internal function, to recurse | |
protected function _addTree($dirname, $localname) | |
{ | |
$dir = opendir($dirname); | |
while ($filename = readdir($dir)) { | |
// Discard . and .. | |
if ($filename == '.' || $filename == '..') { | |
continue; | |
} | |
// Proceed according to type | |
$path = $dirname . '/' . $filename; | |
$localpath = $localname ? ($localname . '/' . $filename) : $filename; | |
if (is_dir($path)) { | |
// Directory: add & recurse | |
$this->addEmptyDir($localpath); | |
$this->_addTree($path, $localpath); | |
} else { | |
if (is_file($path)) { | |
// File: just add | |
$this->addFile($path, $localpath); | |
} | |
} | |
} | |
closedir($dir); | |
} | |
// Helper function | |
public static function zipTree($dirname, $zipFilename, $flags = 0, $localname = '') | |
{ | |
$zip = new self(); | |
$zip->open($zipFilename, $flags); | |
$zip->addTree($dirname, $localname); | |
$zip->close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment