Linux 压缩和解压 zip

码农日常5小时前更新 Trunks
6 0 0

zip 是目前使用最多的压缩文件格式,哪怕是 Linux 服务器也经常需要和 zip 文件打交道。

ZIP 文件格式是一种数据压缩和文档储存的文件格式,原名Deflate,发明者为菲尔·卡茨,他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为application/zip。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7z格式。

我这里使用的 Linux 版本是 CentOS7,一般大多数系统都会内置有 zip 工具,不过一些最小安装的 Linux 可能没有 zip 工具,如果压缩时出现 -bash: zip: 未找到命令 说明没有内置 zip 工具,只需要下载 zip 工具即可。

下载 unzip:

Shell
yum -y install unzip

下载 zip:

Shell
yum -y install zip

如果要更详细的了解 yum 的使用可以看 Linux Yum简单使用教程 。

如果是 Ubuntu 这一类系统的话使用:

Shell
apt-get install zip

在询问是否安装的时候输入 y 回车,Ubuntu 在安装 zip 的时候也会自动安装 unzip。

zip 压缩

压缩的命令为: zip 新 文件名 要压缩的文件名 ,下面把 home 目录下的 1.txt 和 2.txt 压缩为 1.zip

Shell
[root@localhost home]# zip 1.zip 1.txt 2.txt
  adding: 1.txt (stored 0%)
  adding: 2.txt (deflated 34%)

查看一下文件:

Shell
[root@localhost home]# ls
1.txt  1.zip  2.txt

压缩当前目录下的所有文件:

Shell
[root@localhost home]# zip all.zip *
  adding: 1.txt (stored 0%)
  adding: 1.zip (stored 0%)
  adding: 2.txt (deflated 34%)

把当前目录下的 newdir 目录和里面所有的文件都压缩到 newdir.zip

Shell
[root@localhost home]# zip -r newdir.zip newdir
  adding: newdir/ (stored 0%)
  adding: newdir/1.txt (stored 0%)
  adding: newdir/2.txt (deflated 4%)

给 zip 包添加文件: zip -g zip包文件名 要添加的文件名 ,下面给 1.zip 中添加一个 3.txt

Shell
[root@localhost home]# zip -g 1.zip 3.txt
  adding: 3.txt (stored 0%)

直接打包不压缩: zip -0 zip包名 要打包的文件名 ,下面把 1.txt 打包为 file.zip

Shell
[root@localhost home]# zip -0 file.zip 1.txt
  adding: 1.txt (stored 0%)

高质量压缩: zip -9 新文件名 要压缩的文件名 ,下面把 3 个 txt 文件压缩为 file.zip

Shell
[root@localhost home]# zip -9 file.zip 1.txt 2.txt 3.txt
updating: 1.txt (stored 0%)
  adding: 2.txt (deflated 34%)
  adding: 3.txt (stored 0%)

对比一下直接打包的文件体积却确实小了一点点

Shell
[root@localhost home]# ls -l
总用量 20
-rw-r--r--. 1 root root  23 2月  22 15:04 1.txt
-rw-r--r--. 1 root root 668 2月  22 16:38 1.zip
-rw-r--r--. 1 root root 145 2月  22 15:07 2.txt
-rw-r--r--. 1 root root  64 2月  22 16:22 3.txt
-rw-r--r--. 1 root root 618 2月  22 16:37 file.zip

排除文件: zip -X 新文件名 要排除的文件名 ,下面把除 2.txt 外的所有文件都压缩到 1.zip

Shell
[root@localhost home]# zip -X 1.zip 2.txt
  adding: 2.txt (deflated 34%)

加密压缩: zip -e 新文件名 要压缩的文件名 ,按下回车后会提示输入密码,下面把 1.txt 加密压缩为 1.zip

Shell
[root@localhost home]# zip -e 1.zip 1.txt
Enter password: 
Verify password: 
  adding: 1.txt (stored 0%)

删除 zip 包中的文件: zip -d zip包名 要删除的文件名 ,下面删除 1.zip 中的 1.txt 文件:

