业务经理

微信扫描以上二维码

028-61286886

技术咨询

不解压缩的情况下获取zip压缩包目录并生成前端树

迅睿框架 更新 2023-08-30 15:50:45 又菜又爱玩


不解压缩的情况下获取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();



       ?>

方案

  • 又菜又爱玩 #1 · 2023-03-08 17:13:24

    image
    配合jstree.js 使用 给各种文件图标
  • 又菜又爱玩 #2 · 2023-03-09 08:01:14
    <?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判断缓存是否存在,不存在才进行一个创建,这样并不是太友好,当页面被采集等问题频繁访问可能导致服务器卡死,需要改进为 上传文件 时进行缓存
    代码可以直接拿走复制黏贴直接使用,需要注意的点,
    文件路径缓存路径
  • 又菜又爱玩 #3 · 2023-03-09 08:50:03
    // 初始化文件树结构
        $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];
            }
        }
    又菜又爱玩 这块有错误,需修改
  • 又菜又爱玩 #4 · 2023-03-10 10:10:32
                    {if $down}
                     <h6 class="card-title">最近更新:{dr_date($myfile.inputtime)}</h4>
                            
       <?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/tmp/cache/';
        $cacheFile = $cacheDir . $cacheName;
        $cacheTime = 86400; // 缓存时间,单位为秒
      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();
       if ($zip->open($zipName) === true) {
    
            // 初始化文件树结构
            $tree = [];
            for ($i = 0; $i < $zip->numFiles; $i++) {
                // 获取文件名,使用ZipArchive类提供的方法解决编码问题
                $filename = $zip->getNameIndex($i, ZipArchive::FL_ENC_RAW);
                // 转换编码为UTF-8
                $filename = iconv('CP936', 'UTF-8', $filename);
                // 分割路径为数组
                $path = explode('/', $filename);
                $node = &$tree;
                foreach ($path as $dir) {
                    if (!isset($node[$dir])) {
                        $node[$dir] = [];
                    }
                    $node = &$node[$dir];
                }
            }
    
            // 生成 HTML 树
            function treeToHtml($tree) {
                $html = '<ul>';
                // 先输出文件夹
                foreach ($tree as $name => $subtree) {
                    if (!empty($name) && empty(pathinfo($name, PATHINFO_EXTENSION))) {
                        $html .= '<li';
                        if (!empty($subtree)) {
                            $html .= ' class="jstree-open"  data-jstree=\'{"type":"folder"}\'';
                        }
                        $html .= '>' . htmlentities($name);
                        if (!empty($subtree)) {
                            $html .= treeToHtml($subtree);
                        }
                        $html .= '</li>';
                    }
                }
    
            // 再输出文件
            foreach ($tree as $name => $subtree) {
                if (!empty($name) && !empty(pathinfo($name, PATHINFO_EXTENSION))) {
                    $ext = pathinfo($name, PATHINFO_EXTENSION);
                    $html .= '<li';
                    if (!empty($subtree)) {
                        $html .= '  data-jstree=\'{"type":"folder"}\'';
                    }
                    $html .= ' data-jstree=\'{"type":"'.$ext.'"}\'';
                    $html .= '>' . htmlentities($name);
                    if (!empty($subtree)) {
                        $html .= treeToHtml($subtree);
                    }
                    $html .= '</li>';
                }
            }
    
        $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  bxs-file-blank'],
            ['folder', '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>
    {else}
    暂无文件
    {/if}
    最终版本:
    解决问题1:文件夹优先显示 文件后显示排序,而不是默认首字母
    解决问题2:获取zip目录时 当文件夹是中文, centos7 默认中文乱码的问题


    image


    image
  • 远策 #5 · 2023-05-22 14:28:40
    学习一下a学习一下a
  • 远策 #6 · 2023-05-22 14:35:11
    又菜又爱玩 学习一下学习一下
  • html #7 · 2023-05-23 11:39:40
    又菜又爱玩 学习一下学习一下
  • atv17690943 #8 · 2023-08-05 15:47:02
    学习一下学习一下
  • atv17690943 #9 · 2023-08-05 15:47:58
    又菜又爱玩 学习一下学习一下
  • 听海 #10 · 2023-08-30 15:50:45
    学习一下学习一下

迅睿系统漏洞

站群分站系统
站群分站系统(2026-10-15)
子站域名新增llms.txt访问
GEO系统
GEO系统(2026-10-14)
修复下载zip空白地址问题 修复流量复刻和任务队列错误
GeoSeo收录宝
GeoSeo收录宝(2026-10-14)
新增 站长工具箱 前端首页自动调用meta 数据
滑块验证码 Gocaptcha
滑块验证码 Gocaptcha(2026-10-13)
基于开源项目 go-captcha 的行为验证码插件,滑动拼图、区域...
GeoSeo工具箱
GeoSeo工具箱(2026-10-13)
优化巡检任务
GeoSeo排名助手
GeoSeo排名助手(2026-10-11)
加入大模型自定义
SEO聚合关键字
SEO聚合关键字(2026-10-11)
优化关键字列表页最多支持1万篇文章,过多会导致服务器扛不住;
CMS内容同步复制
CMS内容同步复制(2026-10-10)
增加时间是否更新的开关