从GitHub repo下载单个文件有哪些技巧?

我不想要显示原始文件的URL;对于二进制文件,什么都没有。

http://support.github.com/discussions/feature-requests/41-download-single-file

是否有可能将GitHub用作“下载服务器”?

如果我们决定切换到GoogleCode,是否提供了上述功能?

或者开源项目是否有免费托管和VCS?


当前回答

您可以尝试github文件获取器,它是一个命令行工具,可以从github repo下载单个文件夹或文件。

想象一个真实的场景:您正在访问以下网页,并想单独下载异步子目录。

https://github.com/reduxjs/redux/tree/master/examples

很抱歉不能发布图片。

使用github文件获取器,您应该首先复制该页面的urlhttps://github.com/reduxjs/redux/tree/master/examples/async,然后在命令行中运行以下命令:

提取器--url=https://github.com/reduxjs/redux/tree/master/examples/async

其他回答

或者试试这个

const https = require('https');
const fs = require('fs');
const DOMAIN = 'raw.githubusercontent.com';

function writeFile(data, fileName) {
  fs.appendFile(fileName, data.toString(), err => {
    if (err) {
      console.log('error in writing file', err);
    }
  });
}

function EOF(data) {
  console.log('EOF');
}

function getFileName(pathToFile) {
  var result = pathToFile.split('/');
  var splitLength = result.length;
  return result[splitLength - 1];
}
function getFile(branchName, username, repoName, ...pathToFile) {
  pathToFile.forEach(item => {
    const path = `/${username}/${repoName}/${branchName}/${item}`;
    const URL = `${DOMAIN}${path}`;
    const options = {
      hostname: DOMAIN,
      path: path
    };
    var fileName = getFileName(item);

    https
      .get(options, function(res) {
        console.log(res.statusCode);
        /* if file not found */
        if (res.statusCode === 404) {
          console.log('FILE NOT FOUND');
        } else {
          /* if file found */
          res.on('data', data => writeFile(data, fileName));
          res.on('end', data => EOF(data));
        }
      })
      .on('error', function(res) {
        console.log('error in reading URL');
      });
  });
}
getFile('master', 'bansalAyush', 'InstagramClone', '.babelrc', 'README.md');

您可以尝试github文件获取器,它是一个命令行工具,可以从github repo下载单个文件夹或文件。

想象一个真实的场景:您正在访问以下网页,并想单独下载异步子目录。

https://github.com/reduxjs/redux/tree/master/examples

很抱歉不能发布图片。

使用github文件获取器,您应该首先复制该页面的urlhttps://github.com/reduxjs/redux/tree/master/examples/async,然后在命令行中运行以下命令:

提取器--url=https://github.com/reduxjs/redux/tree/master/examples/async

您应该只使用文件的原始URL进行操作。

例如,下载AFNetworking的README:

curl https://raw.githubusercontent.com/AFNetworking/AFNetworking/master/README.md > ADREADME.md 

由于它是公共回购,因此您不需要任何凭据。请注意url的类型:raw.githubusercontent.com/path/to/file

只需将wget与raw=True参数一起使用

wget "https://github.com/user/repository/blob/master/directory/file_name?raw=True" -O target_path/file_name

可以这样使用curl:

卷曲-OLhttps://raw.githubusercontent.com/<username>/<repo名称>/<branch名称>/path/to/file

O表示curl下载内容L表示卷曲遵循重定向