压缩解压打包工具基础

 目录

   前言

   compress压缩解压工具

gzip压缩解压工具

bzip2压缩解压工具

xz压缩解压工具

zip压缩打包工具

tar打包工具

split文件分割

cpio打包压缩

 

前言

无论是我们的个人笔记本台式机还是服务器,它们的存储设备可以存储的东西都是有限的,不可能无限的存储东西,除非我们不停地增加硬盘的数量,但是这又是非常不现实的,因为我们需要无限的空间,很多时候我们需要存储大量的不可删除的数据时,就会采用压缩的方式,这样我们就可以即剩下了很多空间同时又保存了我们的文件。

在我们的常用的Windows操作系统上,压缩工具对文件的压缩效果并不是很出色,但是在在linux上文件的压缩率可以达到上千倍。接下来,让我们看看压缩工具都有那些,以及他们的压缩效果吧!

compress压缩解压工具

文件后缀名:.Z

参数:

-d 解压缩

      uncompress

      zcat X.Z > X

   -c 将结果打印到屏幕上,配合重定向,不会覆盖原文件,但权限会变。

-f 默认不对硬链接数为2及以上的文件压缩,加上f,强制压缩指定文件,而其他同inode的文件硬链接数减1.

   -v 显示详细过程。

[root@CT71 app]#ll -h
total 31M
-rw-r--r--. 1 root root 31M Aug 10 15:39 etc.tar
[root@CT71 app]#compress etc.tar
[root@CT71 app]#ll -h
total 16M
-rw-r--r--. 1 root root 16M Aug 10 15:39 etc.tar.Z

由上面我们可以很容易知道compress这个压缩工具压缩率不是特别高,并且压缩后源文件是不会在存在的,如何保证源文件存在同时对文件进行压缩呢?

[root@CT71 app]#compress -c etc.tar > etc.tar.Z
[root@CT71 app]#ll
total 47572
-rw-r--r--. 1 root root 32286720 Aug 10 15:39 etc.tar
-rw-r--r--. 1 root root 16422423 Aug 10 16:27 etc.tar.Z

对于如何解压的问题,上述给出了三种方法,我在这里一一演示:

[root@CT71 app]#compress -d etc.tar.Z
[root@CT71 app]#uncompress etc.tar.Z
[root@CT71 app]#zcat etc.tar.Z > etc.tar

我们此时想,万一我们要压缩的文件是有超链接的话,咋办,我们只需要加上-f选项,就能将我们要压缩的文件从超链接中断掉:

[root@CT71 app]#ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 16:33 etc.tar
[root@CT71 app]#ln etc.tar etc.tar.ln
[root@CT71 app]#ll
total 0
-rw-r--r--. 2 root root 0 Aug 10 16:33 etc.tar  ------------------注意连接数
-rw-r--r--. 2 root root 0 Aug 10 16:33 etc.tar.ln
[root@CT71 app]#compress etc.tar
etc.tar has 1 other links: unchanged
[root@CT71 app]#compress -f etc.tar
[root@CT71 app]#ll
total 4
-rw-r--r--. 1 root root 0 Aug 10 16:33 etc.tar.ln ------------------注意连接数
-rw-r--r--. 1 root root 3 Aug 10 16:33 etc.tar.Z

还有一个-v选项,它的作用就是在我们压缩的过程中显示一个进度,让我们可以了解到我们的压缩进度,对大文件压缩的时候作用比较明显,关于-f与-v选项,在下面我们就不在过多介绍

gzip压缩解压工具

文件后缀名:.gz

参数:

-d 解压缩

      gunzip

      zcat X.gz > X

   -c 将结果打印到屏幕上,配合重定向,不会覆盖原文件,但权限会变。

-f 默认不对硬链接数为2及以上的文件压缩,加上f,强制压缩指定文件,而其他同inode的文件硬链接数减1.

   -v 显示详细过程。

   -# 数字越大,压缩比越高,速度越慢,文件越小。

      -1 等于 --fast

      -23456(default)78

      -9 等于 --best 

[root@CT71 app]#ll -h
total 227M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root 219M Aug 10 16:45 test_gzip
[root@CT71 app]#gzip test_gzip
[root@CT71 app]#ll -h
total 11M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root 2.8M Aug 10 16:45 test_gzip.gz

我们可以看到在默认压缩率的情况下,压缩比已经很高了,如果是9的话,会有多高的压缩率呢?

