Skip to content

Instantly share code, notes, and snippets.

@hadley
Created February 10, 2017 16:15
Show Gist options
  • Save hadley/dfb870bf320ff083f62c833cbae15f07 to your computer and use it in GitHub Desktop.
Save hadley/dfb870bf320ff083f62c833cbae15f07 to your computer and use it in GitHub Desktop.
path <- "~/Documents/ingest/readxl/inst/extdata/datasets.xlsx"
zip_file <- function(path) {
stopifnot(file.exists(path))
path <- normalizePath(path)
contents <- utils::unzip(path, list = TRUE)
names(contents) <- c("path", "size", "date")
contents <- contents[contents$size > 0, , drop = FALSE]
contents <- tibble::remove_rownames(tibble::as_tibble(contents))
structure(
contents,
class = c("zip_file", "tbl_df", "tbl", "data.frame"),
path = path
)
}
zip_con <- function(zip_file, path = 1L, open = "rb", ...) {
if (is.numeric(path) && length(path) == 1) {
path <- zip_file$path[[path]]
}
if (!is.character(path) || length(path) != 1) {
stop("`path` must be a length one character vector or numeric", call. = FALSE)
}
if (!path %in% zip_file$path) {
stop("`path` ", encodeString(path, quote = "'"), " not found", call. = FALSE)
}
unz(attr(zip_file, "path"), path, open = open, ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment