Compress and decompress ZIP files on Linux Terminal


ZIP files are zipped files commonly used on Windows and macOS platforms, even Linux. To create and extract ZIP files on Linux, we can use the common Linux Terminal commands, refer to the article below of Taimienphi.vn to learn how to compress and decompress ZIP files on Linux Terminal.


On Windows, we can create and extract ZIP files using specialized software like WinRAR or 7 Zip. So can you decompress Zip file on Linux ?, you read along refer to the article below of Taimienphi.vn to learn how to compress and decompress ZIP file on Linux Terminal.

Table of Contents:
1. Compressed ZIP file format
1.1. The command zip, unzip, and other utilities
2. How to create a ZIP file on Linux with the zip command
2.1. Include folders in the ZIP file
2.2. Set the compression level
2.3. Add a password for the ZIP file
3. Extract the ZIP file on Linux with the unzip command
3.1. Extract the files to the destination folder
3.2. Unzip the password protected ZIP file
3.3. Including files
3.4. Overwrite the files
3.5. Search inside the ZIP file
4. Add a ZIP file password with the zipcloak command
5. Use the zipdetails command to view the ZIP file details
6. Use the zipgrep command to search inside the ZIP file
7. Use the zipinfo command to view ZIP file information
8. Use the zipsplit command to split the ZIP file into smaller files

1. Compressed ZIP file format

ZIP file is the most commonly used compressed file format. Although .tar.gz and tar.bz2 are popular files on Linux, sometimes Windows users may send you compressed files in ZIP format. Or suppose in case if you are using Linux and want to compress the files to send to other Windows users, in this case the ZIP file format is the best choice, suitable and compatible with all users. .

1.1 Command zip, unzip, and other utilities

On operating systems such as macOS, there are built-in commands that allow users to create ZIP files and extract these files, called zip and unzip commands. There are also some other related utilities like zipcloak, zipdetails, zipsplit and zipinfo.

The above utilities are currently available on Ubuntu versions 19.04, 18.10, 18.04, Manjaro 18.04 and Fedora 29. However, on some other Linux distributions such as CentOS, some utilities are still missing.

To install missing elements on Fedora 29, use the following command:

sudo dnf install perl-IO-Compress

boot and boot zip file on linux terminal 2

To install missing elements on CentOS 7, use the following command:

sudo yum install perl-IO-Compress

boot and boot zip file in linux terminal 3

If any zip utility is missing on the above Linux distributions, you can use the package management tool of that Linux distribution to install the necessary package.

2. How to create a ZIP file on Linux with the zip command

To create a ZIP file on Linux, you must first select the name of the zip file and the files it contains. Alternatively if you wish, you can add the “.zip” extension to the filename for easy identification.

Suppose to create a file named source_code.zip To store all C source files and header files in the current directory, use the following command:

zip source_code * .c * .h

boot and boot zip file in linux terminal 4

Each file will be listed as added. The name and number of compressed files will be displayed.

boot and boot zip file in linux terminal 5

When you look at the newly created ZIP file, you will see that the “.zip” extension has been automatically added.

ls -l source_code.zip

boot and boot zip file in linux terminal 6

If you do not want to display the “.zip” extension on the newly created ZIP file, you can use the option -q (quiet):

zip -q source_code * .c * .h

boot and boot zip file in linux terminal 7

2.1 Include folders in the ZIP file

To include subfolders in a ZIP file, you can use the option -r (recursive) and the subdirectory name in the command line. To create a ZIP file like before but include the subdirectories in the archive, use the following command:

zip -r -q source_code archive / * .c * .h

boot and boot zip file in linux terminal 8

For the recipient to unzip the ZIP file, all the files will be organized and located in a folder on their computer.

The following command will compress the work directory and all subfolders. Note that this command will compress the work directory’s parent folder:

zip -r -q source_code work /

nen and zip file zip on linux terminal 9

2.2. Set the compression level

If you wish, you can also set the compression level for files that add these files to the ZIP archive. The range is from 0 to 9, where 0 is uncompressed. The higher the compression level, the longer it will take to create a ZIP file.

To zip using a specific level of compression, we just need to convert the number as an option on the command line by adding “-“, as the command below:

zip -0 -r -q source_code work /

boot and boot zip file in linux terminal 10

The default compression level is 6, so you don’t need to provide the -6 option, but adding this option doesn’t matter.

zip -r -q source_code work /

boot and boot zip file in linux terminal 11

Maximum compression level is 9.

zip -9 -r -q source_code work /

boot and boot zip file in linux terminal 12

With files and folders compressed selected, the difference between level 0 (uncompressed) and the default level of compression (level 6) is 400K. The difference between the default compression level (level 6) and the highest level of compression (level 9) is only 4K.

With compressed files that store hundreds or thousands of files, the smaller the file size, the more free space will be saved.

2.3. Add a password for the ZIP file

Adding a password to a ZIP file is quite simple. Just use the option -e (encrypt) and you will be asked to enter your password 2 times to verify:

zip -e -r -q source_code work /

boot and boot zip file in linux terminal 13

3. Extract the ZIP file on Linux with the unzip command

To extract the ZIP file on Linux, we use the command unzip + ZIP file name. Note we will have to add the extension “.zip“:

unzip source_code.zip

nen and zip file zip on linux terminal 14

The extracted files will be listed and displayed in the Terminal window:

boot and boot zip file in linux terminal 15

Just like the zip command, the unzip command has options too -q (quiet), so if you don’t want to see and display the following files when compressed in the Terminal window, use the following command:

