ZIP files are a universal archive commonly used on Windows, MacOS and even Linux systems. You can create a zip archive or pick up files from one with some common Linux terminal commands.
ZIP Compressed Archive File Format
Thanks to the ZIP format of the Windows Empire, ZIP files are probably the most common form of compressed archives in the world.
While .tar.gz and tar.bz2 files are common on Linux, Windows users will probably send a ZIP file archive. And if you want to archive some files and send them to a Windows user, ZIP format is the easiest and most compatible solution for everyone.
RELATED: How to extract files from a .tar .gz or .tar.bz2 file on Linux
zip, unzip and other utilities
You may already know that Linux and Unix Similar operating systems like MacOS have tools that allow you to create ZIP files and extract files from them, called
unzip . But there are a whole family related tool such as
We checked some Linux distributions to see if they included these tools in the standard installation. All tools were present in Ubuntu 1
unzipbut none of the other tools and that was also the case with CentOS.
To install the missing elements on Fedora 29, use the following command:
sudo dnf install the perl-IO-Compress
To install the missing elements on CentOS 7, use the following command:
sudo yum install perl-IO- Compress
If any of the zip tools are missing from a Linux distribution not mentioned above, use the Linux distribution package management tool to install the required package.
How to create a zip file with the zip command
To create a ZIP file, you must tell the
zip name of the archive file and which files to include in it. You do not need to add the ".zip" extension to the archive name, but it does not harm you.
Creating a file named
source_code.zip containing all C source code files and main files in the current directory, you would use this command:
zip source_code * .c * .h  zip command in a terminal window " width="644" height="55" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>
Each file is listed as it has been added. The file name and the amount of compression achieved on that file are displayed.
If you look at the new ZIP archive, you can make sure the filename extension ".zip" has been added automatically by
zip.ls -l source_code.zip
If you do not want to see the output of
zipwhen the ZIP file is created, use
-q(silent) option.zip -q source_code * .c * .h
Including directories in ZIP files
To include subdirectories in the ZIP file, use the
-r(recursively) and Enter the name of the subdirectory of the command line. To create a ZIP file as before and also include the archive directory, use this command.zip -r -q source_code archive / * .c * .h
To pay attention to the person who will extract the files from the ZIP file you creating it is often polite to create ZIP files with the files inside it that are in a directory. When the person receiving the ZIP file extracts it, all files are neatly placed in a directory on their computer.
In the following command, we will archive the directory
andand all subdirectories. Note that this command is issued from the parent directory in the folder
Work.zip -r -q source_code work /
Set compression level
You can specify how much compression to apply to the files they are added to the ZIP archive . The range is from 0 to 9, where 0 is not at all any compression. The higher the compression, the longer the time required to create the ZIP file. For modest ZIP files, the time difference is not a significant penalty. But for the modest ZIP files, the default compression (level 6) is probably good enough.
zipto use a certain compression rate, send the number as a command line option, with a "-", like this:zip -0 -r -q source_code work /  0 compression options in a terminal window " width="644" height="130" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>
The default compression level is 6. Where [zip] -r -q source_code work /  level 6 compression in a terminal window " width="644" height="130" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>it is not necessary to give the
-6option, but it does not hurt if you do.
The maximum compression level is level 9.zip -9 -r -q source_code work /
With the choice of files and directories archived here, the difference between no compression (level 0) and the standard compression (level 6) is 400 k. The difference between the standard compression and the highest compression rate (level 9) is only 4K.
It may not seem much, but for archives containing hundreds or even thousands of files, the small extra file compression would add up to a valuable space saving.
Adding passwords to ZIP files
Adding passwords to ZIP files is easy. Use the
-e(encrypt) option and you will be prompted to enter your password and re-enter it for verification.zip -e -r -q source_code work /
How to extract a ZIP file with the Unzip command
To extract the files from a ZIP file, use the Unzip command and enter the ZIP file. Note that you need to provide the ".zip" extension.unzip source_code.zip
When the files are extracted, they are listed to the terminal window.
ZIP files do not have file ownership data. All the files that are extracted have the owner asked the user to extract them.
-q(silent), so you don't need to see the file list that the files are extracted.unzip -q source_code.zip
Extract files to a target directory
To get the files extracted in a specific directory, use the option
-d(directory) and give the path to the directory you want the archive to be extracted to.unzip -q source_code.zip -d ./development
Extract password protected ZIP files
If a ZIP file was created with a password,
unzipwill ask you for the password. If you do not enter the correct password,
unzipwill not extract the files.unzip -q source_code.zip
If you do not care about your password being viewed by others, or stored in your command line history. You can enter the password on the command line using the
-P(password) option. (You must use a capital "P".)unzip -P fifty.treacle.cutlass -q source_code.zip
If you want do not extract a particular file or group of files, use the option
-x(exclude). In this example, we want to extract all files except those ending in a ".h" extension.unzip -q source_code.zip -x * .h
Suppose you have deleted an archive, but you have removed some of the extracted the files by mistake.
A quick fix because it would be to extract the files again. But if you try to extract the ZIP file in the same directory as before,
unzipwill ask you for a decision to overwrite the files. It will expect one of the following answers.
r(change name) the answer is the main object.
- y: Yes, write this file
- n: No, do not write this file
- A: All, overwrite all files
- N: None, overwrite no files
- r: Rename, extract this file, but rename it. You will be asked to enter a new name.
unzip to overwrite all existing files, use
unzip -o -q source_code.zip
The most effective way to replace the missing files is to only
unzip extract all files in the archive that are not in destination directory. To do this, use the
-n option (never write).
unzip -n source_code.zip
Inside a ZIP file
It is often useful and instructive to see a list of the files in a ZIP file before extracting it. You can do this with the option
-l (list archive). It is moved by
less to make the output manageable.
unzip -l source_code.zip | less
Output shows directories and files in the ZIP file, their length and time and date they are added to the archive. Press "q" to exit from
There are other ways to peek inside a ZIP file that provides different types of information, as we should see.
Add a password with the zipcloak command
If you have created a ZIP file but forgot to add a password, what can you do? You can quickly add a ZIP file password using the
zipcloak command. Pass the name of the ZIP file on the command line. You will be prompted for a password.
command shows you a lot with information about the ZIP file. The only sensible way to handle the amount of output this command can give is to lead it through
zipdetails source_code.zip | less
Note that the information will contain file names even if the ZIP file is password protected. This type of information is stored in the ZIP file as metadata and is not included in encrypted data.
Search Inside the file With the zipgrep command
zipgrep allows you to search in the files in a ZIP file. In the following example we want to know which files in the ZIP file have the text "keyval.h" in them.
zipgrep keyval.h source_code.zip
 We can see that the files
