壓縮打包介紹、 gzip壓縮工具、bzip2壓縮工具、xz壓縮工具

6.1 壓縮打包介紹

windows平臺有壓縮工具: winrar、zip、7z linux平臺有壓縮工具:gzip、bzip二、xz、tar.gz、tar.bz二、tar.xz 將文件或目錄壓縮打包,能夠節省必定的磁盤空間,同時也方便傳輸,也包括網絡傳輸(網絡傳輸時節省帶寬資源)mysql

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並不會把二級目錄下的文件壓縮,而僅僅是把二級目錄自己壓縮,那有沒有級聯的選項呢?固然有,那就是:linux

zip -r dir1.zip dir1/
相關文章
相關標籤/搜索