1. Overview

Packing multiple directories and files into a compressed archive makes those files more manageable. Further, compressed archives bring another benefit: saving storage space.

Zip is one of the most popular archive file formats. In this quick tutorial, let’s learn how to list the files in a Zip archive without decompressing them.

2. Introduction to the Problem

First of all, let’s create a Zip archive from some files:

$ tree /tmp/test
/tmp/test
├── Archlinux.torrent
├── beautiful.png
├── document.pdf
├── sub1
│   └── doc1.pdf
└── sub2
    └── doc2.pdf

2 directories, 5 files

We’ve prepared the /tmp/test directory containing files and subdirectories. Now, let’s create a Zip archive using the zip command:

$ 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

As the output above shows, we’ve packed the entire /tmp/test directory in the archive test.zip. Further, the zip command prints the compression rate of each file as well.

Next, let’s see how to list the files in test.zip without decompressing them.

3. Using the unzip Command

The unzip command is a pretty convenient utility to decompress Zip archives. Also, we can use the -l option to list the files in a Zip archive:

$ 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

As we can see in the output above, unzipl reports four properties of the files in the Zip archive: length, date, time, and filename, of course. We should note that the “Length” values in the output are the file sizes before compression.

If we want to know more information about the files, we can use the -v option:

$ 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

The output above tells more information about the files – for example, the compression rates and the size after the compression,

4. Using the zipinfo Command

The zipinfo command, as its name implies, lists detailed information about a Zip archive.

We can simply pass the Zip archive name to the command to get the file list:

$ 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%

We can see in the output above that, by default, zipinfo doesn’t show the compression rate of each file in the archive. Instead, it reports the compression rate of the entire archive at the end.

However, it’s worth mentioning that the zipinfo command prints the list in the “ls -l” layout, including the file permission attributes. This is pretty friendly to Linux command-line users.

Of course, the power of zipinfo isn’t limited to this. When we use the -v option, it can provide us with a detailed multi-page technical report about the Zip archive. Now, let’s see what zipinfo can report about our 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.

For simplicity, we only extract the data of the last file tmp/test/document.pdf from the report. However, as we can see, the report contains detailed technical data of each file object in the archive.

Sometimes, we don’t want to see so much information. What we need may only be the filenames in the Zip archive. If this is the case, we can use the -1 option:

$ 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

This is pretty helpful when we want to pass some filenames to other commands or scripts for further processing, as it minimizes the parsing effort.

5. Using the less Command

We often pipe a verbose output to the less command to view the content in the Linux command line since we can navigate back and forth in the output. Actually, the less command can read Zip files and list the content in the archive too. Next, let’s launch the command 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)

As the output above shows, since our test.zip has only eight files and directories, the whole file list fits on one screen. However, imagine when we’re checking the content of an archive containing thousands of files. Then, it would be nice to view the file list in the less command’s interactive user interface as we can move around the list and search for some interesting keywords.

6. Conclusion

In this article, we’ve introduced three common ways to list files in a Zip archive without actually decompressing them. Let’s summarize them quickly:

  • unzip – With the -l option
  • zipinfo – Provide from filename-only (-1) output to very detailed (-v) technical report
  • less – Interactive user interface; a good choice when checking archives with a large number of files manually