您的位置:首页 > 游戏 > 手游 > Linux之文件打包,压缩,解压

Linux之文件打包,压缩,解压

2024/10/6 14:37:17 来源:https://blog.csdn.net/qq_41124528/article/details/139482311  浏览:    关键词:Linux之文件打包,压缩,解压

打包和压缩

Linux中对文件进行打包,压缩有两种命令

zip:将文件进行压缩

tar:将文件进行打包(通过和其他命令结合,也能实现压缩的功能)

1、tar打包命令

在Linux中,tar命令是一个常用的工具,用于打包和解压文件。它在文件管理、备份和压缩方面扮演着重要角色。tar(tape archive)最初是为磁带设备设计的,但现在已经成为文件操作的标准工具之一。它能够将一组文件和目录打包成单个归档文件,也可以从归档文件中提取出文件和目录。通过结合不同的选项,你可以在tar命令中实现广泛的功能。

tar [选项] file.tar [file]

1.1、常用参数

参数功能
-c创建新的归档文件(打包)
-x从归档文件中提取文件(解包)
-f <文件名>指定归档名
-v显示操作的详细信息
-z通过gzip的方式压缩归档文件
-j通过bzip2的方式压缩归档文件
-C指定目录

 

1.2 常规操作

# 将file1的文件打包成一个文件
tar -cvf file.tar file1# 将aa目录打包
tar -cvf aa.tar aa/# 将aa目录打包并通过gzip的方式压缩
tar -zcvf aa.tar.gz aa/#查看归档文件中的内容
tar -tvf aa.tar# 将aa.tar解压到当前路径
tar -xvf aa.tar# 将aa.tar解压到指定目录
tar -xvf aa.tar -C bb/

2、 zip压缩命令

zip file.zip [选项] 文件

2.1、 常用参数

参数功能
-r压缩文件夹
-q压缩文件时不显示压缩过程的详细信息。
-d从压缩文件中删除指定文件
-u 用于更新现有的ZIP文件,将新的文件或修改后的文件添加到ZIP存档中。
-f用于刷新(更新)现有ZIP文件中的指定文件。
-m用于移动(归档)文件到一个ZIP压缩文件中,并在移动后将源文件删除。
-e用于对ZIP文件进行加密。
-z为压缩文件添加注释。

2.2、常规操作

# 压缩某个文件
[root@192 ~]# zip aa.zip aa.taradding: aa.tar (deflated 97%)
# 压缩某个目录
[root@192 ~]# zip aa.zip -r aa/adding: aa/ (stored 0%)adding: aa/file1 (deflated 85%)adding: aa/file5 (deflated 82%)adding: aa/file2 (deflated 82%)adding: aa/file.tar (deflated 96%)
# 静默压缩
[root@192 ~]# zip aa.zip -q -r aa/
[root@192 ~]# # 从压缩文件中删除某个文件
[root@192 aa]# zip file.zip -d file5
deleting: file5# 往压缩文件中更新某个文件
[root@192 aa]# zip file.zip -u file5adding: file5 (deflated 82%)# 往压缩文件中更新某个文件
[root@192 aa]# zip file.zip -f file5 
freshening: file5 (deflated 71%)# 将某个文件添加到压缩文件中,并删除源文件
[root@192 aa]# zip file.zip -m file6adding: file6 (stored 0%)
[root@192 aa]# ll
总用量 16
-rw-r--r--. 1 root root 1901 6月   5 21:56 file1
-rw-r--r--. 1 root root   74 6月   5 21:51 file2
-rw-r--r--. 1 root root   86 6月   5 22:49 file5
-rw-r--r--. 1 root root 1400 6月   5 22:50 file.zip# 压缩某个文件并进行加密
[root@192 aa]# zip test.zip file* -e
Enter password: 
Verify password: adding: file1 (deflated 85%)adding: file2 (deflated 82%)adding: file5 (deflated 71%)# 压缩某个文件并添加注释
[root@192 aa]# zip test.zip file* -zadding: file1 (deflated 85%)adding: file2 (deflated 82%)adding: file5 (deflated 71%)
enter new zip file comment (end with .):
zheshi^H^H
.

3、unzip解压命令

unzip是用来对zip文件进行解压的命令

unzip [选项] 压缩包名 源文件或源目录列表

3.1、 常用参数

参数功能
-l显示压缩文件内所包含的文件
-t检查压缩文件是否正确
-v执行时显示详细的信息
-z仅显示压缩文件的备注文字
-n解压缩时不要覆盖原有的文件
-P<密码>使用zip的密码选项

-q

执行时不显示任何信息
-d<目录>指定文件解压缩后所要存储的目录

3.2、常规操作

# 显示压缩包中的文件,但是不做解压
[root@192 aa]# unzip -l t.zip 
Archive:  t.zipLength      Date    Time    Name
---------  ---------- -----   ----0  06-05-2024 23:33   test/1901  06-05-2024 21:56   test/file174  06-05-2024 21:51   test/file286  06-05-2024 22:49   test/file5
---------                     -------2061                     4 files# 解压文件压缩包到指定目录(如果目录不存在,则会创建一个目录)
[root@192 aa]# unzip  t.zip -d test/
Archive:  t.zipinflating: test/file1              inflating: test/file2              inflating: test/file5# 解压路径压缩包到指定目录(如果目录不存在,则会创建一个目录)
先创建一个压缩包(相对路径)
[root@192 aa]# zip t.zip -r test/adding: test/ (stored 0%)adding: test/file1 (deflated 85%)adding: test/file2 (deflated 82%)adding: test/file5 (deflated 71%)[root@192 aa]# ll
总用量 16
-rw-r--r--. 1 root root 1901 6月   5 21:56 file1
-rw-r--r--. 1 root root   74 6月   5 21:51 file2
-rw-r--r--. 1 root root   86 6月   5 22:49 file5
drwxr-xr-x. 2 root root   45 6月   5 23:33 test
-rw-r--r--. 1 root root  918 6月   5 23:35 t.zip# 解压到指定目录
[root@192 aa]# unzip t.zip -d a/
Archive:  t.zipcreating: a/test/inflating: a/test/file1            inflating: a/test/file2            inflating: a/test/file5            
[root@192 aa]# ll
总用量 16
drwxr-xr-x. 3 root root   18 6月   5 23:35 a
-rw-r--r--. 1 root root 1901 6月   5 21:56 file1
-rw-r--r--. 1 root root   74 6月   5 21:51 file2
-rw-r--r--. 1 root root   86 6月   5 22:49 file5
drwxr-xr-x. 2 root root   45 6月   5 23:33 test
-rw-r--r--. 1 root root  918 6月   5 23:35 t.zip
可以看到在a目录下,又创建了test目录
[root@192 aa]# ll a
总用量 0
drwxr-xr-x. 2 root root 45 6月   5 23:33 test# 测试压缩文件的有效性
[root@192 aa]# unzip -t t.zip 
Archive:  t.ziptesting: test/                    OKtesting: test/file1               OKtesting: test/file2               OKtesting: test/file5               OK
No errors detected in compressed data of t.zip.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com