问题描述
有没有办法有选择地从 .zip
存档中提取名称与模式匹配的文件?
Is there a way to selectively extract from a .zip
archive those files with names matching a pattern?
例如,如果我想使用存档中的所有 .csv
文件并忽略其他文件.
For example, if I want to use all .csv
files from the archive and ignore other files.
目前的方法:
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
),而是寻找一种方法来首先选择要提取的内容.
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.
我目前使用的是 v3.2.2 (Windows).
I'm currently on v3.2.2 (Windows).
推荐答案
感谢@user20650 的评论.
Thanks to comment from @user20650.
使用两次调用unzip
.首先使用 list=TRUE
来获取文件的 $Name
.其次使用 files=
仅提取名称与模式匹配的文件.
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 )
这篇关于从 .zip 中提取某些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!