Unzip password protected zip files in R

痞子三分冷 提交于 2019-12-29 07:06:29

问题


A password cannot be specified in unzip (utils) function. The other function I am aware of, getZip (Hmisc), only works for zip files containing one compressed file.

I would like to do something like this to unzip all the files in foo.zip in Windows 8:

unzip("foo.zip", password = "mypass")

回答1:


I found this question very useful but saw that no formal answers were posted, so here goes:

  1. First I installed 7z.
  2. Then I added "C:\Program Files\7-Zip\" to my environment path.
  3. I tested that the 7z command was recognized from the command line.
  4. I opened R and typed in system("7z x secure.7z -pPASSWORD") with the appropriate PASSWORD.

I have multiple zipped files and I'd rather not the password show in the source code or be stored in any text file, so I wrote the following script:

file_list <- list.files(path = ".", pattern = ".7z", all.files = T)
pw = readline(prompt = "Enter the password: ")
for (file in file_list) {
  sys_command = paste0("7z ", "x ", file, " -p", pw)
  system(sys_command)
}

which when sourced will prompt me to enter the password, and the zip files will be decompressed in a loop.




回答2:


password <- "your password"

system(command = paste0("unzip -o -P ", password, " ", "yourfile.zip"), wait = TRUE )




回答3:


password ← "your password"

read.table(text=system(paste0("unzip -p -P ", password, " yourfile.zip ", "yourfile.csv"),intern = "TRUE"),stringsAsFactors=FALSE,header=TRUE,sep=",")


来源:https://stackoverflow.com/questions/37665451/unzip-password-protected-zip-files-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!