如何在Linux中zip和解压缩文件

时间:2020-03-05 15:25:59  来源:igfitidea点击:

压缩实用程序压缩文件(Borg和Rar),在其功能和操作中类似。

zip /解压缩是Linux中使用的命令之一,用于创建压缩文件并提取。
在现代Linux Distry中,这些实用程序默认安装。
如果有任何问题,则可以从shell提示安装两个实用程序,但我们必须是root用户来执行此操作。

关于Redhat/CentOS的系统

# yum install zip unzip

关于ubuntu/debian的系统

# apt-get install zip unzip
  • ZIP是Linux和UNIX的压缩和文件包装实用程序。
  • 解压缩将列出,测试或者从zip存档文件中提取文件。

zip和解压缩程序与DOS环境中的PKZIP和Pkunzip相同的方式工作。
压缩文件使用比普通文件更少的磁盘空间,并且它们比较大的未压缩文件更快地传输。
在Linux中,我们可以在GZIP或者Zip的帮助下压缩文件。
如果我们使用非Linux用户交换文件,则可以使用zip来避免兼容性问题。

ZIP文件(压缩)

压缩过程用具有另外扩展的新文件替换原始文件,该文件是.zip(zip命令的情况),因此不要删除我们创建的.zip文件。
它们是压缩包装器中的原始文件!语法是表单

# zip [option] [-b path] filezip file
  • 选项:有可用的zip命令选项
  • -b路径:在暂时保存zip文件,直到压缩完成,然后在当前工作目录中复制它
  • filezip:它是将创建的zip文件的名称
  • 文件:它是原始文件的名称。它可以是多个文件

下面给出的命令创建文件文件文件.zip,它包含位于当前目录中的名为file10的文件的副本。

# zip compressfile file10
# ls -l file*
-rw-r--r-- 1 root root 17 Jan 17 20:44 file1
-rw-r--r-- 1 root root 179 Jan 17 20:44 compressfile.zip

