在无解压缩的情况下将文件列出
1. 概述
将多个目录和文件打包到压缩存档中可以使这些文件更易于管理。此外,压缩档案还有另一个好处:节省存储空间。 Zip 是最流行的存档文件格式之一。在本快速教程中,让我们学习如何在不解压缩的情况下列出 Zip 存档中的文件。
2. 问题介绍
首先,让我们从一些文件创建一个 Zip 存档:
$ tree /tmp/test
/tmp/test
├── Archlinux.torrent
├── beautiful.png
├── document.pdf
├── sub1
│ └── doc1.pdf
└── sub2
└── doc2.pdf
2 directories, 5 files
我们已经准备好包含文件和子目录的*/tmp/test*目录。现在,让我们使用zip 命令创建一个 Zip 存档:
$ zip -r test.zip /tmp/test
adding: tmp/test/ (stored 0%)
adding: tmp/test/sub2/ (stored 0%)
adding: tmp/test/sub2/doc2.pdf (deflated 61%)
adding: tmp/test/sub1/ (stored 0%)
adding: tmp/test/sub1/doc1.pdf (deflated 3%)
adding: tmp/test/beautiful.png (deflated 4%)
adding: tmp/test/Archlinux.torrent (deflated 20%)
adding: tmp/test/document.pdf (deflated 20%)
$ ls -l test.zip
-rw-r--r-- 1 kent kent 2954728 Feb 21 17:26 test.zip
如上面的输出所示,我们已将整个 /tmp/test目录打包到存档 test.zip中。此外,zip命令还会打印每个文件的压缩率。
接下来,让我们看看如何在不解压的情况下列出test.zip中的文件 。
3. 使用unzip命令
unzip 命令是一个非常方便的解压缩 Zip 档案的实用程序。此外,*我们可以使用-l选项列出 Zip 存档中的文件*:
$ unzip -l test.zip
Archive: test.zip
## Length Date Time Name
0 2022-02-21 17:26 tmp/test/
0 2022-02-21 17:23 tmp/test/sub2/
73378 2021-11-17 12:29 tmp/test/sub2/doc2.pdf
0 2022-02-21 17:23 tmp/test/sub1/
21452 2022-01-02 17:06 tmp/test/sub1/doc1.pdf
8085 2022-01-26 19:00 tmp/test/beautiful.png
64739 2022-01-28 02:10 tmp/test/Archlinux.torrent
## 3533473 2022-02-02 10:40 tmp/test/document.pdf
3701127 8 files
正如我们在上面的输出中看到的, unzip -l报告了 Zip 存档中文件的四个属性:长度、日期、时间,当然还有文件名。我们应该注意到输出中的“长度”值是压缩前的文件大小。
如果我们想知道关于文件的更多信息,我们可以使用*-v*选项:
$ unzip -v test.zip
Archive: test.zip
## Length Method Size Cmpr Date Time CRC-32 Name
0 Stored 0 0% 2022-02-21 17:26 00000000 tmp/test/
0 Stored 0 0% 2022-02-21 17:23 00000000 tmp/test/sub2/
73378 Defl:N 28978 61% 2021-11-17 12:29 1c0f66f4 tmp/test/sub2/doc2.pdf
0 Stored 0 0% 2022-02-21 17:23 00000000 tmp/test/sub1/
21452 Defl:N 20772 3% 2022-01-02 17:06 f1a972de tmp/test/sub1/doc1.pdf
8085 Defl:N 7780 4% 2022-01-26 19:00 be5a77ca tmp/test/beautiful.png
64739 Defl:N 51510 20% 2022-01-28 02:10 55ffd9e0 tmp/test/Archlinux.torrent
## 3533473 Defl:N 2844342 20% 2022-02-02 10:40 b92dc568 tmp/test/document.pdf
3701127 2953382 20% 8 files
上面的输出提供了有关文件的更多信息——例如,压缩率和压缩后的大小,
4. 使用zipinfo命令
*zipinfo *命令,顾名思义,列出有关 Zip 存档的详细信息。 我们可以简单地将 Zip 存档名称传递给命令以获取文件列表:
$ zipinfo test.zip
Archive: test.zip
Zip file size: 2954728 bytes, number of entries: 8
drwxr-xr-x 3.0 unx 0 bx stor 22-Feb-21 17:26 tmp/test/
drwxr-xr-x 3.0 unx 0 bx stor 22-Feb-21 17:23 tmp/test/sub2/
-rw-r--r-- 3.0 unx 73378 bx defN 21-Nov-17 12:29 tmp/test/sub2/doc2.pdf
drwxr-xr-x 3.0 unx 0 bx stor 22-Feb-21 17:23 tmp/test/sub1/
-rw-r--r-- 3.0 unx 21452 bx defN 22-Jan-02 17:06 tmp/test/sub1/doc1.pdf
-rw-r--r-- 3.0 unx 8085 bx defN 22-Jan-26 19:00 tmp/test/beautiful.png
-rw-r--r-- 3.0 unx 64739 bx defN 22-Jan-28 02:10 tmp/test/Archlinux.torrent
-rw-r--r-- 3.0 unx 3533473 bx defN 22-Feb-02 10:40 tmp/test/document.pdf
8 files, 3701127 bytes uncompressed, 2953382 bytes compressed: 20.2%
我们可以在上面的输出中看到,默认情况下,zipinfo不会显示存档中每个文件的压缩率。相反,它会在最后报告整个档案的压缩率。
然而,值得一提的是,zipinfo命令以“ ls -l ”布局打印列表,包括文件权限属性。这对 Linux 命令行用户非常友好。
当然,zipinfo的功能不仅限于此。*当我们使用-v选项时,它可以为我们提供有关 Zip 存档的详细的多页技术报告。**现在,让我们看看zipinfo可以报告关于我们的test.zip 的*内容:
$ zipinfo -v test.zip
Archive: test.zip
...
(omitted about 300 lines)
...
## Central directory entry #8:
tmp/test/document.pdf
offset of local header from start of archive: 109575
(000000000001AC07h) bytes
file system or operating system of origin: Unix
version of encoding software: 3.0
minimum file system compatibility required: MS-DOS, OS/2 or NT FAT
minimum software version required to extract: 2.0
compression method: deflated
compression sub-type (deflation): normal
file security status: not encrypted
extended local header: no
file last modified on (DOS date/time): 2022 Feb 2 10:40:38
file last modified on (UT extra field modtime): 2022 Feb 2 10:40:38 local
file last modified on (UT extra field modtime): 2022 Feb 2 09:40:38 UTC
32-bit CRC value (hex): b92dc568
compressed size: 2844342 bytes
uncompressed size: 3533473 bytes
length of filename: 21 characters
length of extra field: 24 bytes
length of file comment: 0 characters
disk number on which file begins: disk 1
apparent file type: binary
Unix file attributes (100644 octal): -rw-r--r--
MS-DOS file attributes (00 hex): none
The central-directory extra field contains:
- A subfield with ID 0x5455 (universal time) and 5 data bytes.
The local extra field has UTC/GMT modification/access times.
- A subfield with ID 0x7875 (Unix UID/GID (any size)) and 11 data bytes:
01 04 e8 03 00 00 04 e8 03 00 00.
There is no file comment.
为了简单起见,我们只从报告中提取最后一个文件tmp/test/document.pdf的数据。但是,正如我们所见,该报告包含存档中每个文件对象的详细技术数据。
有时候,我们不想看到那么多信息。我们需要的可能只是 Zip 存档中的文件名。如果是这种情况,我们可以使用*-1*选项:
$ zipinfo -1 test.zip
tmp/test/
tmp/test/sub2/
tmp/test/sub2/doc2.pdf
tmp/test/sub1/
tmp/test/sub1/doc1.pdf
tmp/test/beautiful.png
tmp/test/Archlinux.torrent
tmp/test/document.pdf
当我们想将一些文件名传递给其他命令或脚本以进行进一步处理时,这非常有用,因为它最大限度地减少了解析工作。
5. 使用less命令
我们经常将详细的输出通过管道传递给*less 命令,以便在 Linux 命令行中查看内容,因为我们可以在输出中来回导航。实际上,less命令可以读取 Zip 文件并列出存档中的内容。接下来,让我们启动命令less test.zip*:
$ less test.zip
==> use zip_file:contained_file to view a file in the archive
==> append : to filename to view the zip file
drwxr-xr-x 0 1000 1000 0 Feb 21 17:26 tmp/test/
drwxr-xr-x 0 1000 1000 0 Feb 21 17:23 tmp/test/sub2/
-rw-r--r-- 0 1000 1000 73378 Nov 17 12:29 tmp/test/sub2/doc2.pdf
drwxr-xr-x 0 1000 1000 0 Feb 21 17:23 tmp/test/sub1/
-rw-r--r-- 0 1000 1000 21452 Jan 2 17:06 tmp/test/sub1/doc1.pdf
-rw-r--r-- 0 1000 1000 8085 Jan 26 19:00 tmp/test/beautiful.png
-rw-r--r-- 0 1000 1000 64739 Jan 28 02:10 tmp/test/Archlinux.torrent
-rw-r--r-- 0 1000 1000 3533473 Feb 2 10:40 tmp/test/document.pdf
test.zip (END)
如上面的输出所示,由于我们的test.zip只有八个文件和目录,所以整个文件列表可以显示在一个屏幕上。但是,想象一下当我们检查包含数千个文件的档案的内容时。然后,最好在less命令的交互式用户界面 中查看文件列表 ,因为我们可以在列表中移动并搜索一些有趣的关键字。