Extract certain files from .zip

前端 未结 1 1077
离开以前
离开以前 2021-01-05 14:02

Is there a way to selectively extract from a .zip archive those files with names matching a pattern?

For example, if I want to use all .csv

1条回答
  •  不知归路
    2021-01-05 14:35

    Thanks to comment from @user20650.

    Use two calls to unzip. First with list=TRUE just to get the $Name for the files. Second with files= to extract only the files whose names match the pattern.

      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 ) 
    

    0 讨论(0)
提交回复
热议问题