递归ftp下载,然后提取gz文件

时间:2011-03-08 01:49:10

标签: r

我想在R中做一个多步文件下载过程。我有中间步骤,但不是第一个和第三个......

# STEP 1 Recursively find all the files at an ftp site 
# ftp://prism.oregonstate.edu//pub/prism/pacisl/grids
all_paths <- #### a recursive listing of the ftp path contents??? ####

# STEP 2 Choose all the ones whose filename starts with "hi"
all_files <- sapply(sapply(strsplit(all_paths, "/"), rev), "[", 1)
hawaii_log <- substr(all_files, 1, 2) == "hi"
hi_paths <- all_paths[hawaii_log]
hi_files <- all_files[hawaii_log]

# STEP 3 Download & extract from gz format into a single directory
mapply(download.file, url = hi_paths, destfile = hi_files)
## and now how to extract from gz format?

3 个答案:

答案 0 :(得分:7)

对于第1部分,RCurl可能会有所帮助。 getURL函数检索一个或多个URL; dirlistonly列出目录的内容而不检索文件。该函数的其余部分创建了下一级url

library(RCurl)
getContent <- function(dirs) {
    urls <- paste(dirs, "/", sep="")
    fls <- strsplit(getURL(urls, dirlistonly=TRUE), "\r?\n")
    ok <- sapply(fls, length) > 0
    unlist(mapply(paste, urls[ok], fls[ok], sep="", SIMPLIFY=FALSE),
           use.names=FALSE)
}

首先从

开始
dirs <- "ftp://prism.oregonstate.edu//pub/prism/pacisl/grids"

我们可以调用此函数并查找看起来像目录的内容,直到完成

fls <- character()
while (length(dirs)) {
    message(length(dirs))
    urls <- getContent(dirs)
    isgz <- grepl("gz$", urls)
    fls <- append(fls, urls[isgz])
    dirs <- urls[!isgz]
}

然后我们可以再次使用getURL,但这一次在fls(或fls的元素,在循环中)来检索实际文件。或者最好打开一个url连接并使用gzcon对文件进行解压缩和处理。

con <- gzcon(url(fls[1], "r"))
meta <- readLines(con, 7)
data <- scan(con, integer())

答案 1 :(得分:5)

如果我使用internet2选项启动R,我可以读取ftp页面的内容。即

C:\Program Files\R\R-2.12\bin\x64\Rgui.exe --internet2

(可以修改在Windows上启动R的快捷方式以添加internet2参数 - 右键单击​​/ Properties / Target,或者只在命令行运行它 - 在GNU / Linux上显而易见)。

该页面上的文字可以这样读:

 download.file("ftp://prism.oregonstate.edu//pub/prism/pacisl/grids", "f.txt")
 txt <- readLines("f.txt")

解析目录列表需要做更多工作,然后递归读取底层文件。

## (something like)
dirlines <- txt[grep("Directory <A HREF=", txt)]

## split and extract text after "grids/"
split1 <- sapply(strsplit(dirlines, "grids/"), function(x) rev(x)[1])

## split and extract remaining text after "/"
sapply(strsplit(split1, "/"), function(x) x[1])
[1] "dem"    "ppt"    "tdmean" "tmax"   "tmin"  

这就是在这里停止看似非常有吸引力,并且有点费力,所以我实际上会推荐一个不同的选择。毫无疑问,RCurl可能是更好的解决方案,我建议您和您的用户学习使用和ftp客户端。命令行ftp,匿名登录和mget都非常容易。

此处针对类似的ftp网站解释了internet2选项:

https://stat.ethz.ch/pipermail/r-help/2009-January/184647.html

答案 2 :(得分:3)

ftp.root <- where are the files
dropbox.root <- where to put the files

#=====================================================================
#   Function that downloads files from URL
#=====================================================================

fdownload <- function(sourcelink) { 

  targetlink <- paste(dropbox.root, substr(sourcelink, nchar(ftp.root)+1, 
nchar(sourcelink)), sep = '')

  # list of contents
  filenames <- getURL(sourcelink, ftp.use.epsv = FALSE, dirlistonly = TRUE)
  filenames <- strsplit(filenames, "\n")
  filenames <- unlist(filenames)

  files <- filenames[grep('\\.', filenames)]  
  dirs <- setdiff(filenames, files)
  if (length(dirs) != 0) {
    dirs <- paste(sourcelink, dirs, '/', sep = '')
  }  

  # files
  for (filename in files) {

    sourcefile <- paste(sourcelink, filename, sep = '')
    targetfile <- paste(targetlink, filename, sep = '')

    download.file(sourcefile, targetfile)
  }

  # subfolders
  for (dirname in dirs) {

    fdownload(dirname)
  }
}