6.1 压缩打包介绍
6.2 gzip压缩工具
6.3 bzip2压缩工具
6.4 xz压缩工具
6.1 压缩打包介绍
常见的压缩文件
如windows系统:.rar .zip .7z
linux系统:.zip, .gz, .bz2, .xz, .tar.gz, .tar.xz
在什么时候需要用到压缩打包文件呢?
一般当要传大型文件或者目录十分多的时候,都会把文件进行压缩,减少空间,方便上传或者拷贝。当然如果不进行压缩,上传也是可以的,但是由于现在的技术和经济条件导致宽带的资费很好,机房的宽带资费更高,而且服务器上的存储也是宝贵的资源,所以不能浪费,就要压缩打包,这样加快了时间。
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。
6.2 gzip压缩工具
介绍
gzip是GNUzip的缩写,它是一个GNU自由软件的文件压缩程序。它是Jean-loupGailly和MarkAdler一起开发的。第一次公开发布版本是1992年10月31日发布的版本0.1,1993年2月发布了版本1.0。
使用方法演示:[root@centos7 ~]# cd /tmp/ #进入/tmp/目录下
[root@centos7 tmp]# touch 1.txt #创建1.txt文件
[root@centos7 tmp]# ls -l 1.txt #查看1.txt文件大小
-rw-r--r--. 1 root root 0 11月 9 21:29 1.txt #可以看出文件为0
#做一个准备工作,把/etc/目录后缀名为conf文件 全部追加到/tmp/1.txt里面
[root@centos7 tmp]# sudo find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
#可以多追加几遍 由于我这里提示不够权限 加了sudo命令来执行
[root@centos7 tmp]# ls -lh 1.txt
-rw-r--r--. 1 root root 1.8M 11月 9 21:58 1.txt #文件已经达到1.8M了
[root@centos7 tmp]# gzip 1.txt #执行压缩
[root@centos7 tmp]# ls -lh 1.txt.gz #查看大小
-rw-r--r--. 1 root root 454K 11月 9 21:58 1.txt.gz #现在只有454K了
#解压
[root@centos7 tmp]# gzip -d 1.txt.gz
[root@centos7 tmp]# gunzip 1.txt.gz
[root@centos7 tmp]# ls -lh 1.txt
-rw-r--r--. 1 root root 1.8M 11月 9 21:58 1.txt
#设置压缩级别 一般不进行操作按默认即可 gzip -# 1.txt //#范围1-9 ,默认6
#上述的命令执行过程中会删除源文件
#不能压缩目录
[root@centos7 tmp]# zcat 1.txt.gz #查看文件内容
#指定目录位置
[root@centos7 tmp]# mkdir a
[root@centos7 tmp]# gzip -c 1.txt > /tmp/a/1.txt.gz
[root@centos7 tmp]# ls -lh /tmp/a/1.txt.gz
-rw-r--r--. 1 root root 454K 11月 9 22:11 /tmp/a/1.txt.gz
[root@centos7 tmp]# ls -lh 1.txt #不删除源文件
-rw-r--r--. 1 root root 1.8M 11月 9 21:58 1.txt
[root@centos7 tmp]# gunzip -c /tmp/a/1.txt.gz > /tmp/1.txt.new #解压文件保留源文件
[root@centos7 tmp]# ls -lh 1.txt.new
-rw-r--r--. 1 root root 1.8M 11月 9 22:16 1.txt.new
6.3 bzip2压缩工具
使用bzip2压缩工具需要进行安装[root@centos7 tmp]# yum install -y bipz2
用法如下:[root@centos7 tmp]# ls
1.txt.bz2 newdisk
[root@centos7 tmp]# bzip2 1.txt #或者bzip2 -z 1.txt命令
[root@centos7 tmp]# ls
1.txt.bz2 newdisk
#bzip -# 1.txt //#范围1-9 ,默认9
#不能压缩目录
[root@centos7 tmp]# bzcat 1.txt.bz2 #查看文件内容
[root@centos7 tmp]# bzip2 -c 1.txt > /tmp/a/1.txt.bz2 #指定目录位置
[root@centos7 tmp]# bzip2 -d 1.txt.bz2 #可以使用bunzip2 1.txt.bz2命令
bzip2: Can't open input file 1.txt: No such file or directory.
[root@centos7 tmp]# ls /tmp/a/1.txt.bz2
/tmp/a/1.txt.bz2
#解压
[root@centos7 tmp]# bzip2 -c -d /tmp/a/1.txt.bz2 > /tmp/1.txt.new2
6.4 xz压缩工具
xz是一种压缩文件格式,采用LZMA SDK压缩,目标文件较gzip压缩文件(.gz或·tgz)小30%,较·bz2小15%。
在使用中需要安装[root@centos7 tmp]# yum install -y zip[root@centos7 tmp]# zip 1.txt.zip 1.txt.new
adding: 1.txt.new (deflated 75%)
[root@centos7 tmp]# zip -r a.zip a/
adding: a/ (stored 0%)
adding: a/1.txt.bz2 (stored 0%)
[root@centos7 tmp]# ls
a systemd-private-a5414955458d48b4b4c21658bbcbccd5-vgauthd.service-788vfb
a.zip systemd-private-a5414955458d48b4b4c21658bbcbccd5-vmtoolsd.service-Qs4kXe
#压缩文件夹 不删除源文件
[root@centos7 tmp]# yum install -y unzip #解压需要安装
[root@centos7 tmp]# unzip 1.txt.zip #解压
Archive: 1.txt.zip
replace 1.txt.new? [y]es, [n]o, [A]ll, [N]one, [r]ename: y #询问
inflating: 1.txt.new
[root@centos7 tmp]# unzip a.zip -d /tmp/a_1/ #解压目录并重命名
Archive: a.zip
creating: /tmp/a_1/a/
extracting: /tmp/a_1/a/1.txt.bz2
[root@centos7 tmp]# ls #查看目录
1.txt.new mewdisk
1.txt.new2 newdisk
1.txt.zip systemd-private-a5414955458d48b4b4c21658bbcbccd5-chronyd.service-jTqiB7
a systemd-private-a5414955458d48b4b4c21658bbcbccd5-vgauthd.service-788vfb
a_1 systemd-private-a5414955458d48b4b4c21658bbcbccd5-vmtoolsd.service-Qs4kXe
a.zip user.111
[root@centos7 tmp]# unzip -l a.zip #查看文件列表
Archive: a.zip
Length Date Time Name
--------- ---------- ----- ----
0 11-09-2017 22:36 a/
0 11-09-2017 22:36 a/1.txt.bz2
--------- -------
0 2 files
#可以看到源文件没有删除