On Windows, we can create and extract ZIP files using specialized software like WinRAR or 7-Zip. But can we extract ZIP files on Linux? Let's explore further in the article below from Mytour to understand how to compress and decompress ZIP files on the Linux Terminal.
1. ZIP file format
1.1. zip, unzip, and other utilities
2. Creating ZIP files on Linux using the zip command
2.1. Including directories in ZIP files
2.2. Setting compression levels
2.3. Adding passwords to ZIP files
3. Extracting ZIP files on Linux using the unzip command
3.1. Extracting files to a destination directory
3.2. Extracting password-protected ZIP files
3.3. Including files
3.4. Overwriting files
3.5. Searching within ZIP files
4. Adding passwords to ZIP files using the zipcloak command
5. Using the zipdetails command to view detailed information about ZIP files
6. Using the zipgrep command to search within ZIP files
7. Using the zipinfo command to view information about ZIP files
8. Using the zipsplit command to split ZIP files into smaller ones
1. ZIP File Format
The ZIP file format is the most commonly used compressed file format today. While .tar.gz and tar.bz2 files are popular on Linux, there are cases where Windows users may send you files compressed in ZIP format. Or suppose you're using Linux and want to compress files to send to other Windows users. In this case, the ZIP file format is the ideal choice, suitable and compatible with all users.
1.1 zip, unzip, and other utilities
Operating systems like macOS come with built-in commands that allow users to create ZIP files and extract them, these commands are called zip and unzip. Additionally, there are other related utilities such as zipcloak, zipdetails, zipsplit, and zipinfo.
Available utilities are present in Ubuntu 19.04, 18.10, 18.04, Manjaro 18.04, and Fedora 29. However, some Linux distributions like CentOS still lack certain utilities.
To install missing components on Fedora 29, use the following command:
sudo dnf install perl-IO-Compress
To install missing components on CentOS 7, use the following command:
sudo yum install perl-IO-Compress
If any zip utility is missing on the above Linux distributions, you can use the package manager of that Linux distribution to install the necessary package.
2. Creating ZIP Files on Linux Using the zip Command
To create a ZIP file on Linux, first, you need to select the name of the compressed file and the files contained within it. Additionally, if desired, you can append the '.zip' extension to the compressed file name for easy identification.
Suppose to create a file named source_code.zip containing all C source files and header files in the current directory, you use the command below:
zip source_code *.c *.h
Each file will be listed as it is added. The name and number of compressed files will be displayed.
Upon looking at the newly created ZIP file, you will notice that the '.zip' extension has been automatically appended.
ls -l source_code.zip
If you prefer not to display the '.zip' extension on the newly created ZIP file, you can use the -q (quiet) option:
zip -q source_code *.c *.h
2.1 Including directories in the ZIP file
To include subdirectories in the ZIP file, you can use the -r (recursive) option followed by the subdirectory name in the command line. To create a ZIP file like before but including subdirectories in the archive, use the following command:
zip -r -q source_code archive/ *.c *.h
Upon extraction of the ZIP file by the recipient, all files will be organized and placed within a single directory on their computer.
The following command will compress the 'work' directory and all its subdirectories. Note that this command will compress the parent directory of the 'work' directory:
zip -r -q source_code work/
2.2. Setting Compression Level
If desired, you can also set the compression level for the files added to the ZIP file. The range is from 0 to 9, where 0 means no compression. Higher compression levels take more time to create the ZIP file.
To instruct zip to use a specific compression level, simply pass the number as an option on the command line by adding '-', like the command below:
zip -0 -r -q source_code work/
By default, the compression level is 6, so you don't need to provide the -6 option, but adding this option won't affect anything either.
zip -r -q source_code work/
The maximum compression level is 9.
zip -9 -r -q source_code work/
For selected files and directories, the difference between level 0 (no compression) and default compression level (level 6) is 400K. The difference between default compression level (level 6) and maximum compression level (level 9) is only 4K.
For archives storing hundreds or thousands of files, smaller archive sizes save more disk space.
2.3. Add Password to ZIP File
Adding a password to a ZIP file is quite simple. Just use the -e (encrypt) option and you will be prompted to enter the password twice for verification:
zip -e -r -q source_code work/
3. Unzipping ZIP Files on Linux using unzip Command
To unzip a ZIP file on Linux, we use the unzip command followed by the ZIP file name. Note that we need to add the '.zip' extension:
unzip source_code.zip
After extraction, the files will be listed and displayed in the Terminal window:
Similar to the zip command, the unzip command also has the -q (quiet) option. So, if you don't want to see the extracted files displayed in the Terminal window, you use the following command:
unzip -q source_code.zip
3.1. Extracting files to the destination directory
To extract files to a specific directory, you use the -d (directory) option followed by the path to the directory where you want to store the extracted files.
unzip -q source_code.zip -d ./development
3.2. Extracting a password-protected ZIP file
If a ZIP file is created with a password, during the extraction process, you will be prompted to enter the password to proceed. If the wrong password is entered, the extraction process will fail.
unzip -q source_code.zip
If you're unconcerned about the password protection and whether others might detect or view the file, you can add the password directly in the command line along with the -P (password) option. Note that this option must be capitalized as '-P'.
unzip -P fifty.treacle.cutlass -q source_code.zip
3.3. Including specific files
If you don't want to extract specific files or a group of files, you can use the -x (exclude) option. For instance, in this example, Mytour will extract all files except those with the extension '.h':
unzip -q source_code.zip -x *.h
3.4. Overwriting files
Assume you've just extracted a ZIP file but unfortunately, you accidentally deleted some of the extracted files.
The solution in this case is to extract the files again. However, if you extract the ZIP file in the same directory as before, you will be prompted to overwrite the files. In this scenario, you can use the following response options:
Note: Apart from the r (rename) response option, the other options are case-sensitive.
y: Yes, overwrite this file.
n: No, do not allow overwriting the file.
A: All, overwrite all files.
N: None, do not overwrite any file.
r:Rename, extract this file but give it a new name. You will be prompted to enter a new name.
To force extraction and overwrite existing files, we use the -o (overwrite) option:
unzip -o -q source_code.zip
Another effective solution to replace missing files is to extract only those files not present in the destination directory. To achieve this, we use the -n (never overwrite) option:
unzip -n source_code.zip
3.5. Searching within ZIP files
Additionally, if you wish, you can preview the contents of a ZIP file before extracting it. To do this, you can use the -l (list archive) option:
unzip -l source_code.zip | less
The command output will display the files and directories inside the ZIP file, including their sizes and the dates they were added to the archive. Press 'q' to exit.
4. Adding a password to a ZIP file using zipcloak command
In case you've just created a ZIP file but unfortunately forgot to add a password for protection. In this scenario, you can easily add a password to the ZIP file using the zipcloak command followed by the ZIP file name in the command line. You will then be prompted to enter the password twice for verification.
zipcloak source_code.zip
5. Using zipdetails command to view details of a ZIP file
The zipdetails command will display relevant information about the ZIP file:
zipdetails source_code.zip | less
Note that this information includes file names even when the ZIP file is protected by a password. This type of information is stored within the ZIP file as metadata and is not part of the encrypted data.
6. Using the zipgrep command to search within a ZIP file
The zipgrep command also allows you to search within a ZIP file. In the example below, suppose you want to search for a file containing the string 'keyval.h' within the ZIP file, you use the command below:
zipgrep keyval.h source_code.zip
As you can see, the files slang.c and getval.c contain the string 'keyval.h'. Additionally, there are 2 copies of each file in different directories within the ZIP file.
7. Using the zipinfo command to view information about a ZIP file
Besides the zipgrep command, the zipinfo command also allows users to view the contents inside a ZIP archive.
zipinfo source_code.zip | less
From left to right, the output displays:
- The permissions of the file.
- Version of the tool used to create the ZIP file.
- Size of the original file.
- File descriptor.
- Compression method (deflation, in this case).
- Data and time stamp.
- Name of the file and directory.
The file descriptor is created by 2 characters. The first character is 't' or 'b' to indicate whether it's a text file or a binary file. If the character is uppercase, the file is encrypted. The second character can be one of the four characters below. These characters represent the types of metadata included in the file: none, extended local header, extra field, or both.
-: If not present, the character will be a hyphen
l: Indicates the presence of an extended local header without an extra field.
x: Indicates the absence of an extended local header with an extra field.
X: Indicates the presence of both an extended local header and an extra field.
8. Utilize the zipsplit command to split the ZIP file into smaller files
In case you need to send a compressed ZIP file to someone but face size limitations or encounter issues during the file transfer, you can use the zipsplit command to split the original ZIP file into smaller-sized ZIP files.
The -n (size) option allows you to set the maximum size for each new ZIP file. In this example, Mytour will split the file named source_code.zip into new ZIP files with sizes under 100KB (102400 bytes).
zipsplit -n 102400 source_code.zip
The size you choose cannot be smaller than the size of any file within the ZIP file.
This article by Mytour.vn has just guided you on how to compress and decompress ZIP files on the Linux Terminal. Additionally, readers can explore some articles already available on Mytour to learn how to create ZIP files in Mac OS X easily.