[root@CT71 app]#gzip -v -9 test_gzip
test_gzip:     98.8% -- replaced with test_gzip.gz
[root@CT71 app]#ll -h
total 11M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root 2.6M Aug 10 16:45 test_gzip.gz

相比默认的压缩率,是比以前小点了。gzip的解压方式也是三种,同时它对文件压缩时,是不会保留原文件的;

[root@CT71 app]#gzip -d test_gzip.gz 
[root@CT71 app]#gunzip test_gzip.gz 
[root@CT71 app]#zcat test_gzip.gz > test_gzip

bzip2压缩解压工具

文件后缀名:.bz2

参数:

-d 解压缩

      bunzip

      bzcat X.bz2 > X

   -k 保留原文件

   -c 将结果打印到屏幕上,配合重定向,不会覆盖原文件,但权限会变。

-f 默认不对硬链接数为2及以上的文件压缩,加上f,强制压缩指定文件,而其他同inode的文件硬链接数减1.

   -v 显示详细过程。

   -# 数字越大,压缩比越高,速度越慢,文件越小。

      -1 等于 --fast

      -2345678

      -9 等于 --best (default)

这个压缩工具相比前两个压缩率不仅有了提高,更重要的是可以保存源文件,并且不改变权限 

[root@CT71 app]#ll -h
total 91M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root  83M Aug 10 17:07 test_gzip
[root@CT71 app]#bzip2 -k test_gzip 
[root@CT71 app]#ll -h
total 91M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root  83M Aug 10 17:07 test_gzip
-rw-r--r--. 1 root root 472K Aug 10 17:07 test_gzip.bz2

同样的我们说一下它的三种解压方式

[root@CT71 app]#bzip2 -d test_gzip.bz2 
[root@CT71 app]#bunzip2 test_gzip.bz2 
[root@CT71 app]#bzcat test_gzip.bz2 > test

xz压缩解压工具

文件后缀名:.xz

参数:

-d 解压缩

      xzcat

   -k 保留原文件

-f 默认不对硬链接数为2及以上的文件压缩,加上f,强制压缩指定文件,而其他同inode的文件硬链接数减1.

   -v 显示详细过程。

   -# 数字越大,压缩比越高,速度越慢,文件越小。

      -0 等于 --fast

      -1 -23456(default)78

      -9 等于 --best 

相对于上面的压缩工具,这个压缩工具是压缩率最高的,高的吓人,用起来也是相当不错的。

[root@CT71 app]#ll -h
total 91M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root  83M Aug 10 17:07 test_xz
[root@CT71 app]#xz -kv test_xz 
test_xz (1/1)
  100 %         41.0 KiB / 82.7 MiB = 0.000    23 MiB/s       0:03             
[root@CT71 app]#ll -h
total 91M
-rw-------. 1 root root 7.6M Aug 10 16:43 messages
-rw-r--r--. 1 root root  83M Aug 10 17:07 test_xz
-rw-r--r--. 1 root root  41K Aug 10 17:07 test_xz.xz

解压方式也是大同小异:

[root@CT71 app]#xz -d test_xz.xz 
[root@CT71 app]#xzcat test_xz.xz > test.xz

zip压缩打包工具

文件后缀名:.zip

打包压缩
    zip –r /testdir/sysconfig /etc/sysconfig/

   unzip

   |zip 将生成的文件名 -

   可以将管道前的输出结果转为文件并压缩。通过此方式压缩的文件只能使用“unzip -p 压缩包 > 新文件 ”来解压缩

unzip -p 预览解压缩后的内容到屏幕,可以配置重定向将结果保存到指定文件,权限会发生变化。 

zip工具具有打包压缩功能,但是一般要配合其他命令加管道才能使用:

[root@CT71 app]#zip -r mima /etc/group /etc/passwd
  adding: etc/group (deflated 48%)
  adding: etc/passwd (deflated 62%)
[root@CT71 app]#ll
total 4
-rw-r--r--. 1 root root 1955 Aug 10 17:58 mima.zip
------------------------------------------------------------------------------

[root@CT71 app]#unzip -d lianxi mima.zip
Archive: mima.zip
inflating: lianxi/etc/group
inflating: lianxi/etc/passwd
[root@CT71 app]#ll
total 4
drwxr-xr-x. 2 root root 33 Aug 10 17:58 etc
drwxr-xr-x. 3 root root 17 Aug 10 19:17 lianxi
-rw-r--r--. 1 root root 1955 Aug 10 17:58 mima.zip
[root@CT71 app]#cd lianxi/
[root@CT71 lianxi]#ll
total 0
drwxr-xr-x. 2 root root 33 Aug 10 19:17 etc
[root@CT71 lianxi]#cd etc/
[root@CT71 etc]#ll
total 8
-rw-r--r--. 1 root root 1184 Aug 7 18:48 group
-rw-r--r--. 1 root root 2711 Aug 7 18:48 passwd

