Introduction
In this lab, we will introduce some Linux compression/decompression tools, precisely zip
and tar
.
In this lab, we will introduce some Linux compression/decompression tools, precisely zip
and tar
.
Before diving deep into the compression tools available on Linux platforms, it is necessary to introduce some file formats commonly used by compression packages. The most common file formats on Windows are .zip
, .rar
, .7z
. On Linux, in addition to the above three formats, there are _.gz
, _.xz
, _.bz2
, _.tar
, _.tar.gz
, _.tar.xz
, _.tar.bz2
.
Extension | Description |
---|---|
*.zip |
The files are packaged and compressed using zip programs |
*.rar |
The files are packaged and compressed using rar programs |
*.7z |
The files are packaged and compressed using 7z programs |
*.tar |
The files without compression are packaged using tar programs |
*.gz |
The compressed files are packaged using gzip programs |
*.xz |
The files are packaged and compressed using xz programs |
*.bz2 |
The files are packaged and compressed using bzip2 programs |
*.tar.gz |
The files are packaged using tar programs and compressed using gzip programs |
*.tar.xz |
The files are packaged using tar programs and compressed using xz programs |
*tar.bz2 |
The files are packaged using tar programs and compressed using bzip2 programs |
*.tar.7z |
The files are packaged using tar programs and compressed using 7z programs |
zip
to Compress and Package Programszip
to package a folder:View the file in the ~/project/lib
directory.
cd ~/project
ls lib/*
zip -r -q -o labex.zip /home/labex/project/lib
du -h labex.zip
file labex.zip
labex:project/ $ zip -r -q -o labex.zip /home/labex/project/lib
du -h labex.zip
file labex.zip
4.0K labex.zip
labex.zip: Zip archive data, at least v1.0 to extract, compression method=store
The above command wraps the directory /home/labex/project/lib
into a file. Now let's look at the size and type of the packaged file.
In the first line of the command:
-r
parameter (also called option) indicates that the recursive package contains the entire contents of the subdirectories.-q
parameter represents a quiet mode meaning no messages are output to the screen.-o
parameter indicates that the output file is required, followed by the package output file name.After packaging and compressing the directory, we will use du
to view the compressed file size (zip file).
zip -r -9 -q -o labex_9.zip /home/labex/project/lib -x ~/*.zip
zip -r -1 -q -o labex_1.zip /home/labex/project/lib -x ~/*.zip
We add a parameter to set the compression level -[1-9]
. 1
means the fastest compression but is bulky. 9
stands for the smallest size but takes the longest time. -x
is to avoid the previous package-generated zip file being packaged into this current zip file. You can only use the absolute path; otherwise, it will not work.
We use du
to view the default compression level, the minimum and the highest compression levels, and also the sizes of uncompressed files:
du -h -d 0 *.zip ~ | sort
labex:project/ $ du -h -d 0 *.zip ~ | sort
1.9G /home/labex
4.0K labex_1.zip
4.0K labex_9.zip
4.0K labex.zip
You can see that the default compression level is the highest, which creates the smallest-sized zip file and takes the longest to finish file compression.
Use the -e
parameter to create an encrypted file:
zip -r -e -o labex_encryption.zip /home/labex/project/lib
Enter password:
Verify password:
adding: home/labex/project/lib/ (stored 0%)
adding: home/labex/project/lib/hello.txt (stored 0%)
adding: home/labex/project/lib/linux.txt (stored 0%)
adding: home/labex/project/lib/labex.txt (stored 0%)
Warning: For zip
, there are some compatibility issues between Windows and Linux/UNIX, such as line break(a non-printable character is signifying the end of the line), which is CR+LF (Carriage-Return + Line-Feed) in Windows and LF in Linux. You can't see line breaks if you open text content on a Windows platform, which was edited on a Linux platform.
zip -r -l -o labex.zip /home/labex/project/lib
For the above problem, you must add the -l
parameter to convert LF to CR+LF.
unzip
to Decompress a Zip FileExtract files in labex.zip
to the current directory:
unzip labex.zip
Use quiet mode to extract the file to a directory specified using the -d
parameter:
unzip -q labex.zip -d ziptest
labex:project/ $ unzip -q labex.zip -d ziptest
labex:project/ $ ll
total 16K
...
drwxrwxr-x 3 labex labex 18 Feb 20 11:17 ziptest
If you only intend to see the contents of the compressed package, you can use the -l
parameter. The files in the package will not be extracted:
unzip -l labex.zip
Using the -O
parameter, we can specify the encoding type:
unzip -O GBK labex.zip
tar
First, we start by learning some of the primary uses of tar
.
tar -cf labex.tar /home/labex/project/lib
labex:project/ $ tar -cf labex.tar /home/labex/project/lib
labex:project/ $ du -h labex.tar
12K labex.tar
-c
means the creation of a tar archive.-f
is used to specify a file name.-v
to output the names of the files so packaged (v = verbose).tar
archive (tarball)Use the -x
parameter to extract to the current directory. Use the -C
parameter along with the -x
parameter to extract to a specified path:
mkdir tardir
tar -xf labex.tar -C tardir
labex:project/ $ tree tardir/
tardir/
└── home
└── labex
└── project
└── lib
├── hello.txt
├── labex.txt
└── linux.txt
4 directories, 3 files
Use the -t
parameter to view the contents of a tar archive but not decompress it:
tar -tf labex.tar
labex:project/ $ tar -tf labex.tar
home/labex/project/lib/
home/labex/project/lib/hello.txt
home/labex/project/lib/linux.txt
home/labex/project/lib/labex.txt
Adding the -z
parameter, we can use gzip to compress files:
tar -czf labex.tar.gz home/labex/project/lib
labex:project/ $ du -h labex.tar.gz
4.0K labex.tar.gz
Use the -x
parameter for extraction for the *.tar.gz
archive:
tar -xzf labex.tar.gz
labex:project/ $ tree home
home
└── labex
└── project
└── lib
├── hello.txt
├── labex.txt
└── linux.txt
3 directories, 3 files
We only need to change one parameter if we want to use a different compression tool:
File format | Parameter |
---|---|
*.tar.gz |
-z |
*.tar.xz |
-J |
*.tar.bz2 |
-j |
Here's an interesting program that can simulate the shape of a flame:
sudo apt-get install libaa-bin
aafire
You will see the fire in the another terminant
Create a file named test
, then use zip
and tar
for packaging and compressing it respectively. In the last step, you need to extract the archive to /home/labex
.