R:从 2 个 zip 文件夹中读取 csv

问题描述

我在一些不幸的情况下工作,需要从 2 个 zip 文件夹中读取 csv 文件。我的意思是文件路径看起来像这样:

//path/folder1.zip/folder2.zip/wanttoread.csv

我尝试模仿这里发现的这个问题的巧妙工作: Extract certain files from .zip ,但到目前为止还没有运气。具体来说,当我运行类似的东西时,我收到一条错误消息

Error in fread(x,sep = ",",header = TRUE,stringsAsFactors = FALSE) : 
embedded nul in string:

接着是一堆编码的废话。

关于如何处理这个问题的任何想法?提前致谢!

解决方法

这是一种使用 tempdir() 的方法:

temp<-tempdir(check = TRUE) #Create temporary directory to extract into

unzip("folder1.zip",exdir = temp) #Unzip outer archive to temp directory

unzip(file.path(temp,"folder2.zip"),#Use file.path to generate the path to the inner archive
      exdir = file.path(temp,"temp2")) #Extract to a subfolder inside temp
                                       #This covers the case when the outer archive might also have a file named wanttoread.csv

list.files(file.path(temp,"temp2")) #We can see the .csv file is now there
#[1] "wanttoread.csv"

read.csv(file.path(temp,"temp2","wanttoread.csv")) #Read it in
#   Var1         Var2
#1 Hello obewanjacobi