R Reading in a zip data file without unzipping it

I have a very large zip file and i am trying to read it into R without unzipping it like so:

temp <- tempfile("Sales", fileext=c("zip"))
data <- read.table(unz(temp, "Sales.dat"), nrows=10, header=T, quote="\"", sep=",")


Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot open zip file 'C:\Users\xxx\AppData\Local\Temp\RtmpyAM9jH\Sales13041760345azip'
104500 次浏览

If your zip file is called Sales.zip and contains only a file called Sales.dat, I think you can simply do the following (assuming the file is in your working directory):

data <- read.table(unz("Sales.zip", "Sales.dat"), nrows=10, header=T, quote="\"", sep=",")

No need to use unz, as now read.table can handle the zipped file directly:

data <- read.table("Sales.zip", nrows=10, header=T, quote="\"", sep=",")

See this post

If you have zcat installed on your system (which is the case for linux, macos, and cygwin) you could also use:

zipfile<-"test.zip"
myData <- read.delim(pipe(paste("zcat", zipfile)))

This solution also has the advantage that no temporary files are created.

The methods of the readr package also support compressed files if the file suffix indicates the nature of the file, that is files ending in .gz, .bz2, .xz, or .zip will be automatically uncompressed.

require(readr)
myData <- read_csv("foo.txt.gz")

The gzfile function along with read_csv and read.table can read compressed files.

library(readr)
df = read_csv(gzfile("file.csv.gz"))


library(data.table)
df = read.table(gzfile("file.csv.gz"))

read_csv from the readr package can read compressed files even without using gzfile function.

library(readr)
df = read_csv("file.csv.gz")

read_csv is recommended because it is faster than read.table

In this expression you lost a dot

temp <- tempfile("Sales", fileext=c("zip"))

It should be:

temp <- tempfile("Sales", fileext=c(".zip"))

This should work just fine if the file is sales.csv.

data <- readr::read_csv(unzip("Sales.zip", "Sales.csv"))

To check the filename without extracting the file. This works

unzip("sales.zip", list = TRUE)

For remote-based zipped files

samhsa2015 <- fread("curl https://www.opr.princeton.edu/workshops/Downloads/2020Jan_LatentClassAnalysisPratt_samhsa_2015F.zip | funzip")

answer from here: https://stackoverflow.com/a/37824192/12387385)