【问题标题】:Recursive files and directories list github递归文件和目录列表 github
【发布时间】:2018-07-13 13:56:52
【问题描述】:

我正在寻找一种方法来列出 github 版本的所有文件和目录

例如:

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

会返回:

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

有没有办法直接用 github api 做到这一点?如果不使用php和api?

【问题讨论】:

    标签: php git github


    【解决方案1】:

    在 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>";
     }
    }
    

    瞧!

    【讨论】:

    • 很好,比我的回答更准确。 +1
    【解决方案2】:

    repo content APIrelease API 都不会枚举文件。
    要获取 repo 内容,您可能需要使用Tree API

    但是对于发布(可以包括 zip 文件或任何其他资产),您需要类似 aatishnn/lszip 的东西:一种列出远程存档内容的方法(此处使用 python)。

      -

    【讨论】:

    • 很遗憾我不能使用 Python... 有任何 php 端口吗?
    • 该应用程序必须在共享托管服务器上运行,shell 访问受限,所以不,我无法执行 python...
    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2011-03-10
    • 1970-01-01
    • 2010-10-04
    • 2021-01-05
    • 1970-01-01
    相关资源
    最近更新 更多