douqi3195 2015-04-07 07:21
浏览 83
已采纳

来自GitLab的私有存储库存档(tar.gz),通过GitLab的API使用PHP下载,似乎已被破坏 - 校验和无效

I want to download a private GitLab repository archive using their API. I'm using PHP and cURL to do that. I'm able to do so using the following code:

$ch = curl_init(http://example.com/api/v3/projects/64/repository/archive?private_token=private_token_goes_here);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

$output = curl_exec($ch);
$fh = fopen("out.tar.gz", 'w');
fwrite($fh, $output);
fclose($fh);

The code itself is nothing complicated and pretty standard.

The issue I'm facing is that once I try to unpack, using the code bellow:

    // decompress from gz
    $p = new PharData($temp_archive . '.tar.gz');
    $p->decompress($temp_archive . '.tar');

    // unarchive from the tar
    $phar = new PharData($temp_archive . '.tar');
    $phar->extractTo($extract_path);

I receive the following error:

Fatal error: Uncaught exception 'UnexpectedValueException' with message ' in phpfile.php on line 294

UnexpectedValueException: phar error: "path/to/downloaded/archive/temp_archive.tar.gz" is a corrupted tar file (checksum mismatch of file "52 comment=01607149cd460cba6f1ac3003bb7b0c9cb0e2b94
") in phpfile.php on line 294

Yes, that is the whole error, I didn't cut out the error message. The ' is the error message.

The checksum for the downloaded file is, indeed, invalid. After a few good hours of Googling and forum browsing I'm back to square one. I have no idea what's wrong with it.

If I was to untar the archive using a terminal(I'm on MAC OS Build 12E55), it will unpack normally. The files are all there and I receive no errors what-so-ever.

I tried manually downloading the archive from GitLab and unpacking it using PHP(I get the same error).

I tried creating a *.tar.gz file using a terminal and unpacking it using PHP(I receive no errors and the unpacking proceeds normally).

I tried uploading a file created using the terminal and downloading it using PHP, after which I unpacked it, again using PHP, and I receive no errors.

I can only assume that I'm either:

  • Not using their API properly
  • Not setting the right headers - although I had tried setting:
    1. Content-Type: application/octet-stream
    2. Content-Type: application/x-tar
    3. Content-Transfer-Encoding: binary...and so on. When using the binary header, the file that I put the content in was set to 'wb'. Still, no luck.

And lastly it could be that their API may not be working properly, although I doubt it.

And nudging in the right direction is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanjiaoxi4928 2015-04-07 08:39
    关注

    This appears to be a bug in the PharData class implementation that was fixed only very recently, see https://bugs.php.net/bug.php?id=64343.

    So it seems that Github uses the pax format for tar files, instead of the ustar format. For a description on tar formats, see: https://github.com/libarchive/libarchive/wiki/FormatTar

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)