Creating Password-Protected ZIP Files on Linux and macOS

Creating a password-protected zip, and adding a password to an existing zip file

* This page contains promotional content

The other day, while self-hosting Bitwarden with Docker , I realised that leaving the data exported from LastPass and 1Password lying around as it is would be dangerous. Deleting it once it is no longer needed is probably the best answer, but I wanted to keep it as a backup, so I decided to save it as a password-protected zip.

Creating a password-protected zip

When you want to create a password-protected zip from a file

$ zip -e sample.zip sample.txt
Enter password:<enter the password>
Verify password:<enter the password>
adding: test (stored 0%)

When you want to create a password-protected zip from a directory

When a directory is involved you need to be careful, because if you forget the -r option you end up with an empty zip file

$ zip -e -r test.zip test
Enter password:
Verify password:
  adding: test/ (stored 0%)
  adding: test/test1.txt (stored 0%)
  adding: test/test2.txt (stored 0%)

Turning a zip file that has no password into a password-protected one

$ zipcloak sample.zip
Enter password:
Verify password:
encrypting: sample

The reverse is also possible: you can remove the password from a password-protected file

$ zipcloak -d <password-protected zip file>

If you find yourself saying “I don’t have zipcloak!”, install the zip package

## Debian/Ubuntu
$ sudo apt install zip

## Redhat/CentOS
$ sudo yum install zip

## MacOS
$ brew install zip

Creating a zip file for compatibility

If you want compatibility with Windows or Linux, a GUI application such as keka has a feature that removes the “.DS_Store” resource fork when it compresses to zip, so using it as well may be a good idea.

Finally

A zip password is not necessarily safe. Password cracking tools exist and it can be analysed easily, so as far as possible set a strong password made of random alphanumerics plus symbols!

See also