ANieder
ANieder

Reputation: 223

Creating encrypted compressed files from R

Ive been searching for a solution to my answer in google and stackoveflow, but had no luck, I hope I am not asking something obvious.

Is it possible to create encrypted (with password) zip files form R? Say I am creating a bunch of xlsx files in R and placing them in a common folder, I would like to grab those files and zip them using a password for enxcryption, so whoever opens the files would need to enter a predefined password. I would need to do this automatically from R, since I am creating many files and doing the encryption by hand is out of the question.

Thanks in advance for any help

Upvotes: 2

Views: 1094

Answers (1)

lukeA
lukeA

Reputation: 54277

Maybe you could use something like this:

zipPsw <- function(dir, fn=tempfile(fileext = ".zip"), psw, addFlags="") {
  stopifnot(Sys.which("zip")!="")
  zip(
    zipfile = fn, 
    files = path.expand(dir), 
    flags = paste0("-r --password ", psw, " ", addFlags)
  )
  return(fn)
}
zipPsw("~/Documents/mysubdir", psw="mypass", addFlags="-j")
# adding: foo.txt (152 bytes security) (stored 0%)
# adding: bar.txt (152 bytes security) (deflated 63%)
# [1] "C:\\Windows\\TEMP\\RtmpysNeBv\\file67703eed1b9d.zip"

Upvotes: 2

Related Questions