Zip and Unzip are popularly used to compress or decompress a file or folder in zip format. We can use Zip to recursively compress all the files within a directory and use Unzip to decompress the files back to the original structure. This tutorial provides the steps required to install Zip/Unzip on the popular Linux distribution Ubuntu. It provides all the steps required to install and use Zip/Unzip on Ubuntu 18.04 LTS. The steps should be similar for other Linux systems and Ubuntu versions.
Install Zip Unzip
Use the below-mentioned command to install both zip and unzip utilities on Ubuntu.
# Install Zip and Unzip sudo apt install zip unzip
# Confirm installation zip --version
# Output Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license. This is Zip 3.0 (July 5th 2008), by Info-ZIP. Currently maintained by E. Gordon. Please send bug reports to the authors using the web page at www.info-zip.org; see README for details.
Latest sources and executables are at ftp://ftp.info-zip.org/pub/infozip, as of above date; see http://www.info-zip.org/ for other sites. .... ....
This is the easiest step to install both
Use Zip
We can use the zip command to compress archive files. Zip is popularly used as compression and file packaging utility on Ubuntu and Linux systems.
Compress File
We can compress a file using zip as shown below.
# Compress file using zip - Syntax zip <destination file> <source file>
# Compress files using zip - Syntax zip <destination file> <source file 1> <source file 1> <source file 1> ...
# Example zip target.zip source.txt
# Verify ls -l target.zip
# Output -rw-r--r-- 1 bravo bravo 200 Oct 2 18:53 target.zip
We can compress a single file using the zip utility by providing the path of the source file and the destination file.
Compress Folder
We can recursively compress a folder and all its files using zip as shown below.
# Compress a file using zip - Syntax zip -r <destination file> <source folder 1> <source folder 2> ...
# Example - Compress single directory cd /data/repositories zip -r repository1.zip repository1
# Output adding: repository1/ (stored 0%) adding: repository1/test1.txt (stored 0%)
# Example - Compress multiple directories cd /data/repositories zip -r repositories-1-4.zip repository1 repository2 repository3 repository4
# Output adding: repository1/ (stored 0%) adding: repository1/test1.txt (stored 0%) adding: repository2/ (stored 0%) adding: repository2/test2.txt (stored 0%) adding: repository3/ (stored 0%) adding: repository3/test3.txt (stored 0%) adding: repository4/ (stored 0%)
adding: repository4/test4.txt (stored 0%)
# Verify ls -l repository1.zip
ls -l repositories-1-4.zip
The above commands create the compressed archive using the zip utility either for a single directory or multiple directories. If we forget to add the option -r, the utility will compress only the directory without any file. You must specify the option -r to recursively compress the directories to generate the archive.
Compress with encryption
We can also generate the password-protected archives using the zip utility for both file and directory. It can be done using the command as shown below.
# Example - File zip -e target.zip source.txt
# It will ask for password Enter password: Verify password: adding: source.txt (stored 0%)
# Example - Folder zip -r -e repository2.zip repository2
# It will ask for password Enter password: Verify password: adding: repository2/ (stored 0%) adding: repository2/test.txt (stored 0%)
This is how we can use zip to compress and file and folder using zip.
Use Unzip
We can use the unzip command to decompress archive files.
# Unzip - Syntax - Same directory unzip <archive>
# Unzip - Syntax - Destination directory unzip <archive> -d <destination>
# Example - Same directory unzip target.zip
# Example - Destination directory unzip target.zip source
Similarly, we can unzip the encrypted file as shown below.
# Unzip unzip repository2.zip
# Output Archive: repository2.zip creating: repository2/ [repository2.zip] repository2/test.txt password: extracting: repository2/test.txt
Summary
This is how we can use the zip and unzip utilities to compress and decompress the files and folders. We also saw how to use these utilities to compress and decompress encrypted files.