您的位置:首页 > 运维架构 > Linux

day18--linux下gzip、bzip2、zip、xz三种压缩工具的介绍

2017-11-09 23:29 731 查看
6.1:压缩打包介绍:

压缩:节省空间,方便传输,带宽资源耗费变少:
常用的压缩文件类型:
windows: .rar .zip .7z
linux: .zip .gz .bz2 .xz .tar.gz .tar.bz2 .tar.xz

1.压缩工具gzip:(不能压缩目录,且压缩后删除源文件)
语法: gzip [-d1-9] file

-d:压缩源文件时使用(结合gzip使用)《=======>gunzip(效果一样)
1-9:指的是压缩级别,1为最差(最大),9为最好(最小),默认是6.
1.1:首先我准备一个内容大的文件,效果比较明显
[root@localhost ~]# du -sh 11.txt            #查看此文件大小的内容为2M.
2.0M    11.txt
[root@localhost ~]# gzip 11.txt              #gzip对其进行压缩:
[root@localhost ~]# du -sh 11.txt.gz         #压缩后大小为24K,效果明显:
24K     11.txt.gz
[root@localhost ~]# gzip -d 11.txt.gz        #使用gzip对其加压缩:
[root@localhost ~]# gzip -1 11.txt           #使用压缩级别1(最差的)来对文本进行压缩:
[root@localhost ~]# du -sh 11.txt.gz         #压缩后大小变为240K:
240K    11.txt.gz
[root@localhost ~]# gunzip 11.txt.gz         #使用gunzip进行解压缩:
如上我们可知,文本压缩的命令有两种如下:
gzip -d file (-d必须要加)
gunzip file (不需要加任何参数)
1.2:gzip还支持对压缩文件内容进行查看
语法:zcat filename
[root@localhost ~]# zcat 1.txt.gz        #使用zcat进行查看
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
1.3:gzip也支持压缩时保留源文件: ( -c )
语法: gzip -c 源文件 压缩后文件名 (两者的名称不能相同)
[root@localhost ~]# gzip -c 1.txt > 2.txt.gz


[root@localhost ~]# gzip -dc 1.txt.gz > 5.txt #将文件解压后到5.txt文件:注意:有时候保留源文件后,再解压时会提示文件已存在,则无法解压:

[root@localhost ~]# bzip2 -d 1.txt.bz2
bzip2: Output file 1.txt already exists.
则可以使用-df选项,强制覆盖:
[root@localhost ~]# bzip2 -df 1.txt.bz2
2、压缩工具bzip2(不支持压缩目录,压缩后不保留源文件)
压缩级别:1-9:默认为9
2.1:语法:bzip [ -dz ] filename
选项如下:
-z:压缩文件时使用(不见-z选项也是可以的)
-d:解压文件时使用:
也是准备一个内容比较大的文件:
[root@localhost ~]# du -sh 11.txt          #查看文件大小为2M:
2.0M    11.txt
[root@localhost ~]# bzip2 11.txt           #使用bzip2进行压缩,默认级别是9:
[root@localhost ~]# du -sh 11.txt.bz2      #压缩后28K,效果较明显:
28K     11.txt.bz2
[root@localhost ~]# bzip2 -d 11.txt.bz2    #使用bzip2 -d 进行解压缩:
[root@localhost ~]# bzip2 -z1 11.txt       #使用1级别进行压缩:文件144K:
[root@localhost ~]# du -sh 11.txt.bz2      #再次查看文件大小:
144K    11.txt.bz2
由此得出:bzip的九个压缩级别:1级别压缩后内容最9压缩后内容最

2.2:gzip2还支持对压缩文件内容进行查看语法:bzcat filename
[root@localhost ~]# bzcat 1.txt.bz2         #bzcat查看文件内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
1.3:gzip也支持压缩时保留源文件: ( -k )语法: bzip2 -k 源文件 压缩后文件名 (两者的名称不能相同)
[root@localhost ~]# bzip2 -k 1.txt  > 2.txt.bz2       #保留源文件:


[root@localhost ~]# bzip2 -dc 1.txt.bz2 > 5.txt #将文件解压后到5.txt文件:3、压缩工具zip:(可以同时压缩文件和目录)-->压缩后会保留源文件:
可以使用yum install -y zip 来安装
windows和linux下的压缩文件可以通用:
语法如下
压缩文件:zip 压缩后名称 需压缩的文件
解压文件:unzip 压缩包名称
3.1:解压缩文件:
[root@localhost ~]# zip 1.txt.zip 1.txt #用zip来压缩文件:
adding: 1.txt (deflated 59%)
[root@localhost ~]# ls -l 1.txt 1.txt.zip
-rw-r--r-- 1 root root 964 11月 9 21:16 1.txt
-rw-r--r-- 1 root root 555 11月 9 22:42 1.txt.zip
3.2:解压缩目录:zip -r 压缩后目录名称 需压缩的目录
[root@localhost ~]# zip -r yuanhh.zip yuanhh
adding: yuanhh/ (stored 0%)
adding: yuanhh/2/ (stored 0%)
adding: yuanhh/4/ (stored 0%)
adding: yuanhh/3 (stored 0%)
adding: yuanhh/1.bak (stored 0%)
adding: yuanhh/1.txt.bz2 (stored 0%)
[root@localhost ~]# ls -ld yuan* #源文件来存在
drwxr-sr-x 4 root yuanhh 63 11月 9 19:24 yuanhh
-rw-r--r-- 1 root root 1360 11月 9 23:02 yuanhh.zip
3.2:所以解压缩文件时,会提示是否覆盖呢:会提示是否覆盖:
[root@localhost ~]# unzip yuanhh.zip
Archive: yuanhh.zip
replace yuanhh/3? [y]es,
o, [A]ll,
one, [r]ename:y
extracting: yuanhh/3
replace yuanhh/1.bak? [y]es,
o, [A]ll,
one, [r]ename: y
extracting: yuanhh/1.bak
replace yuanhh/1.txt.bz2? [y]es,
o, [A]ll,
one, [r]ename: y
extracting: yuanhh/1.txt.bz2
如果不想提示:可以使用unzip -o 压缩文件名
[root@localhost ~]# unzip -o yuanhh.zip #-o则自动覆盖,不会提示:
Archive: yuanhh.zip
extracting: yuanhh/3
extracting: yuanhh/1.bak
extracting: yuanhh/1.txt.bz2
4、压缩工具xz:(不能压缩目录,且压缩后删除源文件)
语法: xz [ -zd ] 文件名
[root@localhost ~]# xz 1.txt #压缩文件
[root@localhost ~]# xz -d 1.txt.xz #解压缩文件
4.1:xz压缩时;也可以保留源文件:用-c:
[root@localhost ~]# xz -c 1.txt > 1.txt.xz #压缩后保留源文件:
[root@localhost ~]# xz -dc 1.txt.xz > 5.txt #将文件解压后到5.txt文件:

附记:使用file命令可以查看文件信息:如下:
[root@localhost ~]# file 2.txt.gz
2.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov 9 21:16:40 2017
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zip gzip bzip2