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")
password ← "your password"
I found this question very useful but saw that no formal answers were posted, so here goes:
7z
command was recognized from the command line.system("7z x secure.7z -pPASSWORD")
with the appropriatePASSWORD
.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:
which when sourced will prompt me to enter the password, and the zip files will be decompressed in a loop.
password <- "your password"
system(command = paste0("unzip -o -P ", password, " ", "yourfile.zip"), wait = TRUE )
I found @Kim 's answer worked for me eventually but not first off. I thought I'd just add a few extra links/steps that helped me get there in the end.
Close and reopen R so that environment path is recognised
If you've already opened R when you do steps 1-3 you need to close and reload R for R to recognise the environment path for 7z. @wush978 's answer to this question r system doesn't work when trying 7zip was informative. I used Sys.getenv("PATH") to check that 7zip was included in the environment paths.
Step 4. I opened R and typed in system("7z x secure.7z -pPASSWORD") with the appropriate PASSWORD.
I actually found this didn't work so I modified it slightly following the instructions in this post which also explains how to specify an output directory https://stackoverflow.com/a/16098709/13678913.
If you have already extracted the files the system command prompts you to choose whether you want to replace the existing file with the file from the archive and provides options (Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit?
So the modified step 4 (Y allows replacement of files)
Putting this altogether as a modified set of instructions
Sys.getenv("PATH")
to check path to 7zip recognised in the environment (as per @wush978 's answer to question r system doesn't work when trying 7zip)system("7z e -oC:/My Documents/output_dir secure.zip -pPASSWORD")
with the appropriate PASSWORD (as per instructions here https://stackoverflow.com/a/16098709/13678913)And here is a modified version of @Kim 's neat function (including specified output directory and check for existing files):
My main script
Code inside source file unzip7z.R