linux环境中通过7-zip软件进行文件的压缩和加密的方法

1、概述

7-zip软件是一个开源的软件,用于对文件进行归档、压缩。并且可以对文件进行加密。

OK,话不多说,咱们通过几个例子,来说明通过7zip进行压缩、加密、解压缩的过程

2、安装软件

通过以下的命令进行安装

rpm -ivh http://172.20.56.117/rpms/p7zip-16.02-20.el7.x86_64.rpm

或者

yum install -y p7zip

3、压缩

通过以下的命令进行压缩

7za a -tzip prac.zip practises/

执行过程:

[root@nctest-snap-test-02 data]# 7za a -tzip prac.zip practises/

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz (406F1),ASM,AES-NI)

Open archive: prac.zip
--
Path = prac.zip
Type = zip
Physical Size = 9350

Scanning the drive:
4 folders, 17 files, 481712 bytes (471 KiB)

Updating archive: prac.zip

Items to compress: 21

    
Files read from disk: 17
Archive size: 9350 bytes (10 KiB)
Everything is Ok

说明:

a 是参数

-tzip 指定格式

prac.zip是打包之后的文件名

practises/是要进行压缩的目录或者文件

查看归档、压缩前后的文件大小

[root@nctest-snap-test-02 data]# du -sh prac.zip 
12K	prac.zip
[root@nctest-snap-test-02 data]# du -sh practises/
508K	practises/
[root@nctest-snap-test-02 data]# 

4、解压缩

通过以下的命令进行解压缩

 7za x prac.zip

执行过程:

[root@nctest-snap-test-02 2]# 7za x prac.zip 

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz (406F1),ASM,AES-NI)

Scanning the drive for archives:
1 file, 9350 bytes (10 KiB)

Extracting archive: prac.zip
--
Path = prac.zip
Type = zip
Physical Size = 9350

Everything is Ok

Folders: 4
Files: 17
Size:       481712
Compressed: 9350

查看解压缩后的文件

[root@nctest-snap-test-02 2]# ls
practises  prac.zip
[root@nctest-snap-test-02 2]# ls -l practises/
total 524
lrwxrwxrwx 1 root root      5 Apr 15 15:28 a.py -> if.py
-rwxr-xr-x 1 root root     51 Dec  9 10:12 bash.sh
-rw-r--r-- 1 root root    414 Dec 17 16:14 cnn-web.py
-rwxr-xr-x 1 root root     43 Nov  1 18:48 hello.py
-rw-r--r-- 1 root root     67 Dec 23 09:45 if.py
-rw-r--r-- 1 root root  10821 Jan 13 14:13 initnginx.json
-rw-r--r-- 1 root root   7688 Jan 13 13:29 initnginx.yaml
-rw-r--r-- 1 root root      0 Jan 13 14:16 initnginx.yml
-rw------- 1 root root      0 Dec  9 10:14 nohup.out
-rw-r--r-- 1 root root     90 Nov 24 17:24 salary.txt
-rw-r--r-- 1 root root     77 Nov 26 15:20 salary.txt.sha256sum
-rw-r--r-- 1 root root     40 Dec  3 14:25 shell-set.sh
-rw-r--r-- 1 root root 462153 Dec  9 10:15 sleep.log
-rwxr-xr-x 1 root root     75 Dec  9 10:13 sleep.sh
drwxr-xr-x 2 root root   4096 Jan 10 10:23 test02
drwxr-xr-x 2 root root   4096 Jan 10 11:03 test03
drwxr--r-- 2 root root   4096 Jan 10 11:04 test04
-rwxr-xr-x 1 root root     94 Jan 11 16:42 testdir.sh
-rwxr-xr-x 1 root root     94 Jan 11 16:42 ts
[root@nctest-snap-test-02 2]# 

5、压缩 +密码

那,通过下面的方式,就可以在创建压缩包的时候,增加密码了。

7za a -padmin123 -tzip prac.zip practises/

-p密码 就是要使用的密码

执行过程

[root@nctest-snap-test-02 data]# 7za a -padmin123 -tzip prac.zip practises/

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz (406F1),ASM,AES-NI)

Scanning the drive:
4 folders, 17 files, 481712 bytes (471 KiB)

Creating archive: prac.zip

Items to compress: 21

    
Files read from disk: 17
Archive size: 9554 bytes (10 KiB)
Everything is Ok
[root@nctest-snap-test-02 data]# 

6、解压缩(使用密码)

  • 输入错误的密码进行解压

[root@nctest-snap-test-02 2]# 7za x prac.zip 

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz (406F1),ASM,AES-NI)

Scanning the drive for archives:
1 file, 9554 bytes (10 KiB)

