首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >文件压缩和打包zip、tar 原

文件压缩和打包zip、tar 原

作者头像
阿dai学长
发布2019-04-03 14:54:47
1.2K0
发布2019-04-03 14:54:47
举报
文章被收录于专栏:阿dai_linux阿dai_linux

第六章 文件压缩和打包

6.5 zip压缩工具

zip命令可以用来解压缩文件,或者对文件进行打包操作。zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有“.zip”扩展名的压缩文件。

注意: zip既可以压缩目录文件也可以压缩普通文件。

语法: zip [options] [filename.zip] [filename]

说明: zip后面先跟目标文件名,也就是自定义的压缩包名,然后跟源文件名。

options:

-r:压缩目录文件时使用,表示级联压缩,连通目录内文件一同压缩

  • 安装zip工具
[root@adai002 d6z]# yum install -y zip

压缩

压缩普通文件

[root@adai002 d6z]# zip 1.txt.zip 1.txt
  adding: 1.txt (deflated 74%)
[root@adai002 d6z]# ls 
1.txt  1.txt.zip  2.txt  adai
[root@adai002 d6z]# du -ah
1.3M	./1.txt
1.3M	./2.txt
1.3M	./adai/1.txt
1.3M	./adai/2.txt
2.5M	./adai
328K	./1.txt.zip
5.2M	.

说明: 使用zip压缩文件时,源文件不会被删除。

压缩目录文件

[root@adai002 d6z]# zip adai.zip adai
  adding: adai/ (stored 0%)   
不加-r选项则只会压缩该文件夹,不压缩其内容
###############################
[root@adai002 d6z]# zip -r adai.zip adai
updating: adai/ (stored 0%)
  adding: adai/1.txt (deflated 74%)
  adding: adai/2.txt (deflated 74%)