unzip -q source_code.zip

nen and zip file zip on linux terminal 16

3.1. Extract the files to the destination folder

To extract the files into a specific directory, you use the option -d (directory) + path to the directory where you want to store the extracted file.

unzip -q source_code.zip -d ./development

nen and zip file zip on linux terminal 17

3.2. Unzip the password protected ZIP file

If a ZIP file is created with a password protected, during the decompression of the file you will be asked to enter a password to continue. If the password is entered incorrectly, the file extraction process will fail.

unzip -q source_code.zip

nen and zip file zip on linux 18 terminal

If you don’t care if the password to protect the file has been detected and seen by others, you can add the password in the command line with the same options. -P (password). Note that this option must be capitalized as “-P“.

unzip -P fifty.treacle.cutlass -q source_code.zip

How to install zip file in terminal linux 19

3.3. Including files

If you do not want to solve one or a specific file group, you can use the option -x (exclude). Suppose that in this example, Taimienphi.vn will extract all files in addition to the files ending in. “.H”:

unzip -q source_code.zip -x * .h

boot and boot zip file in linux terminal 20

3.4. Overwrite the files

Suppose you’ve just unzipped a ZIP file, but unfortunately you have deleted some decompression files.

The solution in this case is to decompress the file again. But if you unzip the ZIP file in the same directory first, you will be told to overwrite the file. In this case you can use the feedback options below:

Note: in addition to the r (rename) response option, the remaining options are case-sensitive.

y: Yes, overwrite this file.

n: No, do not allow file overwriting.

A: All, overwrite all files.

N: None, don’t overwrite any files.

r:RenameUnzip the file but give it a new name. You will be prompted to enter a new name.

nen and zip file zip on linux terminal 21

To force decompress to overwrite existing files, we use the option -o (overwrite):

unzip -o -q source_code.zip

show and remove zip file on linux terminal 22

In addition, the most effective solution to replace missing files is to extract only compressed files that are not in the destination directory. To do this, we use the option -n (never overwrite):

unzip -n source_code.zip

boot and boot zip file in linux terminal 23

3.5. Search inside the ZIP file

Also if you want you can preview the files inside the ZIP file before extracting the file. To do this, you can use the option -l (list archive):

unzip -l source_code.zip | less

boot and boot zip file in linux terminal 24

The command output will display the files and folders inside the ZIP file, including the size and date added to the archive. Press “q“to exit.

nen and rip zip file on linux terminal 25

4. Add a ZIP file password with the zipcloak command

In case if you’ve just created a ZIP file but unfortunately you forgot to add no password protection. In this case you can easily add a password to the ZIP file using the command zipcloak + ZIP file name in the command line. You will now be asked to enter your password 2 times to verify.

zipcloak source_code.zip

show and remove zip file on linux terminal 26

5. Use the zipdetails command to view the ZIP file details

The zipdetails command will display information related to the ZIP file:

zipdetails source_code.zip | less

show and remove zip file on linux terminal 27

Note that this information includes the file name even if the ZIP file is password protected. This type of information is stored in a ZIP file as metadata (metadata) and is not part of the encrypted data.

nen and zip file zip on linux terminal 28

6. Use the zipgrep command to search inside the ZIP file

The zipgrep command also lets you search within ZIP files. In the example below, suppose that if you wanted to search for files with the string “keyval.h“in the ZIP file, use the following command:

zipgrep keyval.h source_code.zip

nen and zip file zip on linux terminal 29

As you can see the slang.c and getval.c files contain the string “keyval.h”. There are also 2 copies of each file in different directories in the ZIP file.

7. Use the zipinfo command to view ZIP file information

In addition to the zipgrep command, the zipinfo command also allows users to view the contents of a ZIP file.

zipinfo source_code.zip | less

nen and zip file zip on linux terminal 30

From left to right, the output shows:

– File permissions

– The version of the tool used to create a ZIP file.

– Original file size.

– File descriptor.

– Compression method (deflation, in this case).

– Data and time stamp

– Name of file and directory.

File descriptor is made of 2 characters. The first character is “t” or “b” to indicate it is a text file or a binary file. If the character is a printed letter, the file will be encrypted. The second character can be one of the four characters below. These characters represent the metadata types included in the file: none, extended local header, extra field, or both.

: If none exists, the character will be a hyphen

l: If there is an extended local header, there is no extra field.

x: If no extended local header has extra field.

X: If there is extended local header and extra field.

nen and zip file zip on linux terminal 31

8. Use the zipsplit command to split the ZIP file into smaller files

If you need to send a ZIP file to a user but the size is limited or there is a problem sending the file, you can use the zipsplit command to divide the original ZIP file into smaller ZIP files.

Option -n (size) Allow setting the maximum size for each new ZIP file. In this example, Taimienphi.vn will split the file named source_code.zip into new ZIP files below 100KB (102400 bytes).

zipsplit -n 102400 source_code.zip

nen and zip file zip on linux terminal 32

The size you choose cannot be smaller than the size of any file in the ZIP file.

https://thuthuat.taimienphi.vn/nen-va-giai-nen-file-zip-tren-terminal-linux-47687n.aspx
The article above Taimienphi. vn has shown you how to compress and decompress ZIP files on Linux Terminal. Also readers can refer to some articles already on Taimienphi.vn to learn how Create ZIP files in Mac OS X is simple.

.

Add a Comment

Your email address will not be published. Required fields are marked *