【问题标题】:Extract certain files from .zip从 .zip 中提取某些文件
【发布时间】:2017-02-25 05:21:31
【问题描述】:

有没有办法选择性地从 .zip 存档中提取名称与模式匹配的那些文件?

例如,如果我想使用存档中的所有 .csv 文件并忽略其他文件。

目前的做法:

zipped_file_names <- unzip('some_archive.zip') # extracts everything, captures file names
csv_nms <-  grep('csv', zipped_file_names, ignore.case=TRUE, value=TRUE)
library('data.table')
comb_tbl <- rbindlist(lapply(csv_nms,  function(x) cbind(fread(x, sep=',', header=TRUE, 
                                                               stringsAsFactors=FALSE), 
                                                         file_nm=x) ), fill=TRUE ) 

我不只是选择要阅读的内容 (csv_nms),而是寻找一种方法来选择首先提取哪些内容。

我目前使用的是 v3.2.2 (Windows)。

【问题讨论】:

  • 您可以使用unziplist=TRUE 参数来获取文件名列表,然后遍历要提取的文件名
  • 多哈。在仔细阅读?unzip 之后,我看到:list If TRUE, list the files and extract none. The equivalent of unzip -l. 谢谢。

标签: r unzip


【解决方案1】:

感谢@user20650 的评论。

两次调用unzip。首先使用list=TRUE 只是为了获取文件的$Name。其次使用files= 仅提取名称与模式匹配的文件。

  zipped_csv_names <- grep('\\.csv$', unzip('some_archive.zip', list=TRUE)$Name, 
                           ignore.case=TRUE, value=TRUE)
  unzip('some_archive.zip', files=zipped_csv_names)
  comb_tbl <- rbindlist(lapply(zipped_csv_names,  
                               function(x) cbind(fread(x, sep=',', header=TRUE,
                                                       stringsAsFactors=FALSE),
                                                 file_nm=x)), fill=TRUE ) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    相关资源
    最近更新 更多