Linux常用命令-zip

命令

zip

描述

package and compress (archive) files
zip压缩工具

用法

1
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Options:
-f freshen: only changed files
-d 从zip包中删除文件
-r 递归目录
-0 只打包不压缩
-1 快速压缩,最低压缩级别
-q 静默操作
-c 添加一行注释
-@ 从标准输入读取文件名
-x 排除文件名
-F 修复zip包 (-FF 深层修复)
-A 调整自解压exe文件包
-T 测试压缩文件的完整性
-y 保留软链接而不是原文件
-e 加密,交互式输入密码
-P 设置密码,非交互式
-h2 显示更多帮助
-u update: only changed or new files
-m 将文件移动到zip包中并删除原文件
-j junk (don't record) directory names
-l convert LF to CR LF (-ll CR LF to LF)
-9 最高压缩级别
-v 显示详情
-z 添加注释
-o make zipfile as old as latest entry
-i 包含文件名
-D do not add directory entries
-J junk zipfile prefix (unzipsfx)
-X eXclude eXtra file attributes
-n don't compress these suffixes

注意

zip默认打包后不会删除原文件,-m选项删除原文件

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$ zip -v
This is Zip 3.0 (July 5th 2008)

$ ls
a1 a2 b1 b2 dir1/
# zip命令需要指定zip包名,如下打包a1和a2到a.zip
$ zip a.zip a1 a2
adding: a1 (stored 0%)
adding: a2 (stored 0%)
$ ls
a1 a2 a.zip b1 b2 dir1/
$ echo cc >> a1
# -f 刷新zip包中的文件,即同步本地文件变更到zip包中
$ zip -f a.zip
freshening: a1 (stored 0%)
$ unzip a.zip -d /tmp/
Archive: a.zip
extracting: /tmp/a1
extracting: /tmp/a2
$ more /tmp/a1
aa
cc
$ echo cc > a3
# -u 刷新zip包中的变更文件和新增文件
$ zip -u a.zip a*
adding: a3 (stored 0%)
# zipinfo命令查看zip包文件信息
$ zipinfo a.zip
Archive: a.zip
Zip file size: 431 bytes, number of entries: 3
-rw-r--r-- 3.0 unx 6 tx stor 21-Oct-20 23:42 a1
-rw-r--r-- 3.0 unx 4 tx stor 21-Oct-20 22:11 a2
-rw-r--r-- 3.0 unx 3 tx stor 21-Oct-20 23:43 a3
3 files, 13 bytes uncompressed, 13 bytes compressed: 0.0%
$ rm a1 a2 a3
# unzip命令解压文件
$ unzip a.zip
Archive: a.zip
extracting: a1
extracting: a2
extracting: a3
# -r 递归打包目录
$ zip -r dir1.zip dir1/
adding: dir1/ (stored 0%)
adding: dir1/a1 (stored 0%)
adding: dir1/a2 (stored 0%)
adding: dir1/d1/ (stored 0%)
adding: dir1/d1/b1 (stored 0%)
adding: dir1/d1/b2 (stored 0%)
$ zipinfo dir1.zip
Archive: dir1.zip
Zip file size: 898 bytes, number of entries: 6
drwxr-xr-x 3.0 unx 0 bx stor 21-Oct-20 22:11 dir1/
-rw-r--r-- 3.0 unx 3 tx stor 21-Oct-20 22:11 dir1/a1
-rw-r--r-- 3.0 unx 4 tx stor 21-Oct-20 22:11 dir1/a2
drwxr-xr-x 3.0 unx 0 bx stor 21-Oct-20 22:11 dir1/d1/
-rw-r--r-- 3.0 unx 3 tx stor 21-Oct-20 22:11 dir1/d1/b1
-rw-r--r-- 3.0 unx 4 tx stor 21-Oct-20 22:11 dir1/d1/b2
6 files, 14 bytes uncompressed, 14 bytes compressed: 0.0%
# -d 从zip包中删除文件
$ zip -d a.zip a1
deleting: a1
$ zipinfo a.zip
Archive: a.zip
Zip file size: 158 bytes, number of entries: 1
-rw-r--r-- 3.0 unx 4 tx stor 21-Oct-20 22:11 a2
1 file, 4 bytes uncompressed, 4 bytes compressed: 0.0%
# -x 排除文件
$ zip ab.zip a* b* -x b1
adding: a1 (stored 0%)
adding: a2 (stored 0%)
adding: b2 (stored 0%)
# -e 输入密码
$ zip -e a1.zip a1
Enter password:
Verify password:
adding: a1 (stored 0%)
# -P 设置密码
$ zip -P a a2.zip a2
adding: a2 (stored 0%)
# 需要输入密码才能解压
$ unzip a1.zip
Archive: a1.zip
[a1.zip] a1 password:
extracting: a1
# 也可以直接指定解压密码
$ unzip -P a a2.zip
Archive: a2.zip
extracting: a2
# -m 打包后删除原文件
$ ls
a1 a2 b1 b2 dir1/ dir1.zip
$ zip a1.zip -m a1
adding: a1 (stored 0%)
$ ls
a1.zip a2 b1 b2 dir1/ dir1.zip