文档的压缩与打包

一 常见压缩文件

  • windows:   rar,zip,7z
  • linux : zip, tar, gz,bz2 ,xz, tar.gz等 

Linux上常见的压缩文件通常都是tar.gz格式的。tar.gz格式表示 先由tar打包,然后再由gzip 压缩。

使用压缩文件的好处:能节省磁盘空间,还能传输时节省网络带宽(上传和下载)

 

二 gzip压缩工具

gzip命令格式: gzip [-d#] filename ,其中# 为1-9的数字。

  • -d : 该参数在解压缩时使用。
  • -#:表示压缩等级,1为最差,9为最好,6为默认。

实例:

1  压缩文件1.txt 后生成1.txt.gz格式的压缩文件。

[root@localhost tmp]# ls
1.txt
[root@localhost tmp]# gzip 1.txt
[root@localhost tmp]# ls
1.txt.gz

 注:gzip后直接跟文件名,表示在当前目录下压缩该文件,而原文件也会消失。

2 解压 压缩文件1.txt.gz 如下

[root@localhost tmp]# gzip -d 1.txt
[root@localhost tmp]# ls
1.txt

3 gzip 不支持压缩目录,压缩目录会报错,如下所示

[root@localhost /]# gzip /test
gzip: /test is a directory -- ignored

-# 选项平时很少用,使用默认压缩级别就行。

 

三 bzip2压缩工具

bzip2命令格式: bzip2  [-dz]  filename。 -z 压缩和-d 解压缩两个选项。压缩级别默认为9.

加或不加-z 都可压缩文件。

[root@localhost tmp]# ls
1.txt
[root@localhost tmp]# touch 2.txt
[root@localhost tmp]# ls
1.txt  2.txt
[root@localhost tmp]# bzip2 2.txt
-bash: bzip2: 未找到命令

如果没有bzip2命令,需要安装。

输入命令:# yum install -y bzip2

 

实例如下所示:

[root@localhost tmp]# bzip2 2.txt
[root@localhost tmp]# ls
1.txt  2.txt.bz2
[root@localhost tmp]# bzip2 -d 2.txt.bz2
[root@localhost tmp]# ls
1.txt  2.txt
[root@localhost tmp]# bzip2 -z 2.txt
[root@localhost tmp]# ls
1.txt  2.txt.bz2

bzip2命令 也不可以压缩目录,压缩目录会报错。

[root@localhost /]# bzip2  /test/
bzip2: Input file /test/ is a directory.

 

四  xz压缩工具

xz命令格式:xz  [-dz]  filename。类似bizp2。同样也不可以压缩目录。

实例如下:

[root@localhost tmp]# ls
1.txt  2.txt
[root@localhost tmp]# touch 3.txt
[root@localhost tmp]# ls
1.txt  2.txt  3.txt
[root@localhost tmp]# xz 3.txt
[root@localhost tmp]# ls
1.txt  2.txt  3.txt.xz

[root@localhost tmp]# xz -d 3.txt.xz
[root@localhost tmp]# ls
1.txt  2.txt  3.txt
[root@localhost tmp]# cd /
[root@localhost /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  newdir  opt  proc  root  run  sbin  srv  sys  test  tmp  usr  var
[root@localhost /]# xz /test
xz: /test: Is a directory, skipping

 

五 zip 压缩工具

zip 可以压缩目录和文件,压缩目录时,需要指定目录下的文件。

用法特殊,需要先安装。

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

实例:

  • 压缩文件
[root@localhost tmp]# ls
1.txt  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF
[root@localhost tmp]# zip 1.txt.zip  1.txt   //zip压缩
  adding: 1.txt (deflated 64%)
[root@localhost tmp]# ls
1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF
[root@localhost tmp]# du -sh 1.txt.zip   // 压缩后大小为12K
12K	1.txt.zip
[root@localhost tmp]# du -sh 1.txt       // 压缩前大小为28K
28K	1.txt

  •  压缩目录到指定目录下
[root@localhost tmp]# ls
11  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test
[root@localhost tmp]# zip -r test.zip test/ 
  adding: test/ (stored 0%)
  adding: test/1.txt (deflated 64%)
  adding: test/test/ (stored 0%)
[root@localhost tmp]# ls
11  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test  test.zip
  • 解压缩文件,命令unzip需要先安装。
[root@localhost tmp]# unzip 1.txt.zip 
Archive:  1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: 11^H
  inflating: 11                      
[root@localhost tmp]# ls
11  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF
// 会弹出提示,选择后是否生成新的文件  , 新的文件11

  • 查看压缩文件列表
[root@localhost tmp]# unzip -l  1.txt.zip 
Archive:  1.txt.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
    25447  01-04-2018 22:34   1.txt
---------                     -------
    25447                     1 file
  • 解压到指定目录下
root@localhost tmp]# unzip 1.txt.zip -d test/  //解压到指定目录下
Archive:  1.txt.zip
  inflating: test/1.txt              
warning:  skipped "../" path component(s) in ../test/
   creating: test/test/
[root@localhost tmp]# ls
11  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test
[root@localhost tmp]# ls test/
1.txt  test

 

六 tar打包工具

tar 可以把目录打包成一个文件,它把所有文件整合成一个大文件。

格式为: tar [-zjxcvfpP] filename.tar

 

实例如下:

  • 打包文件
[root@localhost tmp]# ls
11  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test  test.zip
[root@localhost tmp]# tar -cvf 12.tar  test 1.txt 2.txt
test/
test/1.txt
test/test/
1.txt
2.txt
[root@localhost tmp]# ls
11  12.tar  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test  test.zip
// 新的打包文件12.tar
  • 查看已打包好的tar文件中的文件列表
[root@localhost tmp]# tar -tf 12.tar 
test/
test/1.txt
test/test/
1.txt
2.txt
  • 解包(原来的文件不删除,而且它会覆盖当前已经存在的文件或目录)
[root@localhost tmp]# tar -tf 12.tar 
test/
test/1.txt
test/test/
1.txt
2.txt
[root@localhost tmp]# ls
11  12.tar  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test  test.zip
[root@localhost tmp]# rm -rf test
[root@localhost tmp]# ls
11  12.tar  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test.zip
[root@localhost tmp]# tar -xvf 12.tar
test/
test/1.txt
test/test/
1.txt
2.txt
[root@localhost tmp]# ls
11  12.tar  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test  test.zip

 

七 打包并压缩

使用gzip格式压缩,用-z 选项,就可压缩成gzip格式的文件。 (文件格式为.tar.gz)

实例如下:

[root@localhost tmp]# tar -zcvf 13.tar.gz  test
test/
test/1.txt
test/test/
[root@localhost tmp]# ls
11  12.tar  13.tar.gz  1.txt  1.txt.zip  2.txt  3.txt  systemd-private-c4e9699d74f04b4f9e0a4f2f95e6e55d-vmtoolsd.service-5rMXGF  test  test.zip
// 打包并压缩后的格式为.tar.gz

 

转载于:https://my.oschina.net/primerliu/blog/1603300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值