我正在尝试使用R下载并解压缩一个zip文件。每当这样做时,我都会收到错误消息
解压缩错误(temp,列表= TRUE):'exdir'不存在
我正在使用基于堆栈溢出问题Using R to download zipped data file, extract, and import data的代码
举个简单的例子:
# Create a temporary file
temp <- tempfile()
# Download ZIP archive into temporary file
download.file("http://cran.r-project.org/bin/windows/contrib/r-release/ggmap_2.2.zip",temp)
# ZIP is downloaded successfully:
# trying URL 'http://cran.r-project.org/bin/windows/contrib/r-release/ggmap_2.2.zip'
# Content type 'application/zip' length 4533970 bytes (4.3 Mb)
# opened URL
# downloaded 4.3 Mb
# Try to do something with the downloaded file
unzip(temp,list=TRUE)
# Error in unzip(temp, list = TRUE) : 'exdir' does not exist
到目前为止我尝试过的:
R版本2.15.2
R-Studio版本0.97.306
编辑:如果我使用unz而不是unzip,该代码将起作用,但我无法弄清为什么一个起作用而另一个却不起作用。从CRAN指南:
在Windows设置上:将exdir指定为路径时出现此错误。对我来说,解决方案是删除路径名中的尾随/或\\。
这是一个示例,如果不存在,它会创建新文件夹
locFile <- pathOfMyZipFile
outPath <- "Y:/Folders/MyFolder"
# OR
outPath <- "Y:\\Folders\\MyFolder"
unzip(locFile, exdir=outPath)
迟了几年,但尝试使用unzip()
时仍然出现此错误。这似乎是一个错误,因为如果指定了unzip
,则会创建exdir
状态的手册页:
exdir将文件解压缩到的目录(相当于unzip -d)。必要时将创建它。
我一直在使用的解决方法是手动创建必要的目录:
dir.create("directory")
unzip("file-to-unzip.zip", exdir = "directory/")
一种痛苦,但它似乎起了作用,至少对我而言。
这可以体现出另一种方式,并且文档中并未阐明原因。 您的扩展名不能以“ /”结尾,它必须仅仅是目标文件夹的名称。
例如,由于'exdir'而失败:
unzip(temp, overwrite = F, exdir = "data_raw/system-data/")
这很好用:
unzip(temp, overwrite = F, exdir = "data_raw/system-data")
大概是当解压缩在exdir路径的末尾看到“ /”时,它一直在寻找;而省略“ /”则表示解压缩“您已找到它,请在此处解压缩”。
我正在Windows 7计算机上使用R3.2.1。
我发现解决此问题的方法需要一些步骤,但对我有用:
file_url http://your.file.com/file_name.zip”
download.file(file_url,“ file_name.zip”)
*如果不确定您的工作目录,则可以使用getwd()进行检查。如果要更改工作目录,可以使用setwd(“ C:users / username / ...”)将其设置为所需的目录。
unzip(“ file_name.zip”,exdir =“ file_name”)
list.files(“ file_name”)
希望这会有所帮助!
链接http://transparencia.al.gov.br/media/arquivo/comparativo_despesas-2017.zip”
dir.create(“ Dados /”)
download.file(link,destfile =“ Dados / desp_al_2017.zip”)
unzip(“ Dados / desp_al_2017.zip”,exdir =“ ./Dados”)