38

I want to unzip a file in R. I completely don't know what to do.

I searched and I found it method like this :

unzip(zipfile, files = NULL, list = FALSE, overwrite = TRUE,
      junkpaths = FALSE, exdir = ".", unzip = "internal",
      setTimes = FALSE)

but I don't know what should I do with this.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
saleh sereshki
  • 2,402
  • 3
  • 17
  • 18

3 Answers3

48

You could do it like this:

zipF<-file.choose() # lets you choose a file and save its file path in R (at least for windows)
outDir<-"C:\\Users\\Name\\Documents\\unzipfolder" # Define the folder where the zip file should be unzipped to 
unzip(zipF,exdir=outDir)  # unzip your file 

Well you could also define both paths in R the classical way:

Assuming your zip file is named file.zip

zipF<- "C:\\path\\to\\my\\zipfile\\file.zip"
outDir<-"C:\\Users\\Name\\Documents\\unzipfolder"
unzip(zipF,exdir=outDir)

exdir defines the directory to extract files to. It will be created if not already available. If you don't set exdir, unzip will just unzip it to your current working directory.

Deset
  • 877
  • 13
  • 19
2

To unzip many files you can do also:

files <- list.files(path="../Output/datasets/", pattern=".zip$")
outDir <- "../Output/datasets/unzip"
for (i in files) {
  unzip(paste0("../Output/datasets/",i), exdir=outDir)
}

Where the ../ move up one directory without entering the absolute path.

And the unzip folder is created automatically.

1

I did this

dataFrom="E:/test/"
folderTo="E:/test2/"
s=list.files(dataFrom)
j=1`enter code here`
while(j<=length(s))
{
  unzip(paste(dataFrom,s[j], sep=""),exdir=folderTo)
  j=j+1
}

Answer is: exdir doesn't exist, and it exists, I created

RamPrakash
  • 1,687
  • 3
  • 20
  • 25