通过 .gz 列表解压
untar through a list of .gz
在 R 中,我想从该站点的每个目录下载并解压所有 .gz 文件:ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI/
我在这方面遇到了困难:我将这里 ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI/ 中的所有 ~60 个 .gz 放在 R 的列表中,并想将每个解压到一个目录中,但为了我的生活想不通。
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
> typeof(list_gz)
[1] "list"
> head(list_gz)
[[1]]
[1] "ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz"
[[2]]
[1] "ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201402.nc.gz"
> sapply(list_gz, function(i) getURL(untar(i)))
gzip: can't stat: ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz (ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz.gz): No such file or directory
Show Traceback
Rerun with Debug
Error in function (type, msg, asError = TRUE) :
Failed to connect to 0 port 80: Connection refused
这里我不太确定。也许我应该修改我的前半部分代码并下载 ~60 .gzs 而不是尝试以 list/sapply 方法下载和解压缩它们。谢谢!
我可能会下载然后解压。
确保设置工作目录。
library(curl)
library(stringr)
list_gz = list("ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz",
"ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201402.nc.gz")
sapply(list_gz, function(x) {
# this will dl the file and save with same name (name after last '/')
file_name = sub(".*//(.*)/", "", x)
year = c(str_match(file_name, "\d\d\d\d"))
if(!dir.exists(year)) dir.create(year)
curl_download(x, destfile = paste0(year, "/", file_name))
# insert code for unzipping here - my computer wouldnt let me untar the files
# untar(paste0(year, "/", file_name))
})
在 R 中,我想从该站点的每个目录下载并解压所有 .gz 文件:ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI/
我在这方面遇到了困难:我将这里 ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI/ 中的所有 ~60 个 .gz 放在 R 的列表中,并想将每个解压到一个目录中,但为了我的生活想不通。
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
> typeof(list_gz)
[1] "list"
> head(list_gz)
[[1]]
[1] "ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz"
[[2]]
[1] "ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201402.nc.gz"
> sapply(list_gz, function(i) getURL(untar(i)))
gzip: can't stat: ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz (ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz.gz): No such file or directory
Show Traceback
Rerun with Debug
Error in function (type, msg, asError = TRUE) :
Failed to connect to 0 port 80: Connection refused
这里我不太确定。也许我应该修改我的前半部分代码并下载 ~60 .gzs 而不是尝试以 list/sapply 方法下载和解压缩它们。谢谢!
我可能会下载然后解压。
确保设置工作目录。
library(curl)
library(stringr)
list_gz = list("ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201401.nc.gz",
"ftp://ftp.dwd.de/pub/data/gpcc/GPCC_DI//2014/GPCC_DI_201402.nc.gz")
sapply(list_gz, function(x) {
# this will dl the file and save with same name (name after last '/')
file_name = sub(".*//(.*)/", "", x)
year = c(str_match(file_name, "\d\d\d\d"))
if(!dir.exists(year)) dir.create(year)
curl_download(x, destfile = paste0(year, "/", file_name))
# insert code for unzipping here - my computer wouldnt let me untar the files
# untar(paste0(year, "/", file_name))
})