What is an OS-agnostic solution to use fread
on a zip? I can't seem to find one.
Setting up the stage
Let's create two dataframes, write them to disk, and put them in a zip archive (I stole this from: How to zip multiple CSV files in R?)
library(zip)
df1 <- head(mtcars)
df2 <- head(iris)
write.csv(df1, 'df1.csv')
write.csv(df2, 'df2.csv')
zip(zipfile='df.zip', files=list.files(path = getwd(), pattern = ".csv$"))
Now I want to read this zip into R
Let's say I want to read df1.csv from the zip.
fread('df.zip/df1.csv')
Error in fread("df.zip/df1.csv") : File 'df.zip/df1.csv' does not exist or is non-readable
I tried this from fread() of file from archive
fread('unzip -p df.zip/df1.csv')
Null data.table (0 rows and 0 cols)
Warning message:
In fread("unzip -p df.zip/df1.csv") : File '/var/folders/w5/kqy78qb17v176195dtyyc4pj40000gn/T//RtmpIlNSk8/filee1693cc7f89' has size 0. Returning a NULL data.table.
I do not understand what it is trying to import, but clearly not my dataframe of interest.
Can you help?
Edit 1
Unzipping first is not really an option. In practice, I am working with batches of highly compressible files. Usually ~ 3000 xls files, each 1M rows. 100 Gb unzipped / 8 Gb zipped. Needless to say it would be much more comfortable to read directly from the zip!