要压缩文件夹,我们使用了zip命令的-r`选项。
下面给出的命令将将目录脚本 - 测试压缩到文件名后面。

# zip -r back script-test/
 adding: script-test/(stored 0%)
 adding: script-test/hello2 (deflated 8%)
 adding: script-test/password-gen.sh (deflated 4%)
 adding: script-test/continue.sh (deflated 20%)
 adding: script-test/folder1/(stored 0%)
 adding: script-test/folder1/file1 (stored 0%)

请参阅它在文件的目录上是如何运行的。
你可以看到结果

# ls -l back.zip 
-rw-r--r-- 1 root root 6255 Jan 21 12:31 back.zip

我们可以自己在ZIP文件末尾添加.zip扩展。

# zip extension.zip file10 
 adding: file10 (stored 0%)

该命令将直接理解,而不会再添加相同的扩展名。

# ls -l extension.zip 
-rw-r--r-- 1 root root 179 Jan 21 13:15 extension.zip

你可以看到结果。

解压缩文件(提取)

Unzip命令仅提取.zip文件。
所以我们将使用它来提取创建的zip文件。
提取文件如下所示

# unzip compressfile.zip 
Archive: compressfile.zip
 extracting: file10

它提取当前目录中的内容。
所以,如果有原始文件,它会问我们是否要替换它

# unzip file.zip
replace file10? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
 extracting: file10

当zip文件包含多个文件时,它将与所有文件进行操作。
让我们尝试解压缩zip文件夹

# unzip back.zip 
Archive: back.zip
  creating: script-test/
 inflating: script-test/hello2 
 inflating: script-test/password-gen.sh 
 inflating: script-test/continue.sh 
  creating: script-test/folder1/
extracting: script-test/folder1/file1

更多zip和解压缩命令选项

1)压缩当前目录

下面给出的命令将压缩当前目录以及所有子目录:

# zip -r backupfile.zip .
 adding: hello2 (deflated 8%)
 adding: script-test/(stored 0%)
 adding: script-test/hello2 (deflated 8%)

2)压缩当前目录中的所有文件

下面写入的命令将创建一个名为backfile.zip的文件,该文件包含当前目录中的所有文件。
以"。
"开头的文件名称不包括在内。

# zip backfile.zip *
 adding: administration.sh (deflated 26%)
 adding: backupfile.zip (stored 0%)
 adding: back.zip (stored 0%)
# ls -l backfile.zip 
-rw-r--r-- 1 root root 28702 Jan 21 13:09 backfile.zip

3)在当前目录中提取所有压缩文件

我们可以提取以下所有ZIP文件

# unzip *.zip
Archive: backfile.zip
 extracting: backupfile.zip 
 extracting: back.zip 
 extracting: compressfile.zip

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

在解压缩期间,如果有原始文件,Unzip命令将询问我们是否要替换或者重命名新提取的文件。
如果我们在压缩存档中有某些文件,操作可能不容易。

# unzip script-fol.zip 
Archive: script-fol.zip
replace script-test/extension.zip? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: script-test/extension.zip 
replace script-test/hello2? [y]es, [n]o, [A]ll, [N]one, [r]ename:

因此,我们可以强制Unzip命令用unzip -o命令覆盖现有文件

# unzip -o script-fol.zip 
Archive: script-fol.zip
 extracting: script-test/extension.zip 
 inflating: script-test/hello2 
 extracting: script-test/script.zip 
 inflating: script-test/bonjour~ 
 inflating: script-test/script-test/hello2 
 inflating: script-test/script-test/bonjour~ 
 inflating: script-test/script-test/password-gen.sh

5)将文件压缩到另一个目录

可以通过指示结尾ZIP文件名称的新目标路径来zip zip文件并将zip文件保存在另一个目录中。

# zip test/zipfolder/file10.zip file10 
adding: file10 (stored 0%)

6)将存档解压缩到另一个目录

与压缩操作一样,可以将文件提取到另一个目录中。
它是nutzip命令的-d选项

# unzip file10.zip -d ~/script-test/
Archive: file10.zip
 extracting: /root/script-test/file10

7)最好的压缩

我们还可以尝试使用的"-9"选项以获得最佳压缩,如下所示:

# zip -9 -r backupfile mydata

8)压缩多个文件

可以将倍数文件压缩到一个zip文件中。
我们将它压缩到另一个目录

# zip /mnt/zip/script.zip continue.sh hello password-gen.sh 
 adding: continue.sh (deflated 20%)
 adding: hello (deflated 8%)
 adding: password-gen.sh (deflated 4%)

9)请参阅zip文件的内容而不解压缩

可以看到带有unzip -l命令的zip文件的内容。
就像我们列出了详细信息文件夹的内容而无需移动即可。

# unzip -l /mnt/zip/script.zip
Archive: /mnt/zip/script.zip
 Length Date Time Name
--------- ---------- ----- ---
 179 04-25-2016 02:10 continue.sh
 66 05-15-2016 20:12 hello
 107 04-25-2016 02:10 password-gen.sh
--------- ------
 352 3 files

10)从压缩存档中提取一个或者多个文件

如果压缩存档具有多个文件,则可以仅提取一个或者多个文件。
此操作不会提取压缩存档的所有文件。
我们需要指定每个文件名以提取

# unzip /mnt/zip/script.zip hello continue.sh -d ~/test/zipfolder/
Archive: /mnt/zip/script.zip
 inflating: /root/test/zipfolder/continue.sh 
 inflating: /root/test/zipfolder/hello

使用上面的命令意味着我们已经完全了解要提取的文件的名称。
因此,例如,它意味着我们已经列出了带有Unzip -L命令的压缩存档的内容

11)在减压期间排除某些文件

正如我们能够过滤要提取的文件,就可以在解压缩期间排除某些文件

# unzip backfile.zip -x file10 administration.sh -d /tmp
Archive: backfile.zip
 inflating: /tmp/administration_complete 
 inflating: /tmp/administration_total 
 extracting: /tmp/backupfile.zip 
 extracting: /tmp/back.zip 
 inflating: /tmp/bonjour~ 
 extracting: /tmp/compressfile.zip 
 inflating: /tmp/continue.sh 
 creating: /tmp/folder1/
 inflating: /tmp/hello 
 inflating: /tmp/hello2 
 creating: /tmp/intro-script/
 inflating: /tmp/mon-nom 
 inflating: /tmp/password-gen.sh 
 creating: /tmp/script-test/
 extracting: /tmp/toto h. Check the integrity of a compressed archive

我们可以检查压缩存档是否没有错误。
有可能的是unzip -t命令

# unzip -t backfile.zip 
Archive: backfile.zip
 testing: administration_complete OK
 testing: administration.sh OK
 testing: administration_total OK
 testing: backupfile.zip OK
 testing: back.zip OK
 testing: bonjour~ OK
 testing: compressfile.zip OK
 testing: continue.sh OK
 testing: file10 OK
 testing: folder1/OK
 testing: hello OK
 testing: hello2 OK
 testing: intro-script/OK
 testing: mon-nom OK
 testing: password-gen.sh OK
 testing: script-test/OK
 testing: toto OK
No errors detected in compressed data of backfile.zip.

12)在不创建目录的情况下解压缩存档

如果压缩存档包含一个文件夹,则在提取操作期间,它将重新创建精确结构。
可以在不创建已经存在的文件夹的情况下提取存档。
但他们的内容将直接提取

我们将考虑上面示例的zip存档。
它包含folder1和script-test文件夹,我们将在不创建两个的情况下提取

# unzip -j backfile.zip 
Archive: backfile.zip
 inflating: administration_complete 
 inflating: administration.sh 
 inflating: administration_total 
 extracting: backupfile.zip 
 extracting: back.zip 
 inflating: bonjour~ 
 extracting: compressfile.zip 
 inflating: continue.sh 
 extracting: file10 
 inflating: hello 
 inflating: hello2 
 inflating: mon-nom 
 inflating: password-gen.sh 
 extracting: toto

我们可以看到文件夹不存在

13)列出有关存档的详细信息

可以详细信息有关使用"LS -L"命令的存档内容的详细信息。

# unzip -Z backfile.zip 
Archive:  backfile.zip
Zip file size: 28702 bytes, number of entries: 17
-rwxr-xr-x  3.0 unx      147 tx defN 17-Apr-25 02:21 administration_complete
-rwxr-xr-x  3.0 unx      144 tx defN 17-Apr-25 02:10 administration.sh
-rwxr-xr-x  3.0 unx      147 tx defN 17-Apr-25 02:19 administration_total
-rw-r--r--  3.0 unx    18727 bx stor 17-May-21 12:54 backupfile.zip
-rw-r--r--  3.0 unx     6255 bx stor 17-May-21 12:45 back.zip
-rwxr-xr-x  3.0 unx       78 tx defN 17-Apr-25 02:10 bonjour~
-rw-r--r--  3.0 unx      179 bx stor 17-May-21 12:44 compressfile.zip
-rwxr-xr-x  3.0 unx      179 tx defN 17-Apr-25 02:10 continue.sh
-rw-r--r--  3.0 unx       17 tx stor 17-May-17 20:44 file10
drwxr-xr-x  3.0 unx        0 bx stor 17-May-17 04:28 folder1/
-rwxrwxr--  3.0 unx       66 tx defN 17-May-15 20:12 hello
-rwxrwxr--  3.0 unx       66 tx defN 17-May-15 20:12 hello2
drwxrwxrwt  3.0 unx        0 bx stor 17-Apr-27 02:44 intro-script/
-rwxr-xr-x  3.0 unx      147 tx defN 17-Apr-25 02:10 mon-nom
-rwxr-xr-x  3.0 unx      107 tx defN 17-Apr-25 02:10 password-gen.sh
drw-r--r--  3.0 unx        0 bx stor 17-May-19 17:41 script-test/
-rwxr-xr-x  3.0 unx       20 tx stor 17-Apr-25 02:10 toto
17 files, 26279 bytes uncompressed, 26118 bytes compressed:  0.6%

它将提供一些信息,例如存档名称,总大小,以及其中的文件总数。
预告片给出列出的文件数,列出文件的总未压缩和压缩数据大小(不计数内部Zipfile标题)和压缩比。

14)更新一个文件或者更多文件的压缩存档文件

假设我们已经压缩了存档,然后修改了文件。
有可能将更新文件添加到带有zip -u命令的压缩存档。

在下面的示例中,我们更新了文件toto并添加了一个新文件papi

# zip -u backfile.zip toto papi
 adding: toto/(stored 0%)
 adding: papi (stored 0%)

现在让我们检查一下

# unzip -Z backfile.zip | grep "toto" 
-rwxr-xr-x  3.0 unx        0 bx stor 17-May-16 17:46 toto
# unzip -Z backfile.zip | grep "papi" 
-rw-r--r--  3.0 unx        0 bx stor 17-May-22 01:10 papi

我们可以在每个命令的输出上的日期。
我们可以看到更新日期可能而不是4月

15)更新压缩存档的所有文件

只有在zip存档中的更新时才能替换(鲜明)在zip存档中的现有条目,而不是zip存档中的版本。
与"更新"选项不同,这不会添加尚未在zip存档中的文件。

# zip -f test.zip

16)静静地进行压缩和提取

当我们进行提取压缩时,通常ZIP命令显示操作的输出。
可以决定使用ZIP命令的"-Q"选项显示结果,但我们需要在一定时间后检查结果

# zip -rq /mnt/zip/script-fol.zip script-test/
# ls -l /mnt/zip/
total 68
-rw-r--r-- 1 root root 69072 Jan 22 01:35 script-fol.zip

17)解压缩时转换文本文件

一些非Linux系统将文本文件存储有两个线端字符CR和LF而不是所有UNIX系统上使用的更高效的单个字符(LF)。
我们可以发出兼容性。
我们可以使用"Unzip -A"(Unzip -a"与ASCII转换的助记符面对它。
它会转换备注文本的文件,同时留下标记为二进制文件的文件。

# unzip -a script-fol.zip 
Archive: script-fol.zip
 creating: script-test/
 extracting: script-test/extension.zip [binary]
 inflating: script-test/hello2 [text] 
 extracting: script-test/script.zip [binary]
 inflating: script-test/bonjour~ [text]

18)仅将主目录的子目录压缩到单个存档中

假设我们有一个带有一些文件和子目录的文件夹。
现在假设我们只需要压缩代表子目录。
我会给你一个脚本线来做。
确保首先输入主目录。

我们将首先检查我们是否进入正确的目录

# pwd
/mnt/zip/test

现在让我们看看我们的子目录

# ls -lF /mnt/zip/test
total 12
-rw-r--r-- 1 root root 0 Jan 22 02:31 COURSE
-rw-r--r-- 1 root root 0 Jan 22 02:31 FILE
-rw-r--r-- 1 root root 434 Jan 22 02:33 file.zip
drwxr-xr-x 2 root root 4096 Jan 22 02:44 pap/
drwxr-xr-x 2 root root 4096 Jan 22 02:44 pop/
-rw-r--r-- 1 root root 0 Jan 22 02:31 TEXT

我们可以看到我们有PAP和POP子文件夹。
让我们试试脚本

# for i in */; do zip -r "${i%/}.zip" "$i"; done
 adding: pap/(stored 0%)
 adding: pop/(stored 0%)

让我们检查他们的zip文件

# ls
COURSE FILE file.zip pap pap.zip pop pop.zip TEXT

我们可以看到它以分别为名称创建zip文件夹。
请注意,我们的命令仅压缩主目录的每个级别1子目录,而不是子目录的每个后代目录。

如果要查看其工作原理,请在zip之前包含回声,并且我们将看到打印的命令而不是执行。

# for i in */; do echo zip -r "${i%/}.zip" "$i"; done
zip -r pap.zip pap/
zip -r pop.zip pop/

19)将文件和目录压缩为单个存档

由于某种原因,我们可以将文件和目录压缩到单个档案中。
要做到这一点,我们可以使用下面的脚本线

# for i in *; do zip -r "${i%}.zip" "$i"; done
 adding: COURSE (stored 0%)
 adding: FILE (stored 0%)
 adding: file.zip (stored 0%)
 adding: pap/(stored 0%)
 adding: pop/(stored 0%)
 adding: TEXT (stored 0%)
# ls
COURSE FILE FILE.zip pap pop TEXT
COURSE.zip file.zip file.zip.zip pap.zip pop.zip TEXT.zip

20)显示有关压缩存档的详细信息

我们可以提供有关带有"-V""选项的压缩存档的更多信息,Unzip命令

# unzip -v backfile.zip 
Archive:  backfile.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ---
     147  Defl:N      125  15% 04-25-2016 02:21 beced85d  administration_complete
     147  Defl:N      125  15% 04-25-2016 02:19 beced85d  administration_total
   18727  Stored    18727   0% 05-21-2016 12:54 9a9d9d5b  backupfile.zip
    6255  Stored     6255   0% 05-21-2016 12:45 57a487bf  back.zip
      78  Defl:N       70  10% 04-25-2016 02:10 7ea59cec  bonjour~