Read Zipped Files into R

Master R

By Guangming Lang Comment

In the age of big data, it’s not uncommon to encounter a large zipped file of multiple text files. Unzipping will take time. It turns out we can read them into R without unzipping first.

# get the name of the zipped file
fname_zipped = "xxxxx.zip"

# list all files names inside of a .zip file
fnames = as.character(unzip(fname_zipped, list = TRUE)$Name)

# read every file into R, assuming they are .csv files
lst = vector("list", length(fnames))
for (i in seq_along(fnames))
        lst[[i]] = read.csv(unz(fname_zipped, fnames[i]), stringsAsFactors = F)

# check
str(lst[[1]])

If you enjoyed this post, get updates. It's FREE