I'm using php ziparchive function to open zip file and want to list all subfolders of a folder located in a zip file. For example... World.zip contains a folder country,foo & bar and I want to list names of all states folder of that country folder,but not of foo & bar
1条回答 默认 最新
- douling0053 2016-07-16 03:57关注
i found this note on the manuals which i got most of the code from which depends on looping through the total number of files
and i refactored it to match your needs and here is the code after refactoring
<?php $filePath = 'zip/file.zip'; $za = new ZipArchive(); if ($za->open($filePath) !== true) { // check for the zip archive echo "archive doesn't exist or it's on Read-only mode "; } else { $Tree = $pathArray = array(); //empty arrays for ($i = 0; $i < $za->numFiles; $i++) { $path = $za->getNameIndex($i); $pathBySlash = array_values(explode('/', $path)); $c = count($pathBySlash); $temp = &$Tree; for ($j = 0; $j < $c - 1; $j++) if (isset($temp[$pathBySlash[$j]])) $temp = &$temp[$pathBySlash[$j]]; else { $temp[$pathBySlash[$j]] = array(); $temp = &$temp[$pathBySlash[$j]]; } if (substr($path, -1) == '/') $temp[$pathBySlash[$c - 1]] = array(); else $temp[] = $pathBySlash[$c - 1]; } $array = $Tree['folder_name_to_list_its_files']; // First style of Displaying echo "<pre>"; print_r($array); // Second style of Displaying foreach ($array as $key => $value) { foreach ($value as $val) { echo $key . " | " . $val . "<br /> "; } } echo "</pre>"; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 matlab有关常微分方程的问题求解决
- ¥15 perl MISA分析p3_in脚本出错
- ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
- ¥15 ubuntu虚拟机打包apk错误
- ¥199 rust编程架构设计的方案 有偿
- ¥15 回答4f系统的像差计算
- ¥15 java如何提取出pdf里的文字?
- ¥100 求三轴之间相互配合画圆以及直线的算法
- ¥100 c语言,请帮蒟蒻写一个题的范例作参考
- ¥15 名为“Product”的列已属于此 DataTable