Using the zip and zipcloak commands on Linux

Both the Linux zip and zipcloak commands can create encrypted zip files, but they have some important and interesting differences. Here’s what you need to know about how they work and what you should understand when using them.

zip

The zip command provides an easy way to take a group of files and squeeze their content into a single smaller file. To join a group of files into a single file—often done to make copying them to other systems considerably easier—use a command like the one shown below. The first argument is the name to be used for the zip file and is followed by the list of files to be included.

$ zip files.zip file1 file2
 adding: file1 (deflated 58%)
 adding: file2 (deflated 60%)

The command below provides a simple example in which files are joined in an encrypted zip file. Notice that the -e option that specifies that the resulting zip file is to be encrypted. The command will fail if you don’t enter the same password at each prompt.

$ zip -e files.zip file1 file2
Enter password:
Verify password:
  adding: file1 (deflated 58%)
  adding: file2 (deflated 60%)

The zip command not only stores the files together, but reduces the size of the files in the process. The zip file in these examples is less than half the size of the two files combined. Notice the “deflated” lines in the output shown above.

-rw-r—r—. 1 shs shs 2224 Nov 19 14:18 file1
-rw-r—r—. 1 shs shs 6712 Nov 19 14:18 file2
-rw-r—r—. 1 shs shs 3993 Nov 19 14:19 files.zip

The original files, after being added to the encrypted zip file, can be deleted afterwards, but you will need to preserve the password used so that you can extract the contents later.

You can also use the zip command to recursively collect files and include them in a single zip file. Here’s an example command that would put all of the files in your bin directory into a file named “bin.zip”.

$ zip -r bin.zip ~/bin

While the zip command can create encrypted files, it cannot encrypt existing zip files. For that, you can use the zipcloak command or go through the process of extracting the zip file’s contents and then re-zipping them using the -e option to put them back together in encrypted form.

NOTE: Zip files are smaller than the collection of separate files included in them whether or not they are encrypted.

zipcloak

The zipcloak command is different from zip in one very important way: It will encrypt or decrypt an existing zip file, but it will not create a zip file on its own. To use the zipcloak command to encrypt a zip file, use a command like the one shown below.

$ zipcloak files.zip
Enter password:
Verify password:
encrypting: file1
encrypting: file2
$ ls -l files.zip -rw-r—r—. 1 shs shs 3961 Nov 19 14:30 files.zip

After the encryption, the resulting file is nearly identical in size to the one created without encryption. In this case, it’s just a bit smaller.

unzip

The unzip command will extract the contents of zip files whether they are encrypted or not. It will, however, ask for the password for encrypted files.

$ unzip efiles.zip
Archive:  efiles.zip
[efiles.zip] file1 password:
  inflating: file1
  inflating: file2

If files with the same names exist in the directory, you will be asked whether you want to overwrite them. In addition, you can select individual files from a zip file if you don’t want to extract all of them. Here is an example of both extracting a single file and electing to overwrite the current file:

$ unzip efiles.zip file2
Archive:  efiles.zip
[efiles.zip] file2 password:
replace file2? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: file2

Compatibility

The zip and zipcloak commands are properly compatible. The unzip command can extract content from unencrypted and encrypted zip files and works as expected whether the files were encrypted with a zip -e (encrypt) command or a zipcloak command. Similarly, the zipcloak -d (decrypt) command will decrypt an encrypted zip file whether it was encrypted with a zip -e command or a zipcloak command.

Wrap-Up

Zip files are generally used to locally back up groups of files that might be undergoing changes or to copy files to other systems one group at a time. The zip and unzip commands allow you to easily create or extract content from zip files. The zipcloak command allows you to encrypt zip files or extract from encrypted zip files.

Copyright © 2022 IDG Communications, Inc.

Leave a Comment