文件压缩打包

Posted 0_o

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件压缩打包相关的知识,希望对你有一定的参考价值。

[TOC]

压缩包格式


windows:
.zip
.tar
.tar.gz
.gz
------------
.rar
.7z
.bz
.bz2
.xz


为什么使用压缩

1,文件或目录太大,需要压缩传送
2,以后学的服务安装包都需要解压


压缩格式及命令

格式

linux命令

.zip

zip

.gz

gzip

.tar

tar

.tar.gz

tar,gzip


压缩命令_gzip


# 1,安装gzip命令
yum install -y gzip

# 2,gzip命令使用
gzip 普通文件名

# 选项
-r:递归压缩

# 特性:
1,压缩文件后,源文件不存在
2,只能压缩文件,不能压缩目录
3,压缩后,压缩文件在源文件的目录下
4,压缩后可直接查看文件内容zcat
5,一个压缩包中,只会有一个文件
6,解压后,压缩包没了,只剩源文件

# 3,解压命令
gzip -d 压缩包名

# 查看文件格式
file 文件名


举例
[root@zxw <sub>]# ll
total 4
-rw-r--r--. 1 root root 28 Apr 18 16:28 1.txt.gz
[root@zxw </sub>]# cat 1.txt.gz
C!]b1.txtK₡뛂[root@zxw <sub>]# (它是个压缩包,所以直接查看不了)
[root@zxw </sub>]# zcat 1.txt.gz
a


[root@zxw <sub>]# ll
total 4
-rw-r--r--. 1 root root 4 Apr 18 16:13 1.txt
[root@zxw </sub>]# gzip 1.txt
[root@zxw <sub>]# ll
total 4
-rw-r--r--. 1 root root 30 Apr 18 16:13 1.txt.gz
[root@zxw </sub>]# cat 1.txt.gz °]b1.txt34[root@zxw <sub>]#
[root@zxw </sub>]# gzip -d 1.txt.gz
[root@zxw <sub>]# ll
total 4
-rw-r--r--. 1 root root 4 Apr 18 16:13 1.txt
[root@zxw </sub>]# cat 1.txt
123


[root@zxw <sub>]# ll
total 8
-rw-r--r--. 1 root root 30 Apr 18 16:13 1.txt.gz
-rw-r--r--. 1 root root 2 Apr 18 16:18 2.txt.gz
[root@zxw </sub>]# cat 1.txt.gz °]b1.txt34[root@zxw <sub>]#
[root@zxw </sub>]# cat 2.txt.gz
b
[root@zxw <sub>]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Apr 18 16:13:36 2022
[root@zxw </sub>]# file 2.txt.gz
2.txt.gz: ASCII text(它的格式是text所以可以直接查看内容)


[root@zxw <sub>]# ll
total 16
-rw-r--r--. 1 root root 2 Apr 18 16:21 1.txt
-rw-r--r--. 1 root root 2 Apr 18 16:21 2.txt
-rw-r--r--. 1 root root 2 Apr 18 16:21 3.txt
-rw-r--r--. 1 root root 2 Apr 18 16:21 4.txt
[root@zxw </sub>]# gzip -r /root/
[root@zxw ~]# ll
total 16
-rw-r--r--. 1 root root 28 Apr 18 16:21 1.txt.gz
-rw-r--r--. 1 root root 28 Apr 18 16:21 2.txt.gz
-rw-r--r--. 1 root root 28 Apr 18 16:21 3.txt.gz
-rw-r--r--. 1 root root 28 Apr 18 16:21 4.txt.gz
# 虽然-r是递归压缩后面写的也是目录,但是它的特性的所有条件都要满足、所以是一个文件一个压缩包)


压缩命令_zip


# 1,安装zip和unzip命令
yum install -y zip
yum install -y unzip

# 2,zip命令
zip 压缩包 文件名

举例
压缩包名 需要放入压缩包的文件
[root@zxw <sub>]# zip xiangzi.zip 1.txt 2.txt 3.txt

# 特性
1,压缩文件后,源文件存在
2,可以指定压缩后保存的路径
3,可以压缩目录,也可以压缩文件,也可以指定多个文件一起压缩
4,压缩目录需要加选项,如果不加,压缩后只会有一个空目录,里面没有文件
5,解压后,压缩包不会消失,如果同一目录下出现同名文件则会询问是否覆盖

# 压缩并指定位置
举例
[root@zxw </sub>]# zip /zxw/xiangzi.zip 1.txt 2.txt 3.txt
adding: 1.txt (stored 0%)
adding: 2.txt (stored 0%)
adding: 3.txt (stored 0%)
[root@zxw <sub>]# ll /zxw
total 4
-rw-r--r--. 1 root root 442 Apr 18 16:55 xiangzi.zip

# 选项
-r:递归压缩,包括目录下的所有文件
举例
[root@zxw </sub>]# ll
total 12
-rw-r--r--. 1 root root 2 Apr 18 17:20 1.txt
-rw-r--r--. 1 root root 2 Apr 18 17:20 2.txt
-rw-r--r--. 1 root root 2 Apr 18 17:20 3.txt
[root@zxw <sub>]# zip -r box.zip ./
adding: .bash_logout.gz (stored 0%)
adding: .bash_profile.gz (stored 0%)
adding: .bashrc.gz (stored 0%)
adding: .cshrc.gz (stored 0%)
adding: .tcshrc.gz (stored 0%)
adding: .bash_history.gz (stored 0%)
adding: 1.txt (stored 0%)
adding: 2.txt (stored 0%)
adding: 3.txt (stored 0%)
[root@zxw </sub>]# ll
total 20
-rw-r--r--. 1 root root 2 Apr 18 17:20 1.txt
-rw-r--r--. 1 root root 2 Apr 18 17:20 2.txt
-rw-r--r--. 1 root root 2 Apr 18 17:20 3.txt
-rw-r--r--. 1 root root 4587 Apr 18 17:32 box.zip

# 压缩目录需要加选项-r,如果不加,压缩后只会有一个空目录,里面没有文件
[root@zxw <sub>]# ll zxw/
total 12
-rw-r--r--. 1 root root 2 Apr 18 17:49 1.txt
-rw-r--r--. 1 root root 2 Apr 18 文件压缩打包

文件压缩打包

文件压缩打包

文件的压缩打包

Windows面试题总结

Linux打包和压缩