[root@CT71 app]#find /etc/ -type f | zip etc.zip -  -----------压缩
  adding: - (deflated 87%)
[root@CT71 app]#ll
total 16
-rw-r--r--. 1 root root 13184 Aug 10 17:28 etc.zip
[root@CT71 app]#unzip -p etc.zip > etc.zz    -------------------------解压
[root@CT71 app]#ll
total 116
-rw-r--r--. 1 root root  13184 Aug 10 17:28 etc.zip
-rw-r--r--. 1 root root 100756 Aug 10 17:29 etc.zz

tar打包工具

   tar是一个文件归档工具,他可以将很多的文件打包成一个文件,以方便我们对文件的保存,传输,再者就是,我们的压缩工具只能对单个文件进行压缩,要是对大量单个文件进行压缩的话,必须将文件进行打包,然后在进行压缩,因此就体现出打包工具的作用。现在我们简单了解一下如何进行打包,如何解包,如何打包压缩等。

一些参数:

   -c--create:建立新的备份文件

    -C <目录>:这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。

-d:记录文件的差别;

-x--extract--get:从备份文件中还原文件

-t--list:列出备份文件的内容;

-z--gzip--ungzip:通过gzip指令处理备份文件

-Z--compress--uncompress:通过compress指令处理备份文件; -f<备份文件>--file=<备份文件>:指定备份文件

-r:添加文件到已经压缩的文件;

-u:添加改变了和现有的文件到已经存在的压缩文件;

-j:支持bzip2解压文件

-v:显示操作过程;

 

   -T 指定个列表,包含需要被打包的文件,以换行符为间隔

   -X 指定个排除列表,以换行符为间隔

 

格式:tar [option] 新建文件名  要处理的文件

示例:  

   tar –cvf etc.tar /etc/ ----将/etc/内的文件打包
   tar –xvf etc.tar  -----将etc.tar解包
   tar –jcvf etc.tar.bz2 /etc/----将/etc/内的文件打包压缩
   tar –jxvf etc.tar.bz2将etc.tar ----- 解压解包

查看打包后的文件:

[root@CT71 app]#tar -cvf passinfo.tar /etc/passwd /etc/shadow /etc/group /etc/gshadow
tar: Removing leading `/' from member names
/etc/passwd
/etc/shadow
/etc/group
/etc/gshadow
[root@CT71 app]#tar -tvf passinfo.tar 
-rw-r--r-- root/root      2711 2017-08-07 18:48 etc/passwd
---------- root/root      2053 2017-08-07 18:48 etc/shadow
-rw-r--r-- root/root      1184 2017-08-07 18:48 etc/group
---------- root/root      1044 2017-08-07 18:48 etc/gshadow
[root@CT71 app]#tar -tf passinfo.tar 
etc/passwd
etc/shadow
etc/group
etc/gshadow

向包内添加文件:

[root@CT71 app]#tar -tvf passinfo.tar 
-rw-r--r-- root/root      2711 2017-08-07 18:48 etc/passwd
---------- root/root      2053 2017-08-07 18:48 etc/shadow
-rw-r--r-- root/root      1184 2017-08-07 18:48 etc/group
---------- root/root      1044 2017-08-07 18:48 etc/gshadow
[root@CT71 app]#tar -rf passinfo.tar /etc/vimrc 
tar: Removing leading `/' from member names
[root@CT71 app]#tar -tvf passinfo.tar 
-rw-r--r-- root/root      2711 2017-08-07 18:48 etc/passwd
---------- root/root      2053 2017-08-07 18:48 etc/shadow
-rw-r--r-- root/root      1184 2017-08-07 18:48 etc/group
---------- root/root      1044 2017-08-07 18:48 etc/gshadow
-rw-r--r-- root/root      2015 2017-08-01 16:02 etc/vimrc

split文件分割

当我们将很多文件打包后很大,我们可以采取将打包后的文件进行切割,分成很多小文件,将其传输后可以再把他们给复原成一个大文件

split b Size d tar-file-name prefix-name tar包分隔为多个文件

使用split 加上-d和不加上-d切割后的后缀是不一样的,加上-d后后缀是01,02,03……不加后缀,则是以aaabac结尾的

