List of content you will read in this article:
The most popular lossless data compression archive file format is Zip. A Zip file is a data object that encapsulates one or more compressed files and folders. Uncompressed files take up more disc space and take longer to transmit from one device to another. Zip files may be readily extracted on Microsoft Windows, macOS, and Linux operating systems using the appropriate software for each platform. In this guide, you'll learn how to use the zip command in Linux to compress files and directories.
Command for Zipping Files
Zip is a command-line tool that aids in the creation of Zip archives.
The syntax for the zip command is as follows:
zip Option Archive-name File
A user must have write rights on a directory in order to build a Zip archive in that directory.
Zip files do not allow ownership information in the Linux way. The person who executes the command owns the extracted files. Use the tar command to keep file ownership and permissions intact. Most Linux distributions do not include the zip program by default, but you may simply install it using your distribution's package management.
Zip may be installed on Ubuntu and Debian systems by the following command:
sudo apt install zip
Zip may be installed on CentOS and Fedora systems by following this command: CentOS and Fedora
sudo yum install zip
How to Compress Files and Directories in Zip Forma
To zip one or more files, divide the files to be included in the pack by a space, as demonstrated below:
zip xyz.zip file1 file2 file3
adding: file1 (deflated 53%)
adding: file2 (stored 0%)
adding: file3 (deflated 48%)
The zip command publishes the names of the files that have been added to the archive as well as the compression technique by default. Unless the file which has to be compressed contains a .zip extension, the extension is created automatically if the archive name does not conclude with it.
zip xyz.zip file1 will produce the same archive as zip xyz file1.
Use the -q option to suppress the output of the zip command:
zip -q xyz.zip file1 file2 file3
Creating a zip archive of a directory that includes the content of subdirectories is common.
You may recursively scan the full directory tree using the -r option:
zip -r xyz.zip dir1
In the same zip file, you can also add numerous files and directories:
zip -r xyz.zip dir1 dir2 filename1 filename2
Compression Methods and Levels
Deflate is Zip's default compression algorithm. If a file cannot be compressed, the zip utility just puts it in the file as it is, instead of compressing it with the store method. The bzip2 compression algorithm is supported by the zip program in most Linux variants.
Use the -Z option to select a compression method.
zip -r -Z bzip2 xyz.zip dir1
adding: sub_dir/ (stored 0%)
adding: sub_dir/filename1 (bzipped 27%)
adding: sub_dir/filename2 (bzipped 68%)
The zip command lets you define a compression level from 0 to 9 by providing a number preceded with a dash. The compression level is set to -6 by default. When -0 is used, all files are saved without compression. -9 forces the zip command to perform the best compression possible for all files.
For example, if you wanted to utilize compression level -9, you would type:
zip -9 -r xyz.zip dir1
The zip operation becomes more CPU-intensive when the compression level is increased, and it takes longer to complete.
Creating a Password-Protected ZIP file
If you have sensitive data that has to be saved in the archive, use the -e option to encrypt it:
zip -e xyz.zip dir1
The archive password must be entered and verified using the following command:
The password you will be entering will not be seen on the screen. However, the next time you log in, make sure you remember your password.
Creating a Split Zip File
Assume you wish to upload the Zip archive to a file hosting provider with a 1GB upload restriction, and your Zip archive is 5GB in size.
To create a new split Zip file, use the -s option followed by size. k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes) can be used as the multiplier.
zip -s 500m -r xyz.zip dir
After a set hits the stated size limit, the operation above will keep producing new archives in it.
Few More Zip Commands
Create a xyz.zip file that contains all of the files in the current directory.
zip xyz *
You may use the following command to include all the hidden files:
zip xyz .* *
Make a xyz.zip file that contains all MP4 files in the current directory, compressed.
zip -0 xyz *.mp4
The zip command in Linux may be used to build Zip archives. On a Linux system, the unzip command may also be used to decompress a ZIP file. We have seen how to zip various files and directories in this guide and various command options related to the zip command in Linux.