文件压缩打包

Posted wx622ea1d31aab0

tags:

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

[TOC]


文件压缩打包


压缩包格式


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

为什么使用压缩

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

压缩格式及命令

格式

linux命令

.zip

zip

.gz

gzip

.tar

tar

.tar.gz

tar.gzip





压缩命令-gzip

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

#2.gzip命令
gzip 普通文件名
-r:递归压缩
-d:解压缩

#栗子
-r:递归压缩
007:53:45 root@miaosen,10.0.0.100:<sub> # gzip -r 1txt 2txt 3txt 4txt
007:54:03 root@miaosen,10.0.0.100:</sub> # ll
total 20
-rw-r--r-- 1 root root 25 Apr 20 02:08 1txt.gz
-rw-r--r-- 1 root root 25 Apr 20 02:08 2txt.gz
-rw-r--r-- 1 root root 25 Apr 20 02:08 3txt.gz
-rw-r--r-- 1 root root 25 Apr 20 02:08 4txt.gz

-d:解压缩
007:54:06 root@miaosen,10.0.0.100:<sub> # gzip -d 1txt.gz 2txt.gz 3txt.gz 4txt.gz
007:54:52 root@miaosen,10.0.0.100:</sub> # ll
total 4
-rw-r--r-- 1 root root 0 Apr 20 02:08 1txt
-rw-r--r-- 1 root root 0 Apr 20 02:08 2txt
-rw-r--r-- 1 root root 0 Apr 20 02:08 3txt
-rw-r--r-- 1 root root 0 Apr 20 02:08 4txt

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

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

压缩命令 -zip

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

# 2.zip命令
zip 压缩包名 文件
压缩包名 需要放入压缩包的文件
007:55:27 root@miaosen,10.0.0.100:<sub> # zip 00.zip 1txt 2txt 3txt 4txt
adding: 1txt (stored 0%)
adding: 2txt (stored 0%)
adding: 3txt (stored 0%)
adding: 4txt (stored 0%)
#压缩并指定位置 (一定要指定一个名称)
008:06:01 root@miaosen,10.0.0.100:</sub> # zip /tmp/0.zip 1txt 2txt
adding: 1txt (stored 0%)
adding: 2txt (stored 0%)
008:06:11 root@miaosen,10.0.0.100:<sub> # ll /tmp/
total 4
-rw-r--r-- 1 root root 294 Apr 20 08:06 0.zip

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

# 选项
-r:递归压缩,包括目录下的所有文件
-l:查看压缩包里面有哪些文件
-d:指定解压路径
008:40:23 root@miaosen,10.0.0.100:</sub> # unzip 00.zip -d /tmp/
Archive: 00.zip
extracting: /tmp/1txt
extracting: /tmp/2txt
extracting: /tmp/3txt
extracting: /tmp/4txt

-l:查看压缩包里面有哪些文件
1208:33:27 root@miaosen,10.0.0.100:~ # unzip -l 00.zip
Archive: 00.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-20-2022 02:08 1txt
0 04-20-2022 02:08 2txt
0 04-20-2022 02:08 3txt
0 04-20-2022 02:08 4txt
--------- -------
0 4 files

-r:递归压缩,包括目录下的所有文件

1208:13:09 root@miaosen,10.0.0.100:/ # zip -r /tmp/root.zip root

## 解压命令
unzip 压缩报名
008:15:12 root@miaosen,10.0.0.100:/tmp # unzip root.zip
Archive: root.zip
creating: root/
extracting: root/.bash_logout
inflating: root/.cshrc
inflating: root/.tcshrc
inflating: root/.bash_history
extracting: root/.lesshst
inflating: root/.bashrc
inflating: root/.bash_profile
inflating: root/a.txt
inflating: root/.viminfo
extracting: root/1txt
extracting: root/2txt
extracting: root/3txt
extracting: root/4txt
extracting: root/00.zip

压缩命令-tar


#语法:命令  选项  压缩包名  文件
tar命令本身是归档

## 选项
c:归档
f:指定包名
z:使用gzip把归档文件压缩
v:显示压缩/解压的过程
x:解压归档文件
C:指定解压的位置(路径)
t:查看压缩包的文件都有哪些
j:使用bzip2压缩文件
J:压缩成.xz包
X:排除指定的文件
--exclude:排除指定文件

## zcf举例
009:28:53 root@miaosen,10.0.0.100:<sub> # tar zcf sj.tgz /etc/passwd
tar: Removing leading `/ from member names

## tf查看压缩包的文件都有哪些
0 ✓ 09:29:48 root@miaosen,10.0.0.100:</sub> # tar tf sj.tgz
etc/passwd

## 解压归档文件
0 ✓ 09:30:43 root@miaosen,10.0.0.100:<sub> # tar xf sj.tgz
0 ✓ 09:31:35 root@miaosen,10.0.0.100:</sub> # ll
total 4
drwxr-xr-x 2 root root 20 Apr 20 09:31 etc
-rw-r--r-- 1 root root 714 Apr 20 09:29 sj.tgz

## X排除指定的文件
0 ✓ 09:33:45 root@miaosen,10.0.0.100:<sub> # cat >>1.txt<<EOF
> /etc/yum.repos.d
> /etc/yum.conf
> /etc/yum
> /etc/xinetd.d
> EOF
## 上列都是/etc的文件使用-X 1.txt 可以排除这些文件被打包压缩
语法: tar zcf sj.tgz -X 1.txt /etc

# --exclude:排除指定文件
#只能指定单个文件,多个需要重复操作--exclude=文件名
2 ✗ 09:47:01 root@miaosen,10.0.0.100:</sub> # tar zcf qwe.gzp --exclude=/etc/yum.repos.d /etc

# 3.压缩后,压缩包的位置可以指定任意目录
0 ✓ 09:47:14 root@miaosen,10.0.0.100:<sub> # tar zcf /tmp/sj.gzp /etc #指定在/tmp下
tar: Removing leading `/ from member names
ll 0 ✓ 09:50:49 root@miaosen,10.0.0.100:</sub> # ll /tmp/
total 9924
-rw-r--r-- 1 root root 10158670 Apr 20 09:50 sj.gzp

##特性
1.压缩文件后,源文件存在
2.目录和文件都可以压缩
3.压缩后,压缩包的位置可以指定任意目录
4.可以查看压缩包里有哪些文件,但是查看不了文件内容
5.一个压缩包中,可以有多个文件或目录
6.解压后,压缩包还在,源文件也可以随意指定路径 -C
7.使用zcf压缩,zxf解压
使用jcf压缩,jxf解压
万能解压命令:xf

## 注意:
1.tar命令在解压开文件时,如果有文件名冲突,则不会询问,直接覆盖
2.tar命令,在打包时,会自动删除绝对路径"/"
3.以后打包,尽量使用相对路径,cd到需要打包目录或文件的上级目录

以上是关于文件压缩打包的主要内容,如果未能解决你的问题,请参考以下文章

文件压缩打包

文件压缩打包

文件压缩打包

文件的压缩打包

Windows面试题总结

Linux打包和压缩