R:解压缩大型压缩 .csv 会产生“zip 文件已损坏”警告

Dan*_*Tan 7 csv r unzip

我正在从联合国粮农组织下载一个 78MB 的 zip 文件,其中包含一个 2.66GB 的 csv。我可以使用 winzip 从文件夹中解压缩下载的文件,但无法unzip()在 R 中使用解压缩文件:

警告 - 下载 78MB!

url <- "http://fenixservices.fao.org/faostat/static/bulkdownloads/FoodBalanceSheets_E_All_Data_(Normalized).zip"
path <- file.path(getwd(),"/zipped_data.zip")
download.file(url, path, mode = "wb")
unzipped_data <- unzip(path)
Run Code Online (Sandbox Code Playgroud)

这会导致警告并且无法解压缩文件:

警告信息

在 unzip(path) 中:zip 文件已损坏

?unzip文档中我看到

“它确实对 bzip2 压缩和 > 2GB zip 文件有一些支持(但不支持 zip 文件中包含的 >= 4GB 文件预压缩:像许多 unzip 版本一样,它可能会截断这些文件,在 R 的情况下,如果可能的话会发出警告)”

这让我相信unzip()应该处理我的文件,但同样的过程已成功下载、解压缩并从FAOstat 读取多个其他较小的表格。我的 csv 大小是否有可能是此错误的根源?如果是这样,解决方法是什么?

Moo*_*per 1

我无法测试我的解决方案,它也取决于您的安装,但希望这会起作用,或者至少为您指出一个合适的解决方案:

您可以通过命令行运行winzip,此页面显示了调用的结构

您还可以从 R 运行命令行,使用systemor shell(这只是一个包装器system

要提取的命令行一般结构为:

winzip32 -e [options] filename[.zip] folder
Run Code Online (Sandbox Code Playgroud)

因此,我们使用此结构和输入路径创建一个字符串,并围绕它创建一个函数,该函数模仿unzip参数zipfileexdir

unzip_wz <- function(zipfile,exdir){
  dir.create(exdir,recursive = FALSE,showWarnings=FALSE) # I don't know how/if unzip creates folders, you might want to tweak or remove this line altogether
  str1 <- sprintf("winzip32 -e '%s' '%s'",zipfile,exdir)
  shell(str1,wait = TRUE)  # set to FALSE if you want the program to keep running while unzipping, proceed with caution but in some cases that could be an improvement of your current solution
}
Run Code Online (Sandbox Code Playgroud)

您可以尝试使用此功能来代替unzip. 它假设 winzip32 已添加到您的系统路径变量中,如果没有,请添加它,或将其替换为 exec 全名,这样您就可以得到如下内容:

str1 <- sprintf("'C://probably/somewhere/in/program/files/winzip32.exe' -e '%s' '%s'",zipfile,exdir)
Run Code Online (Sandbox Code Playgroud)

PS:使用完整路径!命令行不知道您的工作目录(如果需要,我们可以在函数中实现该功能)。