cat 被分隔出的多个文件名 > 单个文件名

   示例:

[root@CT71 app]#ll
total 31532
-rw-r--r--. 1 root root 32286720 Aug 10 11:43 etc.tar
[root@CT71 app]#split -b 5M -d etc.tar etc.tar
[root@CT71 app]#ll
total 63064
-rw-r--r--. 1 root root 32286720 Aug 10 11:43 etc.tar
-rw-r--r--. 1 root root  5242880 Aug 10 11:43 etc.tar00
-rw-r--r--. 1 root root  5242880 Aug 10 11:43 etc.tar01
-rw-r--r--. 1 root root  5242880 Aug 10 11:43 etc.tar02
-rw-r--r--. 1 root root  5242880 Aug 10 11:43 etc.tar03
-rw-r--r--. 1 root root  5242880 Aug 10 11:43 etc.tar04
-rw-r--r--. 1 root root  5242880 Aug 10 11:43 etc.tar05
-rw-r--r--. 1 root root   829440 Aug 10 11:43 etc.tar06

合并文件:

[root@CT71 app]#cat etc.tar0* > etc.tar.1
[root@CT71 app]#ll
total 94596
-rw-r--r--. 1 root root 32286720 Aug 10 17:43 etc.tar
-rw-r--r--. 1 root root  5242880 Aug 10 17:43 etc.tar00
-rw-r--r--. 1 root root  5242880 Aug 10 17:43 etc.tar01
-rw-r--r--. 1 root root  5242880 Aug 10 17:43 etc.tar02
-rw-r--r--. 1 root root  5242880 Aug 10 17:43 etc.tar03
-rw-r--r--. 1 root root  5242880 Aug 10 17:43 etc.tar04
-rw-r--r--. 1 root root  5242880 Aug 10 17:43 etc.tar05
-rw-r--r--. 1 root root   829440 Aug 10 17:43 etc.tar06
-rw-r--r--. 1 root root 32286720 Aug 10 17:44 etc.tar.1
[root@CT71 app]#tar -xf etc.tar.1 
[root@CT71 app]#ll
total 94608
drwxr-xr-x. 133 root root     8192 Aug 10 07:51 etc
-rw-r--r--.   1 root root 32286720 Aug 10 17:43 etc.tar
-rw-r--r--.   1 root root  5242880 Aug 10 17:43 etc.tar00
-rw-r--r--.   1 root root  5242880 Aug 10 17:43 etc.tar01
-rw-r--r--.   1 root root  5242880 Aug 10 17:43 etc.tar02
-rw-r--r--.   1 root root  5242880 Aug 10 17:43 etc.tar03
-rw-r--r--.   1 root root  5242880 Aug 10 17:43 etc.tar04
-rw-r--r--.   1 root root  5242880 Aug 10 17:43 etc.tar05
-rw-r--r--.   1 root root   829440 Aug 10 17:43 etc.tar06
-rw-r--r--.   1 root root 32286720 Aug 10 17:44 etc.tar.1
[root@CT71 app]#ll etc/ | less
total 1352
drwxr-xr-x.  3 root root      101 Jul 11 18:45 abrt
-rw-r--r--.  1 root root       16 Jul 11 18:52 adjtime
-rw-r--r--.  1 root root     1518 Jun  7  2013 aliases
-rw-r--r--.  1 root root    12288 Jul 11 18:54 aliases.db
drwxr-xr-x.  2 root root       51 Jul 11 18:46 alsa
drwxr-xr-x.  2 root root     4096 Jul 11 18:52 alternatives
-rw-------.  1 root root      541 Mar 31  2016 anacrontab
-rw-r--r--.  1 root root       55 Nov  5  2016 asound.conf
-rw-r--r--.  1 root root        1 Nov  6  2016 at.deny
drwxr-xr-x.  2 root root       32 Jul 11 18:45 at-spi2
drwxr-x---.  3 root root       43 Jul 11 18:46 audisp
drwxr-x---.  3 root root       83 Jul 11 18:53 audit
... ...

cpio打包压缩

cpio 有三种操作模式:

 

copy-out模式中, cpio 把文件复制到归档包中。它从标准输入获得文件名列表 (一行一个)。默认 把归档包写到标准输出,因此 一般 重定向到 一个文件中。生成文件名列表的典型方法是使用find 命令; 你可能要在 find 后面用上 -depth选项, 减少因为进入没有访问权限的目录而引起的麻烦。

