使用jar与zip压缩解压文件的区别

使用jar命令压缩和解压文件不会继承原来的权限,切记!
而使用zip/unzip压缩解压文件则会保留文件原来的权限等信息,因此使用压缩解压的时候尽量使用专业的工具

下面是测试内容和结果:
1.首先确认使用zip/unzip会继承文件原来的权限
[root@localhost ~]# umask
0022
[root@localhost ~]# mkdir tst
[root@localhost ~]# touch ./tst/aaa
[root@localhost ~]# touch ./tst/bbb
[root@localhost ~]# touch ./tst/ccc
[root@localhost ~]# ls -l ./tst
total 0
-rw-r--r-- 1 root root 0 Sep 25 00:59 aaa
-rw-r--r-- 1 root root 0 Sep 25 00:59 bbb
-rw-r--r-- 1 root root 0 Sep 25 00:59 ccc
[root@localhost ~]# chmod 700 ./tst/aaa
[root@localhost ~]# chmod 777 ./tst/bbb
[root@localhost ~]# ls -l ./tst                    -----修改三个文件的权限分别是700,777,644
total 0
-rwx------ 1 root root 0 Sep 25 00:59 aaa
-rwxrwxrwx 1 root root 0 Sep 25 00:59 bbb
-rw-r--r-- 1 root root 0 Sep 25 00:59 ccc

[root@localhost ~]# zip -r tst tst                 ------使用zip压缩一个包后,将原来目录改名
  adding: tst/ (stored 0%)
  adding: tst/bbb (stored 0%)
  adding: tst/aaa (stored 0%)
  adding: tst/ccc (stored 0%)
[root@localhost ~]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst
-rw-r--r-- 1 root root  512 Sep 25 01:00 tst.zip
[root@localhost ~]# mv tst tst_bak
[root@localhost ~]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst_bak
-rw-r--r-- 1 root root  512 Sep 25 01:00 tst.zip
[root@localhost ~]# unzip tst.zip                  ------使用unzip解压压缩包,查看权限没有改变
Archive:  tst.zip
   creating: tst/
extracting: tst/bbb                
extracting: tst/aaa                
extracting: tst/ccc                
[root@localhost ~]# ls -l tst
total 0
-rwx------ 1 root root 0 Sep 25 00:59 aaa
-rwxrwxrwx 1 root root 0 Sep 25 00:59 bbb
-rw-r--r-- 1 root root 0 Sep 25 00:59 ccc

2.使用上面的实验结果,测试jar解压文件后的变化
[root@localhost ~]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst_bak
-rw-r--r-- 1 root root  512 Sep 25 01:00 tst.zip
[root@localhost ~]# rm -fr tst                      ------删除掉原来的tst目录,然后使用jar解压原来zip压缩的文件(注意umask为022)
[root@localhost ~]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst_bak
-rw-r--r-- 1 root root  512 Sep 25 01:00 tst.zip
[root@localhost ~]# ls -l /home/oracle/oracle/product/11.2.0/dbhome_1/jdk/bin/jar
-rwxr-xr-x 1 oracle dba 62492 May  6  2009 /home/oracle/oracle/product/11.2.0/dbhome_1/jdk/bin/jar
[root@localhost ~]# export PATH=$PATH:/home/oracle/oracle/product/11.2.0/dbhome_1/jdk/bin/
[root@localhost ~]# which jar
/home/oracle/oracle/product/11.2.0/dbhome_1/jdk/bin/jar
[root@localhost ~]# jar xvf tst.zip
  created: tst/
extracted: tst/bbb
extracted: tst/aaa
extracted: tst/ccc
[root@localhost ~]# ls -l tst                      ------使用jar解压后,在umask为022的情况下,解压后的文件权限为644
total 0
-rw-r--r-- 1 root root 0 Sep 25 01:06 aaa
-rw-r--r-- 1 root root 0 Sep 25 01:06 bbb
-rw-r--r-- 1 root root 0 Sep 25 01:06 ccc
[root@localhost ~]# umask 0000                      ------设置umask为000后,解压后的文件权限为666
[root@localhost ~]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst_bak
-rw-r--r-- 1 root root  512 Sep 25 01:00 tst.zip
[root@localhost ~]# jar xvf tst.zip
  created: tst/
