如何在Linux和类Unix系统中提取Zip文件

时间:2020-01-09 10:40:12  来源:igfitidea点击:

如何使用命令行选项在基于Linux或Unix的系统上提取zip文件?
Linux如何解压zip文件?
您需要在Linux或Unix之类的系统上使用unzip命令提取(解压缩)文件。默认情况下,此命令可能未安装在Linux操作系统上。

安装解压缩命令

首先,打开终端应用程序。
接下来,根据您的Linux或Unix变体,使用以下任何命令来安装解压缩工具:

Debian/Ubuntu Linux用户安装解压缩

使用apt-get命令的语法如下:

sudo apt-get install unzip

CentOS/RHEL/Fedora Linux用户安装解压缩

使用yum命令的语法如下:

sudo yum install unzip

在Linux或类似Unix的系统上如何使用unzip命令?

语法为:

unzip zipfilename
unzip zipfilename fileToExtract
unzip [optionz] zipfilename

提取一个名为foo.zip的zip文件

要使用解压缩将存档数据库.zip的所有成员提取到当前目录及其下的子目录中,并根据需要创建任何子目录,请执行:

unzip database

或者

unzip database.zip

另一个例子:

unzip foo
OR
<pre lang="bash">
unzip foo.zip

输出示例:

:Archive:  foo.zip
   creating: etc/
  inflating: etc/afpovertcp.cfg      
  inflating: etc/aliases             
   creating: etc/apache2/
   creating: etc/apache2/extra/
  inflating: etc/apache2/extra/httpd-autoindex.conf  
  inflating: etc/apache2/extra/httpd-dav.conf  
  inflating: etc/apache2/extra/httpd-languages.conf  
  inflating: etc/apache2/extra/httpd-manual.conf  
  inflating: etc/apache2/extra/httpd-mpm.conf  
.....
...
..
  inflating: etc/sshd_config         
  inflating: etc/sshd_config~previous  
  inflating: etc/syslog.conf         
  inflating: etc/ttys                
extracting: etc/xtab                
  inflating: etc/zshenv

要提取所有python(* .py)文件,请执行:

unzip file.zip "*.py"

要将所有C源文件* .c和* .h以及一个Makefile提取到~/projects/editor目录中:

unzip source.zip "*.[ch]" Makefile -d ~/projects/editor

从zip文件demo.zip中提取除bar.txt之外的所有文件

unzip demo.zip -x bar.txt

将文件提取到指定目录

要将文件解压缩到/tmp/data中而不是默认的当前目录中,请执行:

unzip demo.zip -d /tmp/data
unzip demo.zip -d /tmp/data pic1.jpg

输出示例:

Archive:  demo.zip
   creating: ../ddd/etc/
  inflating: ../ddd/etc/afpovertcp.cfg  
  inflating: ../ddd/etc/aliases      
   creating: ../ddd/etc/apache2/
   creating: ../ddd/etc/apache2/extra/
  inflating: ../ddd/etc/apache2/extra/httpd-autoindex.conf  
  inflating: ../ddd/etc/apache2/extra/httpd-dav.conf  
  inflating: ../ddd/etc/apache2/extra/httpd-default.conf  
....
...

解压缩时强制覆盖现有文件

unzip -o file.zip
unzip -o file.zip data.jpg

-o选项将覆盖文件而不会提示。
传递-n选项永远不会覆盖现有文件:

unzip -n file.zip
unzip -n file.zip data.jpg

在屏幕上列出存储在zip文件中的文件

unzip -l file.zip
unzip -l file.zip | more
unzip -l foo.zip

输出示例:

Archive:  foo.zip
  Length     Date   Time    Name
--------    ----   ----    ---
        0  04-24-15 13:08   etc/
      515  09-10-14 02:17   etc/afpovertcp.cfg
     9970  09-10-14 04:06   etc/aliases
        0  11-06-14 06:51   etc/apache2/
        0  09-10-14 03:39   etc/apache2/extra/
     2877  09-10-14 03:39   etc/apache2/extra/httpd-autoindex.conf
     1747  09-10-14 03:39   etc/apache2/extra/httpd-dav.conf
    11406  09-10-14 03:39   etc/apache2/extra/httpd-ssl.conf
      607  09-10-14 03:39   etc/apache2/extra/httpd-userdir.conf
     1519  09-10-14 03:39   etc/apache2/extra/httpd-vhosts.conf
     3161  09-10-14 03:39   etc/apache2/extra/proxy-html.conf
    20786  09-10-14 03:41   etc/apache2/httpd.conf
....
..
...
      382  06-25-14 16:46   etc/ssh_host_rsa_key.pub
     4161  02-03-15 00:47   etc/sshd_config
     1316  09-10-14 02:17   etc/ttys
        0  09-10-14 02:17   etc/xtab
      126  09-10-14 04:48   etc/zshenv
--------                   ------
  2747844                   281 files

测试或验证压缩的存档数据

unzip -t foo
unzip -t data.zip

输出示例:

Archive:  data.zip
    testing: etc/                     OK
    testing: etc/afpovertcp.cfg       OK
    testing: etc/aliases              OK
    testing: etc/apache2/             OK
    testing: etc/apache2/extra/httpd-default.conf   OK
....
..
...
    testing: etc/ssh_host_rsa_key.pub   OK
    testing: etc/sshd_config          OK
    testing: etc/sshd_config~previous   OK
    testing: etc/syslog.conf          OK
    testing: etc/zshenv               OK
No errors detected in compressed data of data.zip.

查看解压缩命令选项

执行以下命令:

unzip 
unzip -h

输出示例:

UnZip 5.52 of 28 February 2005, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.
 
Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z -  ZipInfo mode ("unzip -Z" for usage).
 
  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive data
  -u  update files, create if necessary      -z  display archive comment
  -x  exclude files that follow (in xlist)   -d  extract files into exdir
 
modifiers:                                   -q  quiet mode (-qq -  quieter)
  -n  never overwrite existing files         -a  auto-convert any text files
  -o  overwrite files WITHOUT prompting      -aa treat ALL files as text
  -j  junk paths (do not make directories)   -v  be verbose/print version info
  -C  match filenames case-insensitively     -L  make (some) names lowercase
  -X  restore UID/GID info                   -V  retain VMS version numbers
  -K  keep setuid/setgid/tacky permissions   -M  pipe through "more" pager
Examples (see unzip.txt for more info):
  unzip data1 -x joe   -  extract all files except joe from zipfile data1.zip
  unzip -p foo | more  -  send contents of foo.zip via pipe into program more
  unzip -fo foo ReadMe -  quietly replace existing ReadMe if archive file newer