Creating or opening a password protected zip file on FreeBSD

So this works on FreeBSD but it probably works on OS X, most Linuxes, Solaris, and other Unixes as well.

Installing prerequisites

In order to create a password protected zip file and later open it, two ports are needed:

  • zip
  • unzip

To install these using packages, do this as root:

#
#
pkg_add zip
pkg_add unzip

Or using ports:

#
#
#
#
cd /usr/ports/archivers/zip
make install
cd /usr/ports/archivers/unzip
make install

Creating a password protected zip file

There are a couple of different ways you may want to create a zip file. You may want to zip a sing file, or two or more files, or and entire directory and all its contents. You may also want to add a file to an existing zip file.

All of these actions can be done with a binary called zip.

Example 1 – Creating a password protected zip file containing one file

The syntax is simple. The -e parameter is to encrypt with a password. Always put the zip file first and the file to be zipped second.

# zip -e myarchive.zip myfile1

Example 2 – Adding a file to your password protected zip file

Since your zip file already is encrypted with a password and adding a file does not require decrypting, you don’t need the password to add a file to the zip file.

# zip myarchive.zip myfile2

Example 3 – Creating a password protected zip file containing multiple files

This is very similar to Example 1. The -e parameter is to encrypt with a password. Always put the zip file first and the files to be zipped last separated by a space.

# zip -e myarchive.zip myfile1 myfile2 myfile3

Example 4 – Creating a password protected zip file containing a directory and all is contents.

The -r parameter is to do a recursive zip (recursive means to include the folder and all its contents). The -e parameter is to encrypt with a password. Always put the zip file first and then the directory name.

# zip -r -e myarchive.zip mydirectory

Example 5 – Delete a file from the zip

This is easier than you think. Because you are not actually reading the contents of a file in the archive, the password is not needed to delete a file inside the zip file.

# zip -d myarchive.zip myfile1

Opening a password protected zip file

The syntax for unzipping a file is a lot easier.  It uses a different binary file called unzip.

# unzip myarchive.zip

The above prompts for the password automatically and unzips the files, assuming the correct password is provided.

One Comment

  1. Francisco says:

    I am not sure if this has changed, but historically the encryption in the "zip" program is fairly weak.
    The zip format itself has support for strong encryption.
    I found this reference that may be of interest: http://www.ask.com/wiki/ZIP_(file_format)#Encryption

Leave a Reply

How to post code in comments?