File Packing and Compression

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will introduce some Linux compression/decompression tools, precisely zip and tar.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/CompressionandArchivingGroup -.-> linux/zip("`Compressing`") linux/CompressionandArchivingGroup -.-> linux/unzip("`Decompressing`") linux/TextProcessingGroup -.-> linux/sort("`Text Sorting`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/pipeline -.-> lab-17998{{"`File Packing and Compression`"}} linux/apt -.-> lab-17998{{"`File Packing and Compression`"}} linux/tar -.-> lab-17998{{"`File Packing and Compression`"}} linux/zip -.-> lab-17998{{"`File Packing and Compression`"}} linux/unzip -.-> lab-17998{{"`File Packing and Compression`"}} linux/sort -.-> lab-17998{{"`File Packing and Compression`"}} linux/mkdir -.-> lab-17998{{"`File Packing and Compression`"}} linux/cp -.-> lab-17998{{"`File Packing and Compression`"}} linux/sudo -.-> lab-17998{{"`File Packing and Compression`"}} linux/du -.-> lab-17998{{"`File Packing and Compression`"}} shell/globbing_expansion -.-> lab-17998{{"`File Packing and Compression`"}} linux/wildcard -.-> lab-17998{{"`File Packing and Compression`"}} end

Explanation

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

Using zip to Compress and Package Programs

Use zip 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:

  • the -r parameter (also called option) indicates that the recursive package contains the entire contents of the subdirectories.
  • the -q parameter represents a quiet mode meaning no messages are output to the screen.
  • the -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).

Set the compression level to 9 and 1 (9 maximum, 1 minimum):

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
  • h, --human-readable
  • d, --max-depth(Display an entry for all files and directories depth.)
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.

Creating an encrypted zip package

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.

Using unzip to Decompress a Zip File

Extract 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.

Create a tar archive (called a tarball) without compression:

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
  • The parameter -c means the creation of a tar archive.
  • The parameter -f is used to specify a file name.
  • The parameter -v to output the names of the files so packaged (v = verbose).

Extracting a 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

Viewing an archive

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

Compress files

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

Time for Fun

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

Summary

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.

Other Linux Tutorials you may like