Password protect a compressed (zipped) file on a Mac

You can do it on your mac without need for 3rd party Apps or Software. I’m currently running macOS High Sierra Version 10.13.6

Objective: We are not assigning password protection to an already compressed (zipped) file. We are creating a new compressed (zipped) file with password protection.

Here’s how to do it

Mac Terminal Icon

You need to open your “Terminal” application found in…
Applications > Utilities

When opened it should look something like this and it’s ready to accept your keyboard input

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$









Compressing a file or a folder of files?

You need to decide if you are compressing a file or a folder of files?
To avoid any mistyping issues I suggest that you move your file or folder of files you want compressed and password protected to your desktop.
Later I’ll show you how to access you file/s without having to move them on to your desktop first.

TO COMPRESS & PASSWORD PROTECT ONE FILE ON YOUR DESKTOP…

NOTE: THERE MUST BE A SPACE BETWEEN EACH NEW COMMAND AND THERE MUST BE NO SPACES IN YOUR FILE NAMES.

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$zip -e ~/Desktop/TheNameYouWantForYourNewZip.zip ~/Desktop/YourFile.txt









PRESS “RETURN”

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$zip -e ~/Desktop/TheNameYouWantForYourNewZip.zip ~/Desktop/YourFile.txt
Enter password:



ENTER YOUR PASSWORD
NOTE: TERMINAL DOES NOT DISPLAY THE TYPING OF PASSWORDS

PRESS “RETURN”

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$zip -e ~/Desktop/TheNameYouWantForYourNewZip.zip ~/Desktop/YourFile.txt
Enter password:
Verify password:




ENTER YOUR PASSWORD AGAIN TO VERIFY IT
NOTE: TERMINAL DOES NOT DISPLAY THE TYPING OF PASSWORDS

PRESS “RETURN”

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$zip -e ~/Desktop/TheNameYouWantForYourNewZip.zip ~/Desktop/YourFile.txt
Enter password:
Verify password:
adding: Users/macgenius/Desktop/YourFile.txt (deflated 3%)
Genius-iMac:~ macgenius$




JOB DONE!
The zip file "TheNameYouWantForYourNewZip.zip" was created on your desktop with the password protection you assigned to it.

So you want to compress and password protect a whole FOLDER (directory) of files?

It’s easy now…
Instead of typing the command “zip -e” type “zip -er
remembering to point the new command to an actual folder (directory) of files. If the new command doesn’t see a directory of files it won’t work.

So far…

zip = the command to compress (zip)
-e = the command to encrypt (password protection)
~ = start from the current users home directory
-er = encrypt recursively through a whole directory of files


EXPERT LEVEL - Mac zip and password protect


So far we’ve put the folders and files on the desktop to avoid any mispelling of long file paths (making our life a little easier whilst learning).
Terminal recognises a space as the start of a new command. Unfortunately our directory path to folders and files may very likely contain spaces. To negotiate this we can escape each unwanted space with a backslash \ .

So now our command to compress a WHOLE DIRECTORY OF FILES located within folders might look something like this…

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$zip -er ~/Desktop/TheNameYouWantForYourNewZip.zip ~/Documents/Example\ Directory/Folder\ with\ files




TOP TIP

Nobody likes typing!

Terminal has a neat trick. Type in your commands to create a zip stopping before you get to the target’s directory path then drag & drop the folder you want zipping into the Terminal window. VoilĂ  the file path appears with all spaces escaped.

MAC Terminal creates a directory path in your compressed (zipped) file

For the most inquisitive amongst you you’d have noticed Terminal creates the directory paths in your zipped (compressed) file. When you uncompress this file you will see extra files created that contain your compressed files directory paths.

Most people would want to open a compressed .zip file and see only the contents they’re expecting and not be confused by extra files that are irreleveant to them.

NOTE: Terminal does contain a command to remove directory paths -j (junk directory paths) in a .zip file. However there are many problems in trying to use this. It will remove ALL directory paths. Even those you may want in your zipped directory. It also doesn’t work if a file name is duplicated within the hierachy of directories you are trying to zip. So, for this tutorial we’re going to ignore this command.

-j = junk the paths
We’re ignoring this command for this tutorial because in this instance it can create more problems than it solves.

Get rid of directory paths in Mac Terminal’s compressed .zip files

