【问题标题】:unzip a .zip file解压缩 .zip 文件
【发布时间】:2015-10-18 22:07:16
【问题描述】:

我想在 R 中解压缩一个文件。我完全不知道该怎么做。

我搜索并找到了这样的方法:

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

但我不知道该怎么办。

【问题讨论】:

标签: r


【解决方案1】:

你可以这样做:

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 

你也可以用经典的方式在 R 中定义两条路径:

假设您的 zip 文件名为 file.zip

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

exdir 定义将文件提取到的目录。如果尚不可用,它将被创建。 如果你不设置exdirunzip只会解压到你当前的工作目录。

【讨论】:

  • 你可以在windows中使用正斜杠
  • 如何设置解压方式,使得解压后的同名文件会被重命名。使用 WinZip 相当于“重命名所有”。
【解决方案2】:

我做了这个

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
}

答案是:exdir不存在,它存在,我创建的

【讨论】:

    【解决方案3】:

    要解压缩许多文件,您还可以这样做:

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

    其中../不输入绝对路径就上移一个目录。

    解压文件夹会自动创建。

    【讨论】: