function addToZip($item, &$zip, $zipdir = "") { if (is_dir($item)) { $zipdir .= basename($item) . SYS_PATH_SEP; $zip->add_dir($zipdir); $handle = opendir($item); while ($entry = readdir($handle)) { if (substr($entry, 0, 1) != '.') { addToZip($item . SYS_PATH_SEP . $entry, &$zip, $zipdir); } } } else { if (is_file($item)) { if (strtolower(substr($item, -4, 4)) != '.zip') { $handle = fopen($item, "r"); $contents = fread($handle, filesize($item)); fclose($handle); $zip->add_dir($zipdir); $zip->add_file($contents, $zipdir . basename($item)); } } } }
function addToZip($zip, $basePath, $file, $subPath = '') { global $verbose; $path = empty($subPath) ? $file : "{$subPath}/{$file}"; $srcPath = "{$basePath}/{$path}"; if ($verbose) { print "adding {$file} from {$srcPath} to {$path}\n"; } if (is_file($srcPath)) { if (!$zip->addFile($srcPath, $path)) { print "Failed to add {$path} to {$packagePath}"; return false; } } else { if (is_dir($srcPath)) { $dir = opendir($srcPath); if (!$dir) { print "Could not open content directory in {$entryPath}\n"; return false; } while (false != ($entry = readdir($dir))) { if ($entry == '.' || $entry == '..') { continue; } if (!addToZip($zip, $basePath, $entry, $path)) { return false; } } } } return true; }
function getModel($originalPath) { $sourcePath = $originalPath; $filePath = str_ireplace(DCTL_PROJECT_PATH, '', $sourcePath); $zip = new zipfile(); $zipdir = SYS_PATH_SEP; $zip->add_dir($zipdir); addToZip(DCTL_SETTINGS, $zip, $zipdir); $zipdir = SYS_PATH_SEP; $source = DCTL_PROJECT_PATH; foreach (explode(SYS_PATH_SEP, $filePath) as $item) { $source .= $item; $zipdir .= $item; if (is_dir($source)) { $source .= SYS_PATH_SEP; $zipdir .= SYS_PATH_SEP; $zip->add_dir($zipdir); $item = $source . DCTL_FILE_HEADER; if (is_file($item)) { addToZip($item, $zip, $zipdir); } // $item = $source.DCTL_MEDIA_SML; // if (is_dir($item)) { // addToZip($item, $zip, $zipdir); // }; } } $zipdir = dirname($zipdir) . SYS_PATH_SEP; if (is_dir($source)) { if (stripos(DCTL_MEDIA_SML, $source) === FALSE && stripos(DCTL_MEDIA_MED, $source) === FALSE) { addToZip($source, $zip, $zipdir); } } else { if (is_file($source)) { $item = $source; addToZip($item, $zip, $zipdir); } } $filePath = dirname($originalPath) . SYS_PATH_SEP . str_ireplace(SYS_PATH_SEP, '#', $filePath) . '.zip'; $fd = fopen($filePath, "wb"); $out = fwrite($fd, $zip->file()); fclose($fd); @chmod($filePath, CHMOD); return $filePath; }