Shell
[root@localhost home]# zip -d 1.zip 1.txt
deleting: 1.txt

下面是 zip 的一些参数说明:

参数 说明
-d 删除 zip 包中的文件
-e 加密压缩
-F 修复 zip 文件
-g 向 zip 包中添加文件
-h 显示 zip 帮助
-h2 显示更多的 zip 帮助
-j 只压缩文件,不处理目录,压缩一个目录只会包含文件,不包含目录
-L 显示 zip 许可证
-m 压缩完成后删除原文件
-o 把 zip 包的修改时间改为 zip 包中文件的最新时间
-p 加密压缩,明文存储密码,安全性不如 -e
-q 压缩文件时不输出任何信息
-T 测试 zip 文件的完整性
-u 更新文件
-X 排除文件
-z 给 zip 包添加注释信息
-0 or -1 or -2 or -3 or -4 or -5 or -6 or -7 or -8 or -9 控制压缩的质量,质量越好压缩速度越慢,-0 不压缩,-9 质量最好,默认为 -6

上面只是写了一部分参数,更多的参数可以看 https://linux.die.net/man/1/zip 

unzip 解压

解压 zip 包到当前目录: unzip zip包文件名 ,下面解压 1.zip

Shell
[root@localhost home]# unzip 1.zip
Archive:  1.zip
 extracting: 1.txt                   
  inflating: 2.txt                   
 extracting: 3.txt                  

把文件解压到其他目录: unzip -d 目录名 zip包文件名 ,下面把 home 目录下的 1.zip 解压到 root 目录:

Shell
[root@localhost home]# unzip -d /root 1.zip
Archive:  1.zip
 extracting: /root/1.txt             
  inflating: /root/2.txt             
 extracting: /root/3.txt             

查看 zip 包内有哪些文件: unzip -l zip包文件名 ,下面查看 1.zip 内有哪些文件:

Shell
[root@localhost home]# unzip -l 1.zip
Archive:  1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       23  02-22-2019 15:04   1.txt
      145  02-22-2019 15:07   2.txt
       64  02-22-2019 16:22   3.txt
---------                     -------
      232                     3 files

查看 zip 包内文件的压缩率: unzip -v zip包文件名 ,下面查看 1.zip 内文件的压缩率:

Shell
[root@localhost home]# unzip -v 1.zip
Archive:  1.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
      23  Stored       23   0% 02-22-2019 15:04 d7df0560  1.txt
     145  Defl:N       95  35% 02-22-2019 15:07 ad661f88  2.txt
      64  Stored       64   0% 02-22-2019 16:22 bc70d4fa  3.txt
--------          -------  ---                            -------
     232              182  22%                            3 files

查看文件是否有损坏: unzip -t zip包文件名 ,下面查看 1.zip 是否有文件损坏:

Shell
[root@localhost home]# unzip -t 1.zip
Archive:  1.zip
    testing: 1.txt                    OK
    testing: 2.txt                    OK
    testing: 3.txt                    OK
No errors detected in compressed data of 1.zip.

下面是 unzip 的参数说明:

参数 说明
-c 将解压缩的结果显示到屏幕上,并对字符做适当的转换。
-f 更新现有的文件。
-l 显示压缩文件内所包含的文件。
-p 与-c参数类似,会将解压缩的结果显示到屏幕上,但不会执行任何的转换。
-t 检查压缩文件是否正确。
-u 与-f参数类似,但是除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中。
-v 查看压缩包内文件的详细信息。
-z 查看压缩包注释。
-j 不处理压缩文件中原有的目录路径。
-L 将压缩文件中的全部文件名改为小写。
-n 解压缩时不要覆盖原有的文件。
-o 不必先询问用户,unzip 执行后覆盖原有文件。
-q 执行时不显示任何信息。
-d 解压到指定目录。

以上就是 Linux zip和 unzip 的使用教程,一个是压缩,一个是解压,两个缺一不可,无论是 CentOS 还是 Ubuntu 命令都是一样的。

© 版权声明

相关文章

暂无评论

暂无评论...