如何求解'Error 1 in extracting from .zip'(R)

How to solve 'Error 1 in extracting from .zip' (R)

作为 Google DA 证书分配的一部分,我试图找到一个关于如何使用 R 下载解压缩和合并多个 .csv 文件的优雅解决方案,但我一直面临同样的问题:

从 zip 文件中提取错误 1

数据: 资料来源:Divvy

我运行的代码是:

## declare variable file names corresponding to calendar months
months <- c(202011:202012,202101:202110)

## declare directory for storing source files
storage <- "C:\Users\...\start"

## vectors of all urls to download from and destination files
urls <- 
  paste0("https://divvy-tripdata.s3.amazonaws.com/",months, "-divvy-tripdata.zip")
  
## idea was to download archives into temporary files, unzip contents to 'storage' directory and remove tempdir.

temp <- tempdir()
tempfile <- paste0(temp,"\",months,".zip")

##Downloading 12 months archives
for(i in seq(urls)){
  download.file(urls[i],tempfile[i], mode="wb")
}

file_names <- list.files(temp, pattern = ".zip")

for (i in seq(file_names)){
  unzip(file_names,exdir=storage,overwrite = FALSE)}

解压警告("file_names", exdir = storage, overwrite = FALSE) : 从 zip 文件中提取错误 1

一切正常,直到解压缩步骤。所有档案都已下载,可以打开,文件未损坏,属性显示扩展名为 .zip

我已经在多台机器上的不同目录中尝试了我的代码,尝试手动下载档案,尝试解压缩每个人并使用循环一次全部解压缩,ldply 仍然是相同的结果。

我花了 3 天时间尝试解决它,感谢任何帮助:)

使用相同的monthsurls变量,以下似乎更简单。请注意将临时文件名放在一起的不同方式,file.path.

tmpdir <- tempdir()
tmpfile <- file.path(tmpdir, months)
tmpfile <- paste0(tmpfile, ".zip")

##Downloading 12 months archives
for(i in seq(urls)){
  download.file(urls[i], tmpfile[i], mode="wb")
  unzip(tmpfile[i], exdir = storage, overwrite = FALSE)
}
unlink(tmfile)
unlink(tmpdir)

list.files(storage, pattern = "\.csv")
# [1] "202011-divvy-tripdata.csv" "202012-divvy-tripdata.csv"
# [3] "202101-divvy-tripdata.csv" "202102-divvy-tripdata.csv"
# [5] "202103-divvy-tripdata.csv" "202104-divvy-tripdata.csv"
# [7] "202105-divvy-tripdata.csv" "202106-divvy-tripdata.csv"
# [9] "202107-divvy-tripdata.csv" "202108-divvy-tripdata.csv"
#[11] "202109-divvy-tripdata.csv" "202110-divvy-tripdata.csv"