To do this we are going to instruct Terminal to begin from the directory we choose rather than the default “User’ starting point

Currently our default starting point is “Genius-iMac:~ macgenius$” [Computer name] [Current User]

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$




Type the command “cd” space (now drag the directory you want to start from into the terminal window) press return.

cd = change directory

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$ cd /Users/macgenius/Documents/Example\ Directory/Folder\ with\ files
Genius-iMac:Folder with files macgenius$




Now each time we press return our starting point is “Folder with files” until such time we issue the command “cd”space (change directory) again or quit out of Terminal and start again at it’s defaults.

Now we’re working from the file that we actually want to compress so there are no file paths to it. It is our starting point.

With that in mind we add the following commands…

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$ cd /Users/macgenius/Documents/Example\ Directory/Folder\ with\ files
Genius-iMac:Folder with files macgenius$ zip -er ~/Desktop/TheNameYouWantForYourNewZip.zip ./




PRESS RETURN - JOB Completes
The zip file “TheNameYouWantForYourNewZip.zip” was created on your desktop without any extra user path files contained within it.

So far…

zip = the command to compress (zip)
-e = the command to encrypt (password protection)
~ = start from the current users home directory
-er = encrypt recursively through a whole directory of files
cd = change directory
./ = path to the current directory

Can you save your new zip file to the folder you are compressing?

YES!

If like me and you probably are if you’re a coder you like things tidy and to make sense. Therefore saving your zip file to your desktop only to file it later is not so clever when you can file it instantly with these commands. In my opinion saving your zipped file in the folder you’re compressing would make it easy to find at a later date. A clean desktop is a clean mind

The following not only saves the zipped file into the directory you’re compressing it’s also set to name the file with the same name of the directory that’s being compressed suffixing it with .zip

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$ cd /Users/macgenius/Documents/Example\ Directory/Folder\ with\ files
Genius-iMac:Folder with files macgenius$ zip -er ./Folder\ with\ files.zip ./




JOB DONE!
The zip file “Folder with files.zip” was created inside the directory you specified as the starting point for Terminal (Folder with files).

IMPORTANT NOTE!
Testing your newly created .zip file you might want to test it on a PC. At first you will be horrified to see that a PC opens your .zip file and displays all of the contents without asking for a password! WTF!
However remain calm and try to open one of the files displayed. Now, it presents you with a password dialogue box and it does this to each and every file within the .zip. Each file is individually password protected. PC users will know that they can specify to unzip ALL files within a zipped package or just target the one they wish to open by double clicking on it. This means they can open all the files within the .zipped package only having to enter the password once (the same way the mac handles them).

 

 

 

 

Additional mac Terminal info…

You can get help on different commands (utilities) within mac terminal by typing the command “-help” so “zip -help” and press return reveals the commands and a brief description of their functions

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$zip -help
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
The default action is to add or replace zipfile entries from list, which
can include the special name - to compress standard input.
If zipfile and list are omitted, zip compresses stdin to stdout.

-f freshen: only changed files -u  update: only changed or new files
-d delete entries in zipfile -m move into zipfile (delete OS files)
-r recurse into directories -j junk (don't record) directory names
-0 store only -l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster -9 compress better
-q quiet operation -v verbose operation/print version info
-c add one-line comments -z add zipfile comment
-@ read names from stdin -o make zipfile as old as latest entry
-x exclude the following names -i include only the following names
-F fix zipfile (-FF try harder) -D do not add directory entries
-A adjust self-extracting exe -j junk zipfile prefix (unzipsfx)
-T test zipfile integrity -X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt -n don't compress these suffixes
-h2 show more help    
Genius-iMac:~ macgenius$

To display the current full list of commands (sometimes referred to as utilities)

Open a fresh terminal window. At your prompt hold down your "escape" key for a couple of seconds.

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$









Then type “y”. Terminal will reveal the commands available. You might need to keep pressing “Return” until the full list is revealed.

Mac Terminal Instructions

Last login: Sun Oct 07 21:39:26 on console
You have mail.
Genius-iMac:~ macgenius$
Display all 1379 possibilities? (y or n)








Useful links

Getting to know the command line

 

 

Please share if you find the content useful - thank you

Master Template World

Contact

Master Template Forum Robot

FORUM

Have a comment or something to say?
Say it here…

[privacy policy]