add method for zipping files instead of using zip command

This commit is contained in:
Uwe Steinmann 2019-12-10 15:59:53 +01:00
parent 658d00fe42
commit f453cac8d2

View File

@ -190,6 +190,61 @@ class SeedDMS_Extension_Mgr {
return $extensions;
} /* }}} */
static protected function Zip($source, $destination, $include_dir = false) { /* {{{ */
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
if (file_exists($destination)) {
unlink ($destination);
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
if ($include_dir) {
$arr = explode("/",$source);
$maindir = $arr[count($arr)- 1];
$source = "";
for ($i=0; $i < count($arr) - 1; $i++) {
$source .= '/' . $arr[$i];
}
$source = substr($source, 1);
$zip->addEmptyDir($maindir);
}
foreach ($files as $file) {
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
} else if (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
} /* }}} */
/**
* Create zip archive of an extension
*
@ -203,8 +258,11 @@ class SeedDMS_Extension_Mgr {
$tmpfile = $this->cachedir."/".$extname."-".$version.".zip";
$cmd = "cd ".$this->extdir."/".$extname."; zip -r ".$tmpfile." .";
exec($cmd);
if(!SeedDMS_Extension_Mgr::Zip($this->extdir."/".$extname, $tmpfile)) {
return false;
}
// $cmd = "cd ".$this->extdir."/".$extname."; zip -r ".$tmpfile." .";
// exec($cmd);
return $tmpfile;
} /* }}} */