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
1.0k views
in Technique[技术] by (71.8m points)

windows - Unzip password protected zip files in R

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")
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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 Files7-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.


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

...