Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

r - Extract certain files from .zip

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 files from the archive and ignore other files.

Current approach:

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 ) 

Instead of just selecting which ones to read (csv_nms), I'm looking for a way to choose which ones to extract in the first place.

I'm currently on v3.2.2 (Windows).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...