Extracting archive: prac.zip
--
Path = prac.zip
Type = zip
Physical Size = 9554

    
Enter password (will not be echoed):
ERROR: Wrong password : practises/a.py
ERROR: Wrong password : practises/bash.sh
ERROR: Wrong password : practises/cnn-web.py
ERROR: Wrong password : practises/hello.py
ERROR: Wrong password : practises/if.py
ERROR: Wrong password : practises/initnginx.json
ERROR: Wrong password : practises/initnginx.yaml
ERROR: Wrong password : practises/initnginx.yml
ERROR: Wrong password : practises/nohup.out
ERROR: Wrong password : practises/salary.txt
ERROR: Wrong password : practises/salary.txt.sha256sum
ERROR: Wrong password : practises/shell-set.sh
ERROR: Wrong password : practises/sleep.log
ERROR: Wrong password : practises/sleep.sh
ERROR: Wrong password : practises/test02/a.log
ERROR: Wrong password : practises/testdir.sh
ERROR: Wrong password : practises/ts
                     
ERROR: E_FAIL

Archives with Errors: 1

Sub items Errors: 17
[root@nctest-snap-test-02 2]# ls -l practises/
total 12
-rw-r--r-- 1 root root    0 Jan 11 16:44 a.py
-rwxr-xr-x 1 root root    0 Dec  9 10:12 bash.sh
-rw-r--r-- 1 root root    0 Dec 17 16:14 cnn-web.py
-rwxr-xr-x 1 root root    0 Nov  1 18:48 hello.py
-rw-r--r-- 1 root root    0 Dec 23 09:45 if.py
-rw-r--r-- 1 root root    0 Jan 13 14:13 initnginx.json
-rw-r--r-- 1 root root    0 Jan 13 13:29 initnginx.yaml
-rw-r--r-- 1 root root    0 Jan 13 14:16 initnginx.yml
-rw------- 1 root root    0 Dec  9 10:14 nohup.out
-rw-r--r-- 1 root root    0 Nov 24 17:24 salary.txt
-rw-r--r-- 1 root root    0 Nov 26 15:20 salary.txt.sha256sum
-rw-r--r-- 1 root root    0 Dec  3 14:25 shell-set.sh
-rw-r--r-- 1 root root    0 Dec  9 10:15 sleep.log
-rwxr-xr-x 1 root root    0 Dec  9 10:13 sleep.sh
drwxr-xr-x 2 root root 4096 Jan 10 10:23 test02
drwxr-xr-x 2 root root 4096 Jan 10 11:03 test03
drwxr--r-- 2 root root 4096 Jan 10 11:04 test04
-rwxr-xr-x 1 root root    0 Jan 11 16:42 testdir.sh
-rwxr-xr-x 1 root root    0 Jan 11 16:42 ts
[root@nctest-snap-test-02 2]# 

这里面输入的是错误的密码,发现,是有报错的,并且解压出来的文件都是0字节的。

  • 使用正确的密码解压缩

[root@nctest-snap-test-02 2]# 7za x prac.zip 

7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,8 CPUs Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz (406F1),ASM,AES-NI)

Scanning the drive for archives:
1 file, 9554 bytes (10 KiB)

Extracting archive: prac.zip
--
Path = prac.zip
Type = zip
Physical Size = 9554

    
Enter password (will not be echoed):
Everything is Ok       

Folders: 4
Files: 17
Size:       481712
Compressed: 9554
[root@nctest-snap-test-02 2]# ls -l practises/
total 524
lrwxrwxrwx 1 root root      5 Apr 15 15:49 a.py -> if.py
-rwxr-xr-x 1 root root     51 Dec  9 10:12 bash.sh
-rw-r--r-- 1 root root    414 Dec 17 16:14 cnn-web.py
-rwxr-xr-x 1 root root     43 Nov  1 18:48 hello.py
-rw-r--r-- 1 root root     67 Dec 23 09:45 if.py
-rw-r--r-- 1 root root  10821 Jan 13 14:13 initnginx.json
-rw-r--r-- 1 root root   7688 Jan 13 13:29 initnginx.yaml
-rw-r--r-- 1 root root      0 Jan 13 14:16 initnginx.yml
-rw------- 1 root root      0 Dec  9 10:14 nohup.out
-rw-r--r-- 1 root root     90 Nov 24 17:24 salary.txt
-rw-r--r-- 1 root root     77 Nov 26 15:20 salary.txt.sha256sum
-rw-r--r-- 1 root root     40 Dec  3 14:25 shell-set.sh
-rw-r--r-- 1 root root 462153 Dec  9 10:15 sleep.log
-rwxr-xr-x 1 root root     75 Dec  9 10:13 sleep.sh
drwxr-xr-x 2 root root   4096 Jan 10 10:23 test02
drwxr-xr-x 2 root root   4096 Jan 10 11:03 test03
drwxr--r-- 2 root root   4096 Jan 10 11:04 test04
-rwxr-xr-x 1 root root     94 Jan 11 16:42 testdir.sh
-rwxr-xr-x 1 root root     94 Jan 11 16:42 ts
[root@nctest-snap-test-02 2]# 

这次是输入了正确的密码,然后解压成功了,各个文件的大小也都显示正常了,不是0字节了。

posted @ 2022-04-15 15:51  Zhai_David  阅读(1668)  评论(0编辑  收藏  举报