1. 程式人生 > >9、壓縮/解壓縮及任務計劃介紹

9、壓縮/解壓縮及任務計劃介紹

壓縮、解壓縮、歸檔

1、壓縮比:

壓縮前和壓縮後的大小體積比例

2、壓縮目的:

時間換空間,用cpu的時間換磁盤的空間;如何選擇壓縮,要衡量是節省cpu時間還是節省硬盤空間。


3、linux壓縮、解壓縮工具,及歸檔工具:

序號壓縮工具解壓縮工具不解壓查看內容後綴備註
1compressuncompress
.z
2gzip
gunzipzcat.gz只能壓縮文件
3bzip2bunzip2
.bz2只能壓縮文件
4xz
unxz
.xz只能壓縮文件
5lzmaunlzmalzcat.lzma
6zip
unzipzcat.zip壓縮比比較小







歸檔工具



1tar




2cpio





3.1、gzip、 gunzip、 zcat:

gzip [ options ] [ name ... ] //壓縮單個文件,後綴為.gz,同時刪除原文件

[ options ]:

-d:直接解壓縮,相當於gunzip,

-#:制定壓縮比,默認是6,數字越大,壓縮比越大(範圍1-9),不建議修改

-c:將壓縮結果輸出至標準輸出。

格式: gzip -c FILE >/PATH/TO/SOMEFILE.GZ

如:

[root@localhost tmp]# ls

fstab issue

[root@localhost tmp]# gzip -c issue > ./issue1.gz

[root@localhost tmp]# ll

total 12

-rw-r--r--. 1 root root 540 Nov 20 19:38 fstab

-rw-r--r--. 1 root root 23 Nov 20 19:38 issue

-rw-r--r--. 1 root root 49 Nov 20 19:53 issue1.gz

[root@localhost tmp]# zcat issue1.gz

\S

Kernel \r on an \m


[root@localhost tmp]#


gunzip [ optios ] [ name ... ] //解壓縮,同時會刪除原壓縮文件

zcat [ -fhLV ] [ name ... ] //直接查看壓縮中的文件內容,用cat查看會出現亂碼

如:

[root@localhost tmp]# ls

fstab issue

[root@localhost tmp]# gzip fstab issue

[root@localhost tmp]# ll

total 8

-rw-r--r--. 1 root root 295 Nov 20 19:38 fstab.gz

-rw-r--r--. 1 root root 49 Nov 20 19:38 issue.gz

[root@localhost tmp]#


[root@localhost tmp]# gunzip fstab.gz issue.gz

[root@localhost tmp]# ll

total 8

-rw-r--r--. 1 root root 540 Nov 20 19:38 fstab

-rw-r--r--. 1 root root 23 Nov 20 19:38 issue

[root@localhost tmp]#


[root@localhost tmp]# zcat issue.gz

\S

Kernel \r on an \m


[root@localhost tmp]#


3.2、 bzip2, bunzip2 、bzcat

bzip2 [ options ] [ filenames ... ] //壓縮單個文件,後綴為.bz2,同時刪除原文件

[ options ]:

-d:直接解壓縮,相當於bunzip,

-#:制定壓縮比,默認是6,數字越大,壓縮比越大(範圍1-9),不建議修改

-k:保留原文件


bunzip2 [ options ] [ filenames ... ]

bzcat [ -s ] [ filenames ... ]

如:

[root@localhost tmp]# gzip ./*

[root@localhost tmp]# ll

total 8

-rw-r--r--. 1 root root 295 Nov 20 19:38 fstab.gz

-rw-r--r--. 1 root root 49 Nov 20 19:38 issue.gz

[root@localhost tmp]# gzip -d fstab.gz issue.gz

[root@localhost tmp]# ll

total 8

-rw-r--r--. 1 root root 540 Nov 20 19:38 fstab

-rw-r--r--. 1 root root 23 Nov 20 19:38 issue



3.3、 xz, unxz, xzcat,

