c# - 无法在 Octokit.net 中将存储库内容作为 .zip 文件 (zipball)

标签 c# .net zip github-api octokit.net

我正在使用 Octokit.net 0.9.0 版(GitHub API for .NET),用于获取少数存储库的 zip 内容。

我已经有了我需要的存储库列表,但我无法将存储库的内容作为 .zip 文件(称为 zipball)获取

到目前为止我尝试了什么

// ... client = new Client(...);
// some authentication logic...
// some other queries to GitHub that work correctly
var url = "https://api.github.com/repos/SomeUser/SomeRepo/zipball";
var response = await this.client.Connection.Get<byte[]>(
        new Uri(url),
        new Dictionary<string, string>(),
        null);
var data = response.Body;
var responseData = response.HttpResponse.Body;

我的尝试有问题​​

  1. 数据为空
  2. responseData.GetType().Name 表示 responseData 是字符串类型
  3. 当我尝试 Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString()); 我得到无效的 zip 文件

Value of response.HttpResponse.Body

问题

在使用 Octokit.net 库进行身份验证后获取存储库 zipball 的正确方法是什么?

我也打开了an issue在 octokit.net 存储库中。

最佳答案

在检查了 Octokit 的源代码后,我认为这是不可能的(从版本 0.10.0 开始):

参见 Octokit\Http\HttpClientAdapter.cs

// We added support for downloading images. Let's constrain this appropriately.
if (contentType == null || !contentType.StartsWith("image/"))
{
    responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else
{
    responseBody = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}

响应主体 (responseData) 被转换为一个 unicode 字符串,因此,它被破坏了,而不是二进制的。看我的PR792 (需要将 if 语句替换为 if (contentType == null || (!contentType.StartsWith("image/") && !contentType.StartsWith("application/"))))来修复这(然后它是一个字节数组。可以使用 System.IO.File.WriteAllBytes("c:\\test.zip", (byte[])responseData); 编写)。

关于c# - 无法在 Octokit.net 中将存储库内容作为 .zip 文件 (zipball),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766916/

相关文章:

c# - 在 EF 中不共享主键的多态关联

Ruby - 读取 zip 文件中的文本文件的最简单方法

c# - SonarQube 将它与 Unity 3D 一起使用时会遇到很多问题

c# 两种形式之间的事件处理

c# - 将 Android View 作为自定义控件集成到 Xamarin.Forms 中

c# - 如何从抛出异常的方法中通过 out/ref 参数获取值?

python - 如何在 Python 中仅列出 zip 存档中的文件夹?

haskell - 使用 Haskell 从 zip 存档中提取单个文件

c# - 如何使用CEF上传文件?

c# - VS2013 : "VSP2340: Environment variables were not properly set" even when running from IDE