In the next article we are going to take a look at the xz compression utility. A colleague told us about her some time ago in an article about how to zip and unzip files in Ubuntu. The use of this tool is similar to gzip and bzip2.
Can be used for compress or decompress files according to the selected operating mode. The compression method used by these files is based on the algorithm of Lzma/ LZMA2. This algorithm began to be developed in the late 90s. It uses a compression dictionary scheme similar to LZ77.
The LZ77 compression algorithm belongs to the family of lossless compressors, also known as text compressors. They are known by that name because they do not omit information from the file when compressing it. Unlike compressors that use algorithms of the type lossy. These omit some information seeking to considerably reduce the size of the original file. An example of this would be jpeg, MP3, MPG, etc.
Compared to "gz" files, "xz" has a better compression ratio and shorter decompression time. However, when we use the default compression settings, it will require more memory to decompress. Gzip has somewhat lower memory usage.
The .xz files are used to compress and decompress data, so that the transfer of files over the Internet or the storage of information on hard drives occupies less. In conclusion, if we want compress a file looking to occupy as little space as possible, we have the option to compress it with xz.
How to use XZ compression
comprimir
El simplest example Compressing a file with xz is as follows. In the terminal (Ctrl + Alt + T) we write:
xz android-x86_64-7.1-r2.iso
You can also use the -z option to perform compression:
xz -z android-x86_64-7.1-r2.iso
These commands will compress the file, but will delete the source file. Yes we do not seek to delete the source files, we will use the -k option As follows:
xz -k android-x86_64-7.1-r2.iso
Decompress
To decompress a file, we will be able to use the -d option:
xz -d android-x86_64-7.1-r2.iso
We can also achieve the same with the option unxz:
unxz android-x86_64-7.1-r2.iso
Force compression
If an operation fails, for example if there is a compressed file with the same name, we will use the -f option to force the process:
xz -kf android-x86_64-7.1-r2.iso
Set compression levels
This tool supports different preset levels of compression (0 to 9. With a default value of 6). We will also be able to use aliases as –fast (it will be fast, but with less compression) to set as value 0 and –best to set as value 9 (slow but higher compression). Some examples of how to set these levels are the following:
xz -k -8 android-x86_64-7.1-r2.iso xz -k --best android-x86_64-7.1-r2.iso
Limit memory
In case of having a small amount of system memory and wanting to compress a huge file, we will have the possibility of using the -memory option = limit (the limit value can be in MB or as a percentage of RAM) to set a memory usage limit for compression:
xz -k --best --memlimit-compress=10% android-x86_64-7.1-r2.iso
Enable silent mode
If we are interested in executing the compression in silent mode, we will only have to add the -q option. We can also enable the verbose mode with -v, as it's shown in the following:
xz -k -q android-x86_64-7.1-r2.iso xz -k -qv android-x86_64-7.1-r2.iso
Create a tar.xz file
The following is an example of the use to get a file with the extension tar.xz.
tar -cf - *.txt | xz -7 > txtfiles.tar.xz
To achieve this same end, we can also use:
tar -cJf txtfiles.tar.xz *.txt
Check the integrity of compressed files
We can test the integrity of the compressed files using the -t option. Using -l we can see the information about a compressed file.
xz -t txtfiles.tar.xz xz -l txtfiles.tar.xz
This is a good tool for compressing files. In this article, we only look at some examples for compressing and decompressing. For more information about everything we can do, you can go to the page man xz.