douxian0279 2018-02-03 02:14
浏览 40
已采纳

递归文件和目录列出github

I`m looking for a way to list all files and directories of a github release

For example:

root
-> x.txt
-> folder1
   -> folder2
      -> file-0.txt
   -> file-1.txt

would return:

root/x.txt
root/folder1/file-1.txt
root/folder1/folder2/file-0.txt

Is there a way to do this directly with github api? if not with php and api?

  • 写回答

2条回答 默认 最新

  • dongxu7408 2018-02-08 03:27
    关注

    In PHP:

    function github_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: token  XXX_TOKEN"));
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    return curl_exec($ch);
    }
    
    $cache = github_curl("https://api.github.com/repos/LucLaverdure/Wizard.Build/releases/latest?client_secret=XXX_SECRET");
    $cache = json_decode($cache, true);
    $cache_file = $cache["zipball_url"];
    
    // get zip file, build it if new
    if (!file_exists(dirname(__FILE__)."/cache/".basename($cache_file).".zip")) {
        $cache_download = github_curl($cache_file);
        file_put_contents(dirname(__FILE__)."/cache/".basename($cache_file).".zip", $cache_download);
    }
    
    
    // list files within zip file (FTP ONLY)
    $zip = new ZipArchive; 
    if ($zip->open(dirname(__FILE__)."/cache/".basename($cache_file).".zip") == TRUE) {
     for ($i = 0; $i < $zip->numFiles; $i++) {
         $filename = $zip->getNameIndex($i);
         echo $filename."<br>";
     }
    }
    

    Voila!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?