copy-in模式中, cpio 从归档包里读取文件, 或者列出归档包里的内容。它从标准输入读入归档包。缺省情况下,cpio从标准输入读取输入数据,向标准输出写入输出数据。

copy-pass模式中, cpio把文件从一棵目录树复制到另一棵, 它结合了 copy-in copy-out 的操作, 但不使用归档包。 cpio从标准输入读取欲复制的文件名列表; 目标目录作为非选项的命令行参数给出。

 

cpio支持下列的归档格式: binary, old ASCII, new ASCII, crc, HPUX binary, HPUX old ASCII, old tar, POSIX.1 tar

 

我们针对cpio常用的参数进行讲解:

   参数:

      -o 将文件拷贝打包成文件或者将文件输出到设备上

-i 解包,将打包文件解压或将设备上的备份还原到系统

-t 预览,查看文件内容或者输出到设备上的文件内容

-v 显示打包过程中的文件名称。

-d 解包生成目录,在cpio还原时,自动的建立目录

-c 一种较新的存储方式

示例:

      /etc/下的普通文件打包成cpio文件存放在/app    

[root@CT71 app]#cd /etc/
[root@CT71 etc]#find -type f | cpio -o > /app/etc_f.cpio
58526 blocks
[root@CT71 etc]#cd /app/
[root@CT71 app]#ll
total 29264
-rw-r--r--. 1 root root 29965312 Aug 10 11:22 etc_f.cpio
     
浏览刚刚打包的文件内容
 
[root@CT71 app]#cpio -vt < etc_f.cpio | less
-rw-r--r--   1 root     root          595 Jul 11 18:42 fstab
-rw-------   1 root     root            0 Jul 11 18:42 crypttab
-rw-r--r--   1 root     root           70 Aug 10 07:51 resolv.conf
-rw-r--r--   1 root     root         1160 Nov  5  2016 fonts/conf.d/25-no-bitmap-fedora.conf
-rw-r--r--   1 root     root          978 Aug 31  2013 fonts/conf.d/README
-rw-r--r--   1 root     root         5582 Nov  6  2016 fonts/fonts.conf
-rw-r--r--   1 root     root         1690 Nov 30  2016 pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
-rw-r--r--   1 root     root         1004 Nov 30  2016 pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
-rw-r--r--   1 root     root         1690 Nov 30  2016 pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
-rw-r--r--   1 root     root          166 Nov  5  2016 pki/ca-trust/README
 

新建一个目录,并将文件恢复到该目录下

 [root@CT71 app]#mkdir etc
[root@CT71 app]#ll
total 29264
drwxr-xr-x. 2 root root        6 Aug 10 11:27 etc
-rw-r--r--. 1 root root 29965312 Aug 10 11:22 etc_f.cpio
[root@CT71 app]#cd etc/
[root@CT71 etc]#pwd
/app/etc
[root@CT71 etc]#cpio -id < ../etc_f.cpio
58526 blocks
[root@CT71 etc]#ll
total 1340
drwxr-xr-x. 3 root root      101 Aug 10 11:29 abrt
-rw-r--r--. 1 root root       16 Aug 10 11:29 adjtime
-rw-r--r--. 1 root root     1518 Aug 10 11:29 aliases
-rw-r--r--. 1 root root    12288 Aug 10 11:29 aliases.db
drwxr-xr-x. 2 root root       51 Aug 10 11:29 alsa
-rw-------. 1 root root      541 Aug 10 11:29 anacrontab
-rw-r--r--. 1 root root       55 Aug 10 11:29 asound.conf
-rw-r--r--. 1 root root        1 Aug 10 11:29 at.deny
drwxr-xr-x. 2 root root       32 Aug 10 11:29 at-spi2
drwxr-xr-x. 3 root root       43 Aug 10 11:29 audisp

  

有时候cpio打包的文件过大,我们可以再使用压缩命令对文件进行压缩:

[root@CT71 app]#bzip2 -k -9 etc_f.cpio
[root@CT71 app]#ll -h
total 38M
drwxr-xr-x. 99 root root 8.0K Aug 10 11:29 etc
-rw-r--r--.  1 root root  29M Aug 10 11:22 etc_f.cpio
-rw-r--r--.  1 root root 8.8M Aug 10 11:22 etc_f.cpio.bz2

/boot/initramfs-xxxx.img

      Centos6: cpio.gz文件。zcat initramfs-xxxx.img |cpio -id

      Centos7: cpio文件 cpio -id < initramfs-xxxx.img