How to compress and decompress (extract) files and folders using Zip

Zip is a file compression and archiving program that allows multiple files and folders to packaged in a single file.

Features

  • Password protection (encryption).
  • Multiple compression algorithms available.
  • Add, delete and extract specific files and folders without compiling or decompiling the entire archive.

Installation

Your server probably already has Zip installed, but if it doesn't, installation is pretty straight-forward.

apt install zip

Archive/Compress

Single File

Syntax
zip [zipfile] [target]
  • [zipfile] -- The name of the zip file you wish to create.
  • [target] -- The files or directory you wish to archive/compress. Leave empty, if you wish to archive the current directory.
Example
zip backup.zip var/www/*

Multiple Files and Folders

Syntax
zip -r [zipfile] [target]
  • -r -- Recursive. (For compressesing/archiving all the subfolders and their files)
  • [zipfile] -- The name of the zip file you wish to create.
  • [target] -- The files or directory you wish to archive/compress. Leave empty, if you wish to archive the current directory.
Examples
Compress a Folder
zip -r backup.zip var/www/*
    • -- Remember: When dealing with filepaths, the asterisk ("*") is a wildcard meaning "anything". Using the wildcard selects anything within a folder--all its files and folders.
Compress All Files with a Particular Extension.
zip -r backup.zip var/www/*.txt
    • -- Remember: When dealing with filepaths, the asterisk ("*") is a wildcard meaning "anything". Using the wildcard followed by a dot (.) and extension (e.g. "txt") selects files with the requested extension.

Extract/Decompress

Syntax

unzip [zipfile] [target]
  • [zipfile] - The filename/filepath of the zip file you want to extract/decompress.
  • [target] -- The directory where the extracted files and folders should be stored. Leave empty, if you wish to use the current directory.

Example.

unzip backup.zip