getval.c contain the string "keyval .hrs". We can also see that there are two copies of each of these files in different directories in the ZIP file.
View information With the zipinfo command
zipinfo gives you yet another way to look inside a ZIP file. As before, we move the output through
zipinfo source_code.zip | less
From left to right shows the output:
- File permissions
- The version of the tool used to create the ZIP file
- The original file size  A file descriptor (described below)
- The compression method (deflation, in this case)
- Data content and time stamp
- File name and all directories
The file descriptor consists of two characters. The first character will be a "t" or a "b" to specify a text or binary file. If it is a capital letter, the file is encrypted. The second character may be one of four characters. This type represents the type of metadata included in this file: none, an extended local heading, an "extra field" or both.
- -: If not, there is also a hyphen  1: l: if there is an extended local header but no extra field
- x: if not there is an extended local header but there is an additional field
- X: if there is an extended local header and there is an extra field
Share the file with the zipsplit command
If you need to send the ZIP file to someone else but there are size restrictions or problems with the transfer of the file, you can use the
zipsplit command to split the original ZIP file in a set of smaller ZIP files.
-n ] (size) lets you specify a maximum size for each of the new ZIP files. In this example, we share the file
source_code.zip . We do not want any of the new ZIP files to be larger than 100 KB (102400 bytes).
zipsplit -n 102400 source_code.zip
The size you choose cannot be smaller than the size of any of the files in the ZIP file.
Using these commands, you can create your own ZIP files, extract zip files you receive, and perform various other operations on them without ever running the Linux terminal.