xz [ options ] [ filenames ... ] //壓縮單個文件,後綴為.xz,同時刪除原文件

[ options ]:

-d:直接解壓縮,相當於unxz,

-#:制定壓縮比,默認是6,數字越大,壓縮比越大(範圍1-9),不建議修改

-k:保留原文件



註意:gzip、bzip、xz只能壓縮文件不能是目錄,而且三者壓縮和解壓縮都會刪除原文件,三者壓縮比依次增大



3.4、歸檔工具:tar、cpio

由於gzip、bzip2、xz壓縮軟件只能壓縮單個文件,不能壓縮目錄。這種情況下,

如果要壓縮一個目錄,則先要進行歸檔操作,歸檔操作就是將多個文件打包成一個。

歸檔一般會增大文件體積,因為歸檔也要有一些文件參與,因此可以將目錄歸檔後在壓縮。


3.4.1、tar命令格式:

tar [OPTION...] [FILE]...

註意:tar命令的options可以不帶“-”,而且創建、展開、查看必須帶-f選項。

options:

-c:創建歸檔

tar -cf /PATH/TO/SOMEFILE.tar FILE....

-x:展開歸檔;-C:和-x一起使用表示展開到何處目錄,也可以不跟,表示當前目錄

tar -xf /PATH/FROM/SOMEFILE.tar [-C /TO/SOME/PATH]

-t:不展開歸檔查看歸檔文件

tar -tf /PATH/TO/SOMEFILE.tar

歸檔後在進行壓縮,也可以歸檔的同時進行壓縮(結合gzip、bzip2、xz)。

歸檔並壓縮:

-z:調用gzip

-j:調用bzip2

-J:調用xz

tar {z|j|J}cf /PATH/TO/SOMEFILE.tar.{gz|bz2|xz} FILE... //歸檔並壓縮

tar {z|j|J}xf /PATH/TO/SOMEFILE.tar.{gz|bz2|xz} FILE... //展開歸檔並解壓縮

如:

[root@localhost tmp]# ls

test

[root@localhost tmp]# tar -zcf test.tar.gz test/

[root@localhost tmp]# ll

total 8

drwxr-xr-x. 2 root root 49 Nov 20 20:55 test

-rw-r--r--. 1 root root 5139 Nov 20 21:08 test.tar.gz

[root@localhost tmp]#


如:

[root@localhost tmp]# ll

total 0

drwxr-xr-x. 2 root root 49 Nov 20 20:55 test

[root@localhost tmp]# gzip test/

gzip: test/ is a directory -- ignored

[root@localhost tmp]# tar -cf test/ //必須指明歸檔後的文件名,否則不通過,如這裏所示

tar: Cowardly refusing to create an empty archive

Try `tar --help‘ or `tar --usage‘ for more information.

[root@localhost tmp]# tar -cf test.tar test/

[root@localhost tmp]# ls

test test.tar

[root@localhost tmp]# tar -tf test.tar //查看歸檔中的文件有哪些

test/

test/fstab

test/functions

test/issue

[root@localhost tmp]#

[root@localhost tmp]# gzip test.tar

[root@localhost tmp]# ll

total 8

drwxr-xr-x. 2 root root 49 Nov 20 20:55 test

-rw-r--r--. 1 root root 5148 Nov 20 20:56 test.tar.gz

[root@localhost tmp]#


3.5、zip:既能歸檔又能壓縮,因此可以壓縮目錄,但是壓縮比有限。

zip、unzip

命令格式:

zip /PATH/TO/SOMEFILE.zip FILE....

如:

[root@localhost tmp]# ls

test

[root@localhost tmp]# zip test

zip error: Nothing to do! (test.zip)

[root@localhost tmp]# zip test.zip

zip error: Nothing to do! (test.zip)

[root@localhost tmp]# zip test.zip test/

adding: test/ (stored 0%)

[root@localhost tmp]# ls

test test.zip

[root@localhost tmp]#

9、壓縮/解壓縮及任務計劃介紹