extracted: tst/bbb
extracted: tst/aaa
extracted: tst/ccc
[root@localhost ~]# ls -l tst
total 0
-rw-rw-rw- 1 root root 0 Sep 25 01:10 aaa
-rw-rw-rw- 1 root root 0 Sep 25 01:10 bbb
-rw-rw-rw- 1 root root 0 Sep 25 01:10 ccc
[root@localhost ~]#

因此可以得出,jar解压后文件的权限与用户当前的umask有关系,而与文件在原来压缩包中的权限没有关系。不会继承原来压缩包中的权限

3.测试jar压缩的时候会不会记录文件的权限
[root@localhost ~]# rm -fr tst tst.zip    ----删除原来的文件,然后设置umask为022,使用jar压缩带权限的文件,然后使用unzip解压
[root@localhost ~]# mv tst_bak tst
[root@localhost ~]# ls -l tst
total 0
-rwx------ 1 root root 0 Sep 25 00:59 aaa
-rwxrwxrwx 1 root root 0 Sep 25 00:59 bbb
-rw-r--r-- 1 root root 0 Sep 25 00:59 ccc
[root@localhost ~]# umask 022
[root@localhost ~]# jar cvf tst.zip tst
added manifest
adding: tst/(in = 0) (out= 0)(stored 0%)
adding: tst/bbb(in = 0) (out= 0)(stored 0%)
adding: tst/aaa(in = 0) (out= 0)(stored 0%)
adding: tst/ccc(in = 0) (out= 0)(stored 0%)
[root@localhost ~]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst
-rw-r--r-- 1 root root  699 Sep 25 01:19 tst.zip
[root@localhost ~]# mv tst tst_bak
[root@localhost ~]# unzip tst.zip
Archive:  tst.zip
   creating: META-INF/
  inflating: META-INF/MANIFEST.MF   
   creating: tst/
extracting: tst/bbb                
extracting: tst/aaa                
extracting: tst/ccc                
[root@localhost ~]# ls -l tst            ---可以看到,使用unzip解压后的文件权限为644
total 0
-rw-r--r-- 1 root root 0 Sep 25 00:59 aaa
-rw-r--r-- 1 root root 0 Sep 25 00:59 bbb
-rw-r--r-- 1 root root 0 Sep 25 00:59 ccc
[root@localhost ~]# rm -fr tst            ---删掉解压后的文件,设置umask为000,再解压发现解压文件的权现为666
[root@localhost ~]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jun  4 20:56 Desktop
drwxr-xr-x 2 root root 4096 Sep 25 01:19 META-INF
drwxr-xr-x 2 root root 4096 Sep 25 00:59 tst_bak
-rw-r--r-- 1 root root  699 Sep 25 01:19 tst.zip
drwxr-xr-x 7 root root 4096 Jan 22  2010 vmware-tools-distrib
[root@localhost ~]# umask 0000
[root@localhost ~]# umask
0000
[root@localhost ~]# unzip tst.zip
Archive:  tst.zip
replace META-INF/MANIFEST.MF? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: META-INF/MANIFEST.MF   
   creating: tst/
extracting: tst/bbb                
extracting: tst/aaa                
extracting: tst/ccc                
[root@localhost ~]# ls -l tst
total 0
-rw-rw-rw- 1 root root 0 Sep 25 00:59 aaa
-rw-rw-rw- 1 root root 0 Sep 25 00:59 bbb
-rw-rw-rw- 1 root root 0 Sep 25 00:59 ccc
[root@localhost ~]#

因此可以判断,使用jar压缩的文件,解压后与系统当前环境的umask有关系。

通过这个实验可以判断,jar压缩和解压的时候,不会记录文件的权限.建议我们在使用这些工具的时候,一定要熟悉它们的特点

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23850820/viewspace-1280734/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23850820/viewspace-1280734/

你可能感兴趣的:(使用jar与zip压缩解压文件的区别)