压缩打包介绍、 gzip压缩工具、bzip2压缩工具、xz压缩工具

6.1 压缩打包介绍

windows平台有压缩工具: winrar、zip、7z linux平台有压缩工具:gzip、bzip2、xz、tar.gz、tar.bz2、tar.xz 将文件或目录压缩打包,可以节省一定的磁盘空间,同时也方便传输,也包括网络传输(网络传输时节省带宽资源)

6.2 gzip压缩工具--不能压缩目录

//准备工作,生成大1M的文件
[root@24centos7-01 test]# find / -type f -name *conf -exec cat {} >> 2.txt \;
//查看文件大小
[root@24centos7-01 test]# du -sh 1.txt 
1.1M	1.txt
//使用gzip对1.txt进行压缩
[root@24centos7-01 test]# gzip 1.txt 

//压缩后的大小
[root@24centos7-01 test]# du -sh 1.txt.gz 
336K	1.txt.gz
[root@24centos7-01 test]# gzip -d 1.txt.gz 
[root@24centos7-01 test]# du -sh 1.txt 
1.1M	1.txt

[root@24centos7-01 test]# find / -type f -name my.cnf -exec cat {} >> /tmp/test/2.txt \;
[root@24centos7-01 test]# du -sh 2.txt 
4.0K	2.txt
[root@24centos7-01 test]# gzip 2.txt 
[root@24centos7-01 test]# ls
1.txt  2.txt.gz

//使用file查看压缩包
[root@24centos7-01 test]# file 2.txt.gz 
2.txt.gz: gzip compressed data, was "2.txt", from Unix, last modified: Thu Nov  9 19:56:14 2017
//使用zcat -c查看压缩包
[root@24centos7-01 test]# zcat -c 2.txt.gz 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

//测试压缩目录失败
[root@24centos7-01 test]# mkdir test
[root@24centos7-01 test]# ls
1.txt  2.txt.gz  test
[root@24centos7-01 test]# gzip test
gzip: test is a directory -- ignored

//压缩时单独生成压缩包,保留原文件
[root@24centos7-01 test]# gzip -c 1.txt > ./1.txt.gz
[root@24centos7-01 test]# ls
1.txt  1.txt.gz  2.txt.gz  test
//解压时保留原文件
[root@24centos7-01 test]# gzip -d -c ./1.txt.gz > ./3.txt
[root@24centos7-01 test]# ls
1.txt  1.txt.gz  2.txt.gz  3.txt  test

6.3 bzip2压缩工具

//安装
[root@24centos7-01 ~]# yum install -y bzip2

//压缩
[root@24centos7-01 test]# bzip2 1.txt
[root@24centos7-01 test]# ls
1.txt.bz2  3.txt  test
[root@24centos7-01 test]# du -sh 1.txt.bz2 
316K	1.txt.bz2

//解压
[root@24centos7-01 test]# bzip2 -d 1.txt.bz2 
[root@24centos7-01 test]# ls
1.txt  3.txt  test
[root@24centos7-01 test]# bzip2 1.txt
//bzip2 -d等同于bunzip2
[root@24centos7-01 test]# bunzip2 1.txt.bz2 
[root@24centos7-01 test]# ls
1.txt  3.txt  test

//压缩时保留原文件
[root@24centos7-01 test]# bzip2 -c 1.txt > ./1.txt.bz2
[root@24centos7-01 test]# ls
1.txt  1.txt.bz2  3.txt  test
[root@24centos7-01 test]# du -sh 1.txt.bz2 
316K	1.txt.bz2
//解压时生成新文件
[root@24centos7-01 test]# bzip2 -d -c ./1.txt.bz2 > 1.txt.bak
[root@24centos7-01 test]# ls
1.txt  1.txt.bak  1.txt.bz2  3.txt  test
[root@24centos7-01 test]# du -sh 1.txt.bak
1.1M	1.txt.bak

//使用file查看压缩包
[root@24centos7-01 test]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k

//使用bzcat查看bz2包中的文件内容
[root@24centos7-01 test]# find / -type f -name my.cnf -exec cat {} > 3.txt \;
[root@24centos7-01 test]# bzip2 3.txt
[root@24centos7-01 test]# ls
1.txt  1.txt.bak  1.txt.bz2  3.txt.bz2  test
[root@24centos7-01 test]# bzcat 3.txt.bz2 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d



6.4 xz压缩--不能压缩目录

//压缩
[root@24centos7-01 test]# xz 1.txt 
[root@24centos7-01 test]# ls
1.txt.xz  test
[root@24centos7-01 test]# du -sh 1.txt.xz 
276K	1.txt.xz
//解压
[root@24centos7-01 test]# xz -d 1.txt.xz 
[root@24centos7-01 test]# ls
1.txt  test
[root@24centos7-01 test]# xz 1.txt 

//压缩时保留原文件
[root@24centos7-01 test]# xz -c 1.txt > ./1.txt.xz
[root@24centos7-01 test]# ls
1.txt  1.txt.xz  test

//解压时保留原文件
[root@24centos7-01 test]# xz -d -c 1.txt.xz > ./2.txt
[root@24centos7-01 test]# ls
1.txt.xz  2.txt  test

//使用xzcat查看压缩文件内容
[root@24centos7-01 test]# find / -type f -name my.cnf -exec cat {} > 4.txt \;
[root@24centos7-01 test]# ls
1.txt  1.txt.xz  2.txt  4.txt  test
[root@24centos7-01 test]# xz 4.txt 
[root@24centos7-01 test]# xzcat 4.txt.xz 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

//使用file查看压缩文件信息
[root@24centos7-01 test]# file 4.txt.xz 
4.txt.xz: XZ compressed data

//压缩目录测试失败
[root@24centos7-01 test]# xz test
xz: test: Is a directory, skipping

zip同时可以在linux和windows上使用但有一点需要注意:

当目录下还有二级目录甚至更多级目录时,zip并不会把二级目录下的文件压缩,而仅仅是把二级目录本身压缩,那有没有级联的选项呢?当然有,那就是:

    zip -r dir1.zip dir1/ 

猜你喜欢

转载自my.oschina.net/u/3585954/blog/1570751