Skip to content

Instantly share code, notes, and snippets.

@dholstius
Last active Aug 29, 2015
Embed
What would you like to do?
Download and extract a zipped file (that may not have .zip at the end of the URL)
unzip_url <- function (
url,
exdir,
zipfile = basename(url),
keep_zipfile = TRUE,
quiet = FALSE
) {
require(httr)
if (missing(exdir)) {
# FIXME: platform-dependent
exdir <- with(parse_url(url), file.path(hostname, str_replace(path, ignore.case("\\.zip$"), "")))
}
zipfile <- file.path(exdir, zipfile)
if (!file.exists(zipfile)) {
if (!file.exists(exdir)) {
dir.create(exdir, recursive = TRUE)
}
if (!quiet) message("Downloading to ", zipfile)
download.file(url, zipfile)
}
if (!quiet) message("Unzipping into ", exdir)
contents <- unzip(zipfile, exdir = exdir)
if (!keep_zipfile) file.remove(zipfile)
return(invisible(exdir))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment