不解压缩的情况下获取zip压缩包目录并生成前端树
代码是在前端页面的,结合https://www.xunruicms.com/wenda/62039.html
使用
<?php
$url = dr_get_file($down);
$parts = parse_url($url);
$path = $parts['path'];
$zipName='/www/wwwroot/tmp/'. $path;
$zip = new ZipArchive();
$zip->open($zipName);
$tree = array();
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$path = explode('/', $filename);
$node = &$tree;
foreach ($path as $dir) {
if (!isset($node[$dir])) {
$node[$dir] = array();
}
$node = &$node[$dir];
}
}
function treeToHtml($tree) {
$html = '<ul>';
foreach ($tree as $name => $subtree) {
if (!empty($name)) {
$html .= '<li>';
$html .= $name;
if (!empty($subtree)) {
$html .= treeToHtml($subtree);
}
$html .= '</li>';
} else {
$html .= treeToHtml($subtree);
}
}
$html .= '</ul>';
return $html;
}
$html = treeToHtml($tree);
echo $html;
$zip->close();
?>
配合jstree.js 使用 给各种文件图标
<?php // 获取文件路径 $url = dr_get_file($down); $parts = parse_url($url); $path = $parts['path']; $zipName = '/www/wwwroot/tmp/' . $path; // 检查缓存是否存在且未过期 $cacheName = 'tree-' . md5($zipName); $cacheDir = '/www/wwwroot/cache/'; $cacheFile = $cacheDir . $cacheName; $cacheTime = 3600; // 缓存时间,单位为秒 if (is_file($cacheFile) && (time() - filemtime($cacheFile) < $cacheTime)) { // 从缓存中获取 HTML 树 $html = file_get_contents($cacheFile, false, stream_context_create(['http' => ['timeout' => 5]])); } else { // 打开 zip 文件并获取文件结构 $zip = new ZipArchive(); $zip->open($zipName); // 初始化文件树结构 $tree = []; for ($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); $path = explode('/', $filename); $node = &$tree; foreach ($path as $dir) { $node = &$node[$dir] ?? ($node[$dir] = []); } } // 生成 HTML 树 function treeToHtml($tree) { $html = '<ul>'; foreach ($tree as $name => $subtree) { if (!empty($name)) { $ext = pathinfo($name, PATHINFO_EXTENSION); $html .= '<li'; if (!empty($subtree)) { $html .= ' class="jstree-open"'; } if (!empty($ext)) { $html .= ' data-jstree=\'{"type":"'.$ext.'"}\''; } $html .= '>' . htmlspecialchars($name); if (!empty($subtree)) { $html .= treeToHtml($subtree); } $html .= '</li>'; } else { $html .= treeToHtml($subtree); } } $html .= '</ul>'; return $html; } $html = treeToHtml($tree); // 关闭 zip 文件并保存 HTML 树到缓存文件 $zip->close(); file_put_contents($cacheFile, $html); } // 输出 HTML 树 echo '<div id="jstree-basic">' . $html . '</div>'; ?> <script> $(document).ready(function() { function generateIconObject() { const icons = [ ['default', 'bx bx-folder'], ['doc', 'bx bxs-file-word text-primary'], ['docx', 'bx bxs-file-word text-primary'], ['xls', 'bx bxs-file-excel text-success'], ['xlsx', 'bx bxs-file-excel text-success'], ['ppt', 'bx bxs-file-powerpoint text-danger'], ['pptx', 'bx bxs-file-powerpoint text-danger'], ['pdf', 'bx bxs-file-pdf text-danger'], ['html', 'bx bxl-html5 text-warning'], ['css', 'bx bxl-css3 text-primary'], ['scss', 'bx bxl-sass text-primary'], ['js', 'bx bxl-javascript text-warning'], ['json', 'bx bxs-file-json text-success'], ['php', 'bx bxl-php text-primary'], ['py', 'bx bxl-python text-success'], ['rb', 'bx bxl-ruby text-danger'], ['java', 'bx bxl-java text-warning'], ['sql', 'bx bx-data text-danger'], ['xml', 'bx bx-code-alt text-primary'], ['csv', 'bx bxs-file-csv text-warning'], ['jpg', 'bx bx-image text-success'], ['jpeg', 'bx bx-image text-success'], ['png', 'bx bx-image text-success'], ['gif', 'bx bx-image text-success'] ]; const types = {}; icons.forEach(icon => { types[icon[0]] = { 'icon': icon[1] }; }); return types; } $('#jstree-basic').jstree({ 'plugins': ['types'], 'types': generateIconObject() }); }); </script>加了缓存以及缓存判断,但是是纯php写法,个人就单纯使用在一个模板页面里,就直接写在模板里了,这里的需要可以拿去参考,提供了大部分jstree 图标调用的代码,缺点1:写在html中的php,不是写成函数什么的
缺点2:缓存时单纯的文件缓存,当页面大量的情况下,容易造成性能瓶颈及磁盘占用,使用内存缓存 为优缺点3:缓存是访问当前页面,然后php进行一个if判断缓存是否存在,不存在才进行一个创建,这样并不是太友好,当页面被采集等问题频繁访问可能导致服务器卡死,需要改进为 上传文件 时进行缓存
代码可以直接拿走复制黏贴直接使用,需要注意的点,
文件路径缓存路径
// 初始化文件树结构 $tree = []; for ($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); $path = explode('/', $filename); $node = &$tree; foreach ($path as $dir) { if (!isset($node[$dir])) { $node[$dir] = []; } $node = &$node[$dir]; } }又菜又爱玩 这块有错误,需修改解决问题1:文件夹优先显示 文件后显示排序,而不是默认首字母
解决问题2:获取zip目录时 当文件夹是中文, centos7 默认中文乱码的问题