2

我正在尝试使用 R 从从包含 JSON 文件的 API 下载的 LZMA 存档中提取文件。在我的计算机上,我可以在 Windows 资源管理器中手动提取文件,没有任何问题。

这是我目前的代码(已删除 API 详细信息):

tempFile <- tempfile()
destDir <- "extracted-files"
if (!dir.exists(destDir)) dir.create(destDir)

download.file("api_url.tar.xz", destfile = tempFile)
untar(tempFile, exdir = destDir)

当我尝试提取文件时,我收到以下错误消息:

/usr/bin/tar: This does not look like a tar archive
/usr/bin/tar: Skipping to next header
/usr/bin/tar: Exiting with failure status due to previous errors
Warning messages:
1: running command 'tar.exe -xf "C:\Users\XXX\AppData\Local\Temp\RtmpMncPWp\file2eec75e23a15" -C "extracted-files"' had status 2 
2: In untar(tempFile, exdir = destDir) :
  ‘tar.exe -xf "C:\Users\XXX\AppData\Local\Temp\RtmpMncPWp\file2eec75e23a15" -C "extracted-files"’ returned error code 2

我正在使用带有 R 版本 3.3.1 (2016-06-21) 的 Windows 10。

4

2 回答 2

0

解决了:

虽然它似乎在 Mac 上运行良好,但要在 Windows 上运行,您需要打开压缩的 .xz 文件连接以便以二进制模式读取,然后将其传递给 untar():

download.file(url, tmp)
zz <- xzfile(tmp, open = "rb")
untar(zz, exdir = destDir)
于 2017-03-13T13:06:32.750 回答
0

另一种更简单的解决方案是为 download.file() 指定“模式”参数,如下所示:

download.fileurl, destfile = tmp, mode = "wb")
于 2017-03-14T11:20:21.810 回答