XZ的介紹
今天升級Python的時候,下載的Python-2.7.8.tar.xz安裝包為xz格式,好吧,我又孤陋寡聞了,居然第一次遇見xz格式的壓縮文件。搜索了一下資料,下面是xz的一些介紹:
xz是一個使用 LZMA壓縮算法的無損數據壓縮文件格式。和gzip與bzip2一樣,同樣支持多文件壓縮,但是約定不能將多於一個的目標文件壓縮進同一個檔案文件。相反,xz通常作為一種歸檔文件自身的壓縮格式,例如使用tar或cpioUnix程序創建的歸檔。xz 在GNU coreutils(版本 7.1 或更新)中被使用。xz 作為壓縮軟件包被收錄在 Fedora (自Fedora 12起), Arch Linux, FreeBSD、 Slackware Linux、CRUX 和 Funtoo中。
XZ Utils壓縮代碼的核心是基於LZMA SDK,但它已經被修改了很多以適應XZ Utils。主壓縮算法目前是LZMA2,它在.xz容器格式中使用。 使用典型文件,XZ Utils比gzip創建的輸出比gzip小30%,比bzip2小15%。到目前為止,lzma utils 的壓縮率仍是最大的,XZ Utils 解壓時間占優勢。
XZ Utils的官方網站為:http://tukaani.org/xz/ ,官網關於XZ的介紹資料如下:
XZ Utils
XZ Utils is free general-purpose data compression software with a high compression ratio. XZ Utils were written for POSIX-like systems, but also work on some not-so-POSIX systems. XZ Utils are the successor to LZMA Utils.
The core of the XZ Utils compression code is based on LZMA SDK, but it has been modified quite a lot to be suitable for XZ Utils. The primary compression algorithm is currently LZMA2, which is used inside the .xz container format. With typical files, XZ Utils create 30 % smaller output than gzip and 15 % smaller output than bzip2.
XZ Utils consist of several components:
· liblzma is a compression library with an API similar to that of zlib.
· xz is a command line tool with syntax similar to that of gzip.
· xzdec is a decompression-only tool smaller than the full-featured xz tool.
· A set of shell scripts (xzgrep, xzdiff, etc.) have been adapted from gzip to ease viewing, grepping, and comparing compressed files.
· Emulation of command line tools of LZMA Utils eases transition from LZMA Utils to XZ Utils.
While liblzma has a zlib-like API, liblzma doesn't include any file I/O functions. A separate I/O library is planned, which would abstract handling of .gz, .bz2, and .xz files with an easy to use API.
XZ的安裝
從官方網址下載https://tukaani.org/xz/xz-5.2.3.tar.gz后,安裝非常簡單。如果你可以yum安裝,那么就更簡單。有些版本甚至默認就自帶xz壓縮命令。
tar -xzvf xz-5.2.3.tar.gz
cd xz-5.2.3
./configure
make
make install
XZ的使用
| -z |
force compression |
壓縮文件 |
| -d |
force decompression |
解壓文件 |
| -t |
test compressed file integrity |
測試壓縮文件完整性 |
| -l |
list information about .xz files |
列出壓縮文件.xz的一些信息 |
| -k |
keep (don't delete) input files |
保留被解壓縮的文件 |
| -f |
force overwrite of output file and (de)compress links |
強制覆蓋輸出文件和(de)壓縮鏈接 |
| -c |
write to standard output and don't delete input files |
壓縮輸入標准輸出並保留被壓縮的文件。 |
| -0 … -9 |
compression preset; default is 6; take compressor *and* decompressor memory usage into account before using 7-9! |
壓縮率預設參數 -0 到 -9調節壓縮率。如果不設置,默認壓縮等級是6 |
| -e |
try to improve compression ratio by using more CPU time; does not affect decompressor memory requirements |
嘗試通過使用更多的CPU時間來提高壓縮比; 不影響解壓內存的要求 |
| -T |
use at most NUM threads; the default is 1; set to 0 to use as many threads as there are processor cores |
壓縮的線程數量。默認為1,設置為0表示跟處理器核數匹配 |
| -q |
suppress warnings; specify twice to suppress errors too |
抑制警告 指定兩次以抑制錯誤 |
| -v |
be verbose; specify twice for even more verbose |
顯示壓縮、解壓詳細信息 |
| -H |
display the long help (lists also the advanced options) |
顯示更多的幫助信息,包含告警選項。 |
| -V |
display the version number and exit |
顯示版本信息並退出 |
解壓文件方法1
[root@DB-Server tmp]# xz -d Python-2.7.8.tar.xz
[root@DB-Server tmp]# tar -xf Python-2.7.8.tar
解壓文件方法2,一次性搞定,但是需要tar支持,有些低版本tar並不支持
[root@DB-Server tmp]#tar -Jxf Python-2.7.8.tar.xz
壓縮文件
[root@DB-Server tmp]# xz -z Python-2.7.8.tar
或
[root@DB-Server tmp]#tar -Jcf Python-2.7.8.tar.xz Python-2.7.8/
查看壓縮文件信息
[root@DB-Server tmp]# xz -l Python-2.7.8.tar.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 10.0 MiB 63.2 MiB 0.159 CRC64 Python-2.7.8.tar.xz
[root@DB-Server tmp]#
壓縮比例簡單測試:
[root@DB-Server tmp]# ls -lrt Python-2.7.8.tar
-rw-r--r-- 1 root root 66263040 Sep 21 22:49 Python-2.7.8.tar
[root@DB-Server tmp]# xz -z Python-2.7.8.tar
[root@DB-Server tmp]# ls -lrt Python-2.7.8.tar.xz
-rw-r--r-- 1 root root 10525244 Sep 21 22:49 Python-2.7.8.tar.xz
[root@DB-Server tmp]# xz -d Python-2.7.8.tar.xz
[root@DB-Server tmp]# gzip Python-2.7.8.tar
[root@DB-Server tmp]# ls -lrt Python-2.7.8.tar.gz
-rw-r--r-- 1 root root 14991942 Sep 21 22:49 Python-2.7.8.tar.gz
[root@DB-Server tmp]# gizp -d Python-2.7.8.tar.gz
-bash: gizp: command not found
[root@DB-Server tmp]# gzip -d Python-2.7.8.tar.gz
[root@DB-Server tmp]# zip Python-2.7.8.tar.gzip Python-2.7.8.tar
adding: Python-2.7.8.tar (deflated 77%)
[root@DB-Server tmp]# ls -lrt Python-2.7.8.tar.gzip
-rw-r--r-- 1 root root 14992071 Sep 22 12:11 Python-2.7.8.tar.gzip
如上簡單測試所示, gzip、zip、xz壓縮的大小對比
Python-2.7.8.tar.xz 10525244
Python-2.7.8.tar.gz 14991942
Python-2.7.8.tar.gzip 14992071
參考資料:
https://teddysun.com/294.html