[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.txt  adai  adai.zip
[root@adai002 d6z]# du -ah
1.3M	./1.txt
1.3M	./2.txt
1.3M	./adai/1.txt
1.3M	./adai/2.txt
2.5M	./adai
328K	./1.txt.zip
656K	./adai.zip
5.9M	.

解压缩

eg1:

[root@adai002 d6z]# unzip 1.txt.zip
Archive:  1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: 1.txt                   
[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.txt  adai  adai.zip

说明: 因zip压缩文件时不删除源文件,所以在相同目录进行解压缩时会提示:‘replace 1.txt? ’。

eg2:

[root@adai002 d6z]# unzip 1.txt.zip -d /tmp/3.txt
Archive:  1.txt.zip
  inflating: /tmp/3.txt/1.txt 

说明: 解压时可以指定目录,但是不能指定其解压后的文件名。

查看压缩文件

[root@adai002 d6z]# unzip -l adai.zip
Archive:  adai.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-22-2017 23:29   adai/
  1277475  06-22-2017 23:29   adai/1.txt
  1277475  06-22-2017 23:29   adai/2.txt
---------                     -------
  2554950                     3 files

说明: zip压缩文件只能使用‘unzip -l’查看其文件目录,无法查看文件内容。

6.6 tar打包

tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。

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

打包

eg1: 打包目录文件

[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.txt  adai  adai.zip
[root@adai002 d6z]# tar -cvf adai.tar adai/
adai/
adai/1.txt
adai/2.txt
[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.txt  adai  adai.tar  adai.zip
[root@adai002 d6z]# du -ah
1.3M	./2.txt
1.3M	./adai/1.txt
1.3M	./adai/2.txt
2.5M	./adai
328K	./1.txt.zip
656K	./adai.zip
1.3M	./1.txt
2.5M	./adai.tar
8.3M	.

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

eg2: 打包普通文件

[root@adai002 d6z]# tar -cvf 2.tar 2.txt
2.txt
[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.tar  2.txt  adai  adai.tar  adai.zip
[root@adai002 d6z]# du -ah
1.3M	./2.txt
1.3M	./adai/1.txt
1.3M	./adai/2.txt
2.5M	./adai
328K	./1.txt.zip
656K	./adai.zip
1.3M	./1.txt
2.5M	./adai.tar
1.3M	./2.tar
9.5M	.

eg3: 同时打包目录文件和普通文件

[root@adai002 d6z]# tar -cvf adailinux.tar adai 1.txt 2.txt
adai/
adai/1.txt
adai/2.txt
1.txt
2.txt
[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.tar  2.txt  adai  adailinux.tar  adai.tar  adai.zip
[root@adai002 d6z]# du -ah
1.3M	./2.txt
1.3M	./adai/1.txt
1.3M	./adai/2.txt
2.5M	./adai
328K	./1.txt.zip
656K	./adai.zip
1.3M	./1.txt
2.5M	./adai.tar
1.3M	./2.tar
4.9M	./adailinux.tar
15M	.

查看包内内容

[root@adai002 d6z]# tar -tf adai.tar
adai/
adai/1.txt
adai/2.txt

选择性打包(--exclude)

[root@adai002 d6z]# ls adai
1.txt  2.txt  3.txt  adailinux.tar  adai.tar
[root@adai002 d6z]# tar -cvf adai.tar adai --exclude "*.txt"
打包除了“.txt”以外的文件
adai/
adai/adailinux.tar
adai/adai.tar
[root@adai002 d6z]# tar -cvf adai2.tar adai --exclude 1.txt --exclude adai.tar 
打包除了1.txt和adai.tar以外的文件
adai/
adai/2.txt
adai/adailinux.tar
adai/3.txt
[root@adai002 d6z]# tar -tf adai2.tar
adai/
adai/2.txt
adai/adailinux.tar
adai/3.txt

解包

[root@adai002 d6z]# tar -xvf adai.tar
adai/
adai/1.txt
adai/2.txt
[root@adai002 d6z]# ls
1.txt  1.txt.zip  2.txt  adai  adai.tar  adai.zip
[root@adai002 d6z]# du -ah
1.3M	./2.txt
1.3M	./adai/1.txt
1.3M	./adai/2.txt
2.5M	./adai
328K	./1.txt.zip
656K	./adai.zip
1.3M	./1.txt
2.5M	./adai.tar
8.3M	.

说明: 解包时,如果该文件已经存在则会直接覆盖,无任何提示。

6.7 打包并压缩

语法: tar [options] [filename] options: -z:同时用gzip压缩 -j:同时用bzip2压缩 -J:同时用xz压缩

打包并压缩

eg1: 打包并用gzip压缩

[root@adai002 d6z]# du -h adai
9.8M	adai
[root@adai002 d6z]# tar -czvf adai.tar.gz adai
adai/
adai/1.txt
adai/2.txt
adai/adailinux.tar
adai/adai.tar
adai/3.txt
[root@adai002 d6z]# du -h adai.tar.gz
2.6M	adai.tar.gz

eg2: 打包并用bzip2压缩

[root@adai002 d6z]# tar -cjvf adai.tar.bz2 adai
adai/
adai/1.txt
adai/2.txt
adai/adailinux.tar
adai/adai.tar
adai/3.txt
[root@adai002 d6z]# du -h adai.tar.bz2
972K	adai.tar.bz2

eg3: 打包并用xz压缩

[root@adai002 d6z]# tar -cJvf adai.tar.xz adai
adai/
adai/1.txt
adai/2.txt
adai/adailinux.tar
adai/adai.tar
adai/3.txt
[root@adai002 d6z]# du -h adai.tar.xz
64K	adai.tar.xz

解包解压缩

示例:解包并解压xz格式的包

[root@adai002 d6z]# tar -xJvf adai.tar.xz
adai/
adai/1.txt
adai/2.txt
adai/adailinux.tar
adai/adai.tar
adai/3.txt

说明: 其余两种压缩格式同理。

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/06/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第六章 文件压缩和打包
  • 6.5 zip压缩工具
    • 压缩
      • 压缩普通文件
      • 压缩目录文件
    • 解压缩
      • 查看压缩文件
        • 6.6 tar打包
          • 打包
          • 查看包内内容
          • 选择性打包(--exclude)
          • 解包
        • 6.7 打包并压缩
          • 打包并压缩
          • 解包解压缩
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档