在Shell脚本中为zip压缩包添加注释(MD5)

在shell中使用zip工具打包文件,并希望将文件的MD5值添加到zip压缩包的注释中。

1. 终端中为zip压缩包添加注释

1.1. 查看需要被压缩的文件

终端使用 fonts-powerline 字体,在此处无法正常显示(详情见 linux 安装并配置zsh)。

 pi@ubuntu  ~/project/zip_md5  ls -al
total 16
drwxrwxr-x 2 pi pi 4096 Jun 10 23:24 .
drwxrwxr-x 5 pi pi 4096 Jun 10 23:23 ..
-rwxrwxr-x 1 pi pi 5949 Jun  2 20:54 hello-rpi

1.2. 计算MD5值,并保存到文件中

 pi@ubuntu  ~/project/zip_md5  md5sum hello-rpi   
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi
 pi@ubuntu  ~/project/zip_md5  md5sum hello-rpi > hello-rpi.md5
 pi@ubuntu  ~/project/zip_md5  cat hello-rpi.md5            
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

 1.3. 使用zip工具制作压缩包

在命令中加上 -z 选项(”-z   add zipfile comment “);压缩过程中,会提示输入注释,就将生成的MD5值输入(注释结束的方式为:换行,单独输入“.”)。

 pi@ubuntu  ~/project/zip_md5  zip hello-rpi.zip hello-rpi hello-rpi.md5 -z
  adding: hello-rpi (deflated 59%)
  adding: hello-rpi.md5 (stored 0%)
enter new zip file comment (end with .):
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi
.
 pi@ubuntu  ~/project/zip_md5  ls -al hello-rpi.zip 
-rw-rw-r-- 1 pi pi 2877 Jun 10 23:34 hello-rpi.zip

1.4. 查看生成的压缩包及注释

 pi@ubuntu  ~/project/zip_md5  zipnote hello-rpi.zip 
@ hello-rpi
@ (comment above this line)
@ hello-rpi.md5
@ (comment above this line)
@ (zip file comment below this line)
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

 

2. shell脚本中为zip压缩包添加注释

2.1. 创建脚本do_zipm

 pi@ubuntu  ~/project/zip_md5  vi do_zipm 

将如下代码输入,并:wq保存。

#!/bin/bash
file_name
=$1 md5=$(md5sum ${file_name}) echo "${md5}" > ${file_name}.md5 zip -q ${file_name}.zip ${file_name} ${file_name}.md5 -z << EOF ${md5} . EOF

2.2. 修改权限,执行脚本

 pi@ubuntu  ~/project/zip_md5  chmod a+x do_zipm 
 pi@ubuntu  ~/project/zip_md5  ls -al do_zipm      
-rwxrwxr-x 1 pi pi 163 Jun 11 00:24 do_zipm
 pi@ubuntu  ~/project/zip_md5  ./do_zipm hello-rpi
 pi@ubuntu  ~/project/zip_md5  ls -al        
total 28
drwxrwxr-x 2 pi pi 4096 Jun 11 00:38 .
drwxrwxr-x 5 pi pi 4096 Jun 10 23:23 ..
-rwxrwxr-x 1 pi pi  163 Jun 11 00:24 do_zipm
-rwxrwxr-x 1 pi pi 5949 Jun  2 20:54 hello-rpi
-rw-rw-r-- 1 pi pi   44 Jun 11 00:38 hello-rpi.md5
-rw-rw-r-- 1 pi pi 2877 Jun 11 00:38 hello-rpi.zip

2.3. 查看注释

 pi@ubuntu  ~/project/zip_md5  zipnote hello-rpi.zip
@ hello-rpi
@ (comment above this line)
@ hello-rpi.md5
@ (comment above this line)
@ (zip file comment below this line)
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

3. 验证MD5

3.1. Raspbian

pi@raspberrypi:~/project/zip_md5 $ unzip hello-rpi.zip
Archive:  hello-rpi.zip
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi
  inflating: hello-rpi
 extracting: hello-rpi.md5
pi@raspberrypi:~/project/zip_md5 $ ls -al
总用量 24
drwxr-xr-x 2 pi pi 4096 6月  11 00:30 .
drwxr-xr-x 5 pi pi 4096 6月  11 00:27 ..
-rwxrwxr-x 1 pi pi 5949 6月   2 20:54 hello-rpi
-rw-rw-r-- 1 pi pi   44 6月  11 00:24 hello-rpi.md5
-rw-r--r-- 1 pi pi 2877 6月  11 00:30 hello-rpi.zip
pi@raspberrypi:~/project/zip_md5 $ md5sum -c hello-rpi.md5
hello-rpi: 成功

3.2. Windows

PS D:\Share\hello-rpi> certutil.exe -hashfile .\hello-rpi md5
MD5 的 .\hello-rpi 哈希:
8417cd4ae26ea57d1c9bf6145ccc4202
CertUtil: -hashfile 命令成功完成。
PS D:\Share\hello-rpi> & type .\hello-rpi.md5
8417cd4ae26ea57d1c9bf6145ccc4202  hello-rpi

 

posted @ 2020-06-11 00:36  袁克波  阅读(1957)  评论(0编辑  收藏  举报