2019独角兽企业重金招聘Python工程师标准>>>
6.1 压缩打包介绍
6.2 gzip压缩工具
6.3 bzip2压缩工具
6.4 xz压缩工具
一、压缩打包介绍
压缩的优点:
节约存储空间
节约宽带
提高上传下载速度
常见的压缩类型:
Windows : .rar、.zip、.7z
Linux:.zip, .gz, .bz2, .xz, .tar.gz, .tar.bz2, .tar.xz
虽然linux的文件后缀名不代表文件的类型,但是为了方便分辨压缩类型,还是需要规范的使用压缩后缀。
二、gzip压缩工具
- gzip特性
gzip不能压缩目录
gzip压缩可以分为1-9级,1级为效率最低,9级最高。级别越高耗费的cpu越大,默认的级别为6。
- 生成一个大文件进行测:
find /etc/ -type f -name "*conf" -exec cat {} >> /tmp/gzip/1.txt \;
- 重复以上操作几次,查看1.txt大小和文件行数:
[root@wxy01 gzip]# du -sh
1.2M .
[root@wxy01 gzip]# wc -l 1.txt
34160 1.txt
- 使用gzip进行压缩,然后查看文件大小:
这种方式压缩后源文件会消失
[root@wxy01 gzip]# gzip 1.txt
[root@wxy01 gzip]# ls
1.txt.gz
[root@wxy01 gzip]# du -sh 1.txt.gz
308K 1.txt.gz
- 查看压缩文件的信息:
[root@wxy01 gzip]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Oct 8 10:41:24 2018
[root@wxy01 gzip]#
- 解压
或者使用gunzip 命令也可以解压,用法相同。
[root@wxy01 gzip]# gzip -d 1.txt.gz
[root@wxy01 gzip]# ls
1.txt
- 不删除源文件进行压缩
这种方式也可指定其他目录,如想压缩到/home下就可以:gzip -c 1.txt > /home
[root@wxy01 gzip]# gzip -c 1.txt > 1.txt.gz
[root@wxy01 gzip]# ll
total 1480
-rw-r--r-- 1 root root 1197896 Oct 8 10:41 1.txt
-rw-r--r-- 1 root root 311347 Oct 8 10:56 1.txt.gz
- 指定解压目录或名称
[root@wxy01 gzip]# gzip -d -c 1.txt.gz > /home/2.txt
[root@wxy01 gzip]# ll
total 1480
-rw-r--r-- 1 root root 1197896 Oct 8 10:41 1.txt
-rw-r--r-- 1 root root 311347 Oct 8 10:56 1.txt.gz
[root@wxy01 gzip]# ll /home/2.txt
-rw-r--r-- 1 root root 1197896 Oct 8 11:00 /home/2.txt
[root@wxy01 gzip]#
- 压缩后的文件不能查看,如果想查看可以使用zcat命令进行查看
三、bzip2压缩工具
如果不能使用bzip2命令,需要安装:
yum -y install bzip2
- bzip2和gzip对比:
压缩效率更高,当然cpu耗费的也更高。
bzip2和gzip的用法相同,如:解压都是-d 指定目录或名称都是-c
同样不支持压缩目录
bzip2的默认压缩级别为:9
bzip2可以使用bzcat命令查看压缩内容
- 同一文件使用gzip和bzip2压缩后大小对比:
[root@wxy01 gzip]# du -sh 1.txt.bz2
100K 1.txt.bz2
[root@wxy01 gzip]# du -sh 1.txt.gz
308K 1.txt.gz
[root@wxy01 gzip]#
- 如果把压缩文件的后缀改为普通文本后缀, 查看的时候就会有相关提示,遇到这种问题可以用file查看文件是什么类型,然后就可以做相关的操作
[root@wxy01 gzip]# less 1bz.txt
"1bz.txt" may be a binary file. See it anyway?
[root@wxy01 gzip]# file 1bz.txt
1bz.txt: bzip2 compressed data, block size = 900k
[root@wxy01 gzip]#
四、xz压缩工具
xz压缩工具和之前的gzip和bzip2用法基本相同,并不常用,通常在tar包中可以看到.tar.xz类型。
xz的压缩效率比bzip2的更加高,消耗的cpu也更加高。
xz的压缩使用的参数和gzip和bzip2相同。
xzcat可以查看压缩内容