博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2018-05-25笔记(文件的压缩和打包)
阅读量:7091 次
发布时间:2019-06-28

本文共 10691 字,大约阅读时间需要 35 分钟。

第六章文件的压缩和打包

6.1 压缩打包介绍

Linux环境常见压缩文件类型:.zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz等,linux系统中的后缀名其实要不要无所谓,但是对于压缩文件来讲必须要带上。这是为了判断压缩文件是由哪种压缩工具所压缩,而后才能去正确的解压缩这个文件。压缩打包的目的是为了方便文件传输,节省磁盘空间,减少传输花费的时间,节省带宽等

.gz gzip 压缩工具压缩的文件
.bz2 bzip2 压缩工具压缩的文件
.tar tar 打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件)
.tar.gz 可以理解为先用tar打包,然后再gzip压缩
.tar.bz2 同上,先用tar打包,然后再bzip2压缩

6.2 gzip压缩工具

gzip是GNUzip的缩写,它是一个GNU自由软件的文件压缩程序,用于UNIX系统的文件压缩。我们在Linux中经常会用到后缀为.gz的文件,它们就是gzip格式的,注意:gzip不能用来压缩目录

语法: gzip [-d#] filename 其中#为1-9的数字
选项:
-d:解压缩(=gunzip)
-#:指定压缩等级,此处#表示1~9数字,9压缩最好,默认为6(压缩等级越高,CPU消耗越高)
-c:指定压缩或解压后的路径

[root@localhost ~]# dd if=/dev/zero of=/tmp/2.txt bs=1M count=10     #用dd命创建一个10M的文件2.txt记录了10+0 的读入记录了10+0 的写出10485760字节(10 MB)已复制,0.0111434 秒,941 MB/秒[root@localhost ~]# du -sh /tmp/2.txt10M /tmp/2.txt[root@localhost ~]# cd /tmp[root@localhost tmp]# gzip 2.txt                                                           #直接用gzip压缩,源文件消失,变成2.txt.gz[root@localhost tmp]# du -sh 2.txt.gz                                                  #可以看到2.txt已经被压缩成12K12K 2.txt.gz[root@localhost tmp]# file 2.txt.gz                                                       #用file查看文件的类型是gzip压缩文件2.txt.gz: gzip compressed data, was "2.txt", from Unix, last modified: Thu May 24 11:50:02 2018[root@localhost tmp]# gzip -d 2.txt.gz                                                 #-d选项是解压用的,压缩文件消失,变成2.txt[root@localhost tmp]# ll总用量 10252drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 11:50 2.txt

-c指定压缩后的路径,源文件不消失

[root@localhost tmp]# gzip -c 2.txt > /tmp/2.txt.gz[root@localhost tmp]# ll                                             #这里可以看到原来的2.txt还在总用量 10264drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 11:50 2.txt-rw-r--r--  1 root    root       10214 5月  24 11:57 2.txt.gz

查看压缩包内容zcat

[root@localhost tmp]# zcat 2.txt.gz      #可查看压缩包的内部内容

解压除了-d选项外,还可以使用gunzip,也可以加上-c指定解压后的路径

[root@localhost tmp]# gunzip 2.txt.gz             gzip: 2.txt already exists; do you wish to overwrite (y or n)? y     #因为之前-c选项2.txt已经存在,会提示是否覆盖[root@localhost tmp]# ll总用量 10252drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 11:57 2.txt

6.3 bzip2 压缩工具

bzip2 是一个基于Burrows-Wheeler 变换的无损压缩软件,压缩效果比传统的LZ77/LZ78压缩算法来得好。它是一款免费软件。可以自由分发免费使用。它广泛存在于UNIX&LINUX的许多发行版本中。bzip2能够进行高质量的数据压缩。它利用先进的压缩技术,能够把普通的数据文件压缩10%至15%,压缩的速度和解压的效率都非常高!支持大多数压缩格式,包括tar、gzip 等等

注意: bzip2也不可以压缩目录
语法: bzip2 [options] [filename]
选项:
-d:解压缩
-z:压缩(=bzip2,所以可以不带该参数直接使用)
-c:指定压缩或解压后的路径
bzip2的使用方法同gzip

[root@localhost tmp]# bzip2 2.txt              #用bzip2压缩2.txt,源文件消失,变成2.txt.bz2[root@localhost tmp]# ll总用量 16drwxr-xr-x  2 root    root    4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump   68 5月  22 13:52 123.log-rw-r--r--  1 root    root      49 5月  24 11:57 2.txt.bz2[root@localhost tmp]# du -sh 2.txt.bz2              #可以看到压缩等级比gzip要高,之前gzip压缩后为12k,这里压缩完变成4K4.0K    2.txt.bz2

解压可以使用-d选项或者bunzip2,这里使用bunzip2解压

[root@localhost tmp]# bunzip2 2.txt.bz2              #解压后,源文件消失,变成2.txt[root@localhost tmp]# ll总用量 10252drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 11:57 2.txt

-c指定解压缩后路径,源文件不消失

[root@localhost tmp]# bzip2 -c 2.txt >/tmp/2.txt.bz2         [root@localhost tmp]# ll                                               #可以看到原来的2.txt还在总用量 10256drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 11:57 2.txt-rw-r--r--  1 root    root          49 5月  24 12:19 2.txt.bz2

同样可以使用bzcat查看压缩包的内容

[root@localhost tmp]# bzcat 2.txt.bz2             #查看压缩包内容

6.4 xz压缩工具

注意: xz同样不可用于压缩目录

语法: xz [options] [filename]
选项:
-d:解压缩

[root@localhost tmp]# xz 2.txt             #不跟选项直接压缩,源文件消失[root@localhost tmp]# ll总用量 16drwxr-xr-x  2 root    root    4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump   68 5月  22 13:52 123.log-rw-r--r--  1 root    root    1660 5月  24 12:19 2.txt.xzdrwxrwxrwx. 2 root    root    4096 5月   1 02:49 nfstest-rw-------. 1 root    root       0 4月  22 00:02 yum.log-rw-------  1 root    root       0 5月  17 01:52 yum.log.bak[root@localhost tmp]# du -sh 2.txt.xz 4.0K    2.txt.xz

解压可以使用-d或者unxz命令

[root@localhost tmp]# xz -d 2.txt.xz                #源文件消失,变成2.txt[root@localhost tmp]# ll总用量 10252drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt

-c选项同gzip和bzip2用法一样

6.5 zip压缩工具

windows下最常见的压缩格式,这里也可以用在linux下,如果没有这个命令,需要安装zip包

[root@localhost tmp]# yum install -y zip

注意: zip既可以压缩目录文件也可以压缩普通文件,源文件不消失

语法: zip [options] [filename.zip] [filename]
说明: zip后面先跟目标文件名,也就是自定义的压缩包名,然后跟源文件名。
选项:
-r:压缩目录文件时使用,表示级联压缩,连通目录内文件一同压缩

[root@localhost tmp]# zip 2.txt.zip 2.txt                #这里先指定压缩后的文件名2.txt.zip再跟要压缩的文件,源文件不消失  adding: 2.txt (deflated 100%)[root@localhost tmp]# ll总用量 10264drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt-rw-r--r--  1 root    root       10350 5月  24 12:35 2.txt.zip

压缩目录-r,连通目录内文件一同压缩

[root@localhost tmp]# zip 123.zip 123  adding: 123/ (stored 0%)[root@localhost tmp]# ll总用量 10268drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root         158 5月  24 12:38 123.zip

解压

[root@localhost tmp]# unzip 2.txt.zip      Archive:  2.txt.zipreplace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y                  #因为源文件不消失,2.txt已经存在,所以提示  inflating: 2.txt                   [root@localhost tmp]# ll总用量 10268drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root         158 5月  24 12:38 123.zip-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt-rw-r--r--  1 root    root       10350 5月  24 12:35 2.txt.zip

-d选项可以指定解压时路径

[root@localhost tmp]# unzip 2.txt.zip -d /tmp/3.txt      #因为2.txt已经存在,这里改成解压为/tmp/3.txtArchive:  2.txt.zip  inflating: /tmp/3.txt/2.txt        [root@localhost tmp]# ll总用量 10272drwxr-xr-x  2 root    root        4096 5月  22 09:38 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root         158 5月  24 12:38 123.zip-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt-rw-r--r--  1 root    root       10350 5月  24 12:35 2.txt.zipdrwxr-xr-x  2 root    root        4096 5月  24 12:43 3.txt

查看压缩文件

[root@localhost tmp]# unzip -l 2.txt.zip Archive:  2.txt.zip  Length      Date    Time    Name---------  ---------- -----   ---- 10485760  05-24-2018 12:19   2.txt---------                     ------- 10485760                     1 file

6.6 tar打包

语法: tar [options] [filename]

选项:
-c:建立一个tar包或者压缩文件包
-f:指定目标文件名,如果多个参数组合使用时,把-f放在最后面,必加选项
-z:同时用gzip压缩
-j:同时用bzip2压缩
-J:同时用xz压缩
-t:查看包里面的文件
-v:可视化
--exclude:后面跟文件名,表示打包除了该文件之外的内容
打包目录或文件tar cvf

[root@localhost tmp]# touch 123/222.txt 123/333.txt[root@localhost tmp]# ll 123总用量 0-rw-r--r-- 1 root root 0 5月  24 12:48 222.txt-rw-r--r-- 1 root root 0 5月  24 12:48 333.txt[root@localhost tmp]# tar cvf 123.tar 123             #先指定打包后文件名再跟打包的文件,这里比较特殊,选项可以加-或者不加-,同样123/123/333.txt123/222.txt[root@localhost tmp]# tar cvf 333.tar 123 2.txt     #同时打包多个123/123/333.txt123/222.txt2.txt

说明: 打包不会删除源文件,当某.tar文件已经存在时,再次打包会直接覆盖该文件,无任何提示。

查看包的内容
-t选项可以用来查看包的内容,-f指定查看的包

[root@localhost tmp]# tar -tvf 333.tar                  #可以看到这前的找包的123目录和目录下的文件,还有2.txtdrwxr-xr-x root/root         0 2018-05-24 12:48 123/-rw-r--r-- root/root         0 2018-05-24 12:48 123/333.txt-rw-r--r-- root/root         0 2018-05-24 12:48 123/222.txt-rw-r--r-- root/root  10485760 2018-05-24 12:19 2.txt

解包tar xvf

[root@localhost tmp]# tar xvf 333.tar               #把333.tar解包,文件已经存在,会直接覆盖,不提示123/123/333.txt123/222.txt2.txt

选择性打包(--exclude)

[root@localhost tmp]# ll 123       #可以看到123目录下有222.txt和333.txt 总用量 0-rw-r--r-- 1 root root 0 5月  24 12:48 222.txt-rw-r--r-- 1 root root 0 5月  24 12:48 333.txt[root@localhost tmp]# tar cvf 123.tar --exclude 333.txt 123           #这里使用--exclude选项打包的时候把333.txt排除123/123/222.txt[root@localhost tmp]# tar cvf 333.tar --exclude "*.txt" 123            #这里排除*.txt,只打包目录本身 123/

6.7 打包并压缩

语法: tar [options] [filename]

选项:
-z:同时用gzip压缩
-j:同时用bzip2压缩
-J:同时用xz压缩
打包的同时用gzip压缩 tar zcvf

[root@localhost tmp]# tar zcvf 3.tar.gz 123                           #这里先指定打包压缩后的文件名123/123/333.txt123/222.txt[root@localhost tmp]# ll[root@localhost tmp]# ll总用量 10260drwxr-xr-x  2 root    root        4096 5月  24 12:48 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt-rw-r--r--  1 root    root         142 5月  24 13:08 3.tar.gz

打包的同时用bzip2压缩 tar jcvf

[root@localhost tmp]# tar jcvf 3.tar.bz2 123123/123/333.txt123/222.txt[root@localhost tmp]# ll总用量 10264drwxr-xr-x  2 root    root        4096 5月  24 12:48 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt-rw-r--r--  1 root    root         152 5月  24 13:11 3.tar.bz2-rw-r--r--  1 root    root         142 5月  24 13:08 3.tar.gz

打包的同时用xz压缩 tar Jcvf,这里是大写J

[root@localhost tmp]# tar Jcvf 3.tar.xz 123123/123/333.txt123/222.txt[root@localhost tmp]# ll总用量 10268drwxr-xr-x  2 root    root        4096 5月  24 12:48 123-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt-rw-r--r--  1 root    root         152 5月  24 13:11 3.tar.bz2-rw-r--r--  1 root    root         142 5月  24 13:08 3.tar.gz-rw-r--r--  1 root    root         192 5月  24 13:12 3.tar.xz

解包

[root@localhost tmp]# tar zxvf 3.tar.gz                 #解包带有zip格式的包123/123/333.txt123/222.txt[root@localhost tmp]# tar jxvf 3.tar.bz2               #解包带有bzip2格式的包123/123/333.txt123/222.txt[root@localhost tmp]# tar Jxvf 3.tar.xz                #解包带有xz格式的包123/123/333.txt123/222.txt

打包发送到远程主机并解包

[root@localhost tmp]# tar cvf - 123 |ssh 192.168.66.132 "cd /tmp/;tar xvf -"123/123/333.txt123/222.txtThe authenticity of host '192.168.66.132 (192.168.66.132)' can't be established.RSA key fingerprint is 2c:57:fc:93:19:b3:56:66:a7:7a:28:fa:47:5e:6e:8f.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.66.132' (RSA) to the list of known hosts.root@192.168.66.132's password: 123/123/333.txt123/222.txt

转载于:https://blog.51cto.com/13736286/2120843

你可能感兴趣的文章
关于类型“LinkButton”的控件“xxx”必须放在具有 runat=server 的窗体标记内问题的解决方案...
查看>>
Javascript:DOM表格操作
查看>>
解决WCF传输大数据量时出错并提示:远程主机强迫关闭了一个现有的连接
查看>>
蓝桥杯-波动数列
查看>>
图片理论基础
查看>>
HDU4300 Clairewd’s message
查看>>
07.设计模式_适配器模式
查看>>
Unity Shader入门精要学习笔记 - 第10章 高级纹理
查看>>
2012.02.09(如何在Linux的Qt中,在while中按键退出)
查看>>
web基础
查看>>
VMware Workstation ubuntu 扩容
查看>>
JavaScript中使用构造器创建对象无需new的情况
查看>>
使用EasyUI,关于日期格式的文本框按照正常方式获取不到值的问题
查看>>
Struts2中的OGNL
查看>>
MySQL5.7 并行复制配置
查看>>
P3980 [NOI2008]志愿者招募
查看>>
js 继承
查看>>
CF710F String Set Queries
查看>>
HTTP协议详解
查看>>
memcache 基础原理
查看>>