"ZIP " is a format for archiving files and lossless data compression. ZIP archive is used in Windows because the built-in archiver creates archives in the .zip format, can contain one or more files and directories, which can be compressed using different algorithms

ZIP installation:

In Ubuntu, Debian:

apt-get install zip unzip  

In CentOS:

yum install zip unzip  

How to zip a ZIP archive in Linux

zip -9r archive.zip archive  
  • zip - archiver command, does not change

  • -9r - keys. They define the rules by which the archiver will work. Keys are specified with a hyphen at the beginning. You can specify the keys as a group, you can each individually, for example -9-r

Keys can be:

  • 9 - compression ratio. Can vary from 1 to 9, from lower to higher, respectively. If you specify 0, there will be no compression

  • r - make compression recursive. This means that if there are subdirectories in the directory to be compressed, they will be included in the archive too

  • x - exclude some files or directories. Specified separately at the very end of the command, for example:

zip -9r archive.zip archive -x "archive/noarchive/*"  

The path can be either relative or absolute. An example of an absolute path:

zip -9r archive.zip /var/www/example.com/archive/* -x "/var/www/example.com/archive/noarchive/*"  
  • --password - set password for the archive. For example:
zip -9r -password pass archive.zip archive  

Now, the password pass is set on the archive. To see the contents of the archive or to decompress it, you will be asked to enter the archive password.

  • archive.zip - the name of the archive. It must end in .zip

  • archive - the folder (directory, directory) you are compressing. The path relative to the current directory is specified here. You can specify absolute, say, */var/log/archive/ **

Also, you can specify multiple source paths in order to pack them into one archive, for example:

zip -9r archive.zip archive archive2 /var/www/example.com/archive/*  

How to unzip a zip archive in Linux

To unzip the .zip archive into the current directory, use the command
unzip archive.zip

You can also explicitly specify the path to unzip:

Relative path example:

unzip archive.zip -d archive  

An example with absolute path:

unzip archive.zip -d /var/www/example.com/archive/  

To unzip only part of the archive, you can use the command:

unzip archive.zip index.php  

This command unzips the index.php file into the current directory.

How to see the contents of a ZIP archive in Linux

To see the contents of a .zip archive without unpacking it, you can use the -l switch

unzip -l archive.zip  

*We recommend that you use Tar instead of ZIP, because the .zip format is very weak and susceptible to hacking, so try not to encrypt important information with it

Updated Oct. 26, 2024