Categories
DailyOps

How to perform basic operations on zip archives

Perform basic operations on zip archives.

Inspect zip utility.

$ apt info zip
Package: zip
Version: 3.0-12
Priority: optional
Section: utils
Maintainer: Santiago Vila 
Installed-Size: 638 kB
Depends: libbz2-1.0, libc6 (>= 2.14)
Recommends: unzip
Homepage: http://www.info-zip.org/Zip.html
Tag: implemented-in::c, interface::commandline, role::program,
 scope::utility, use::compressing, use::storing, works-with-format::zip,
 works-with::archive
Download-Size: 232 kB
APT-Sources: http://deb.debian.org/debian bullseye/main amd64 Packages
Description: Archiver for .zip files
 This is InfoZIP's zip program. It produces files that are fully
 compatible with the popular PKZIP program; however, the command line
 options are not identical. In other words, the end result is the same,
 but the methods differ. :-)
 .
 This version supports encryption.

Inspect unzip utility.

$ apt info unzip
Package: unzip
Version: 6.0-26
Priority: optional
Section: utils
Maintainer: Santiago Vila 
Installed-Size: 394 kB
Depends: libbz2-1.0, libc6 (>= 2.14)
Suggests: zip
Homepage: http://www.info-zip.org/UnZip.html
Tag: implemented-in::c, interface::commandline, role::program,
 scope::utility, use::checking, use::compressing,
 works-with-format::zip, works-with::archive
Download-Size: 171 kB
APT-Manual-Installed: no
APT-Sources: http://deb.debian.org/debian bullseye/main amd64 Packages
Description: De-archiver for .zip files
 InfoZIP's unzip program. With the exception of multi-volume archives
 (ie, .ZIP files that are split across several disks using PKZIP's /& option),
 this can handle any file produced either by PKZIP, or the corresponding
 InfoZIP zip program.
 .
 This version supports encryption.

Install zip utility. The unzip package will be installed alongside.

$ sudo apt install zip
Reading package lists... Done                                                                              
Building dependency tree... Done                                                                           
Reading state information... Done                                                                          
The following additional packages will be installed:                                                       
  unzip                                                                                                    
The following NEW packages will be installed:                                                              
  unzip zip                                                                                                
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.                                             
Need to get 403 kB of archives.                                                                            
After this operation, 1032 kB of additional disk space will be used.                                       
Do you want to continue? [Y/n]                                                                             
Get:1 http://deb.debian.org/debian bullseye/main amd64 unzip amd64 6.0-26 [171 kB]                         
Get:2 http://deb.debian.org/debian bullseye/main amd64 zip amd64 3.0-12 [232 kB]                           
Fetched 403 kB in 0s (1231 kB/s)                                                                           
Selecting previously unselected package unzip.                                                             
(Reading database ... 55193 files and directories currently installed.)                                    
Preparing to unpack .../unzip_6.0-26_amd64.deb ...                                                         
Unpacking unzip (6.0-26) ...                                                                               
Selecting previously unselected package zip.                                                               
Preparing to unpack .../archives/zip_3.0-12_amd64.deb ...                                                  
Unpacking zip (3.0-12) ...                                                                                 
Setting up unzip (6.0-26) ...                                                                              
Setting up zip (3.0-12) ...                                                                                
Processing triggers for man-db (2.9.4-2) ...                                                               
Processing triggers for mailcap (3.69) ... 

zip

Display help information on zip.

$ zip --help
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help

Add files to an archive.

$ zip archive.zip /etc/fstab /etc/hostname /etc/os-release 
  adding: etc/fstab (deflated 20%)
  adding: etc/hostname (stored 0%)
  adding: etc/os-release (deflated 43%)

Add directory to an archive.

$ zip archive.zip -r /etc/dpkg
  adding: etc/dpkg/ (stored 0%)
  adding: etc/dpkg/dpkg.cfg (deflated 38%)
  adding: etc/dpkg/shlibs.override (deflated 31%)
  adding: etc/dpkg/dpkg.cfg.d/ (stored 0%)
  adding: etc/dpkg/shlibs.default (deflated 32%)
  adding: etc/dpkg/origins/ (stored 0%)
  adding: etc/dpkg/origins/default (deflated 23%)
  adding: etc/dpkg/origins/debian (deflated 23%)

Add a specific file list to an archive.

$ find /etc/apt -name "*.gpg" -print
/etc/apt/trusted.gpg.d/debian-archive-bullseye-security-automatic.gpg
/etc/apt/trusted.gpg.d/debian-archive-stretch-security-automatic.gpg
/etc/apt/trusted.gpg.d/debian-archive-bullseye-stable.gpg
/etc/apt/trusted.gpg.d/debian-archive-stretch-stable.gpg
/etc/apt/trusted.gpg.d/debian-archive-buster-security-automatic.gpg
/etc/apt/trusted.gpg.d/debian-archive-buster-automatic.gpg
/etc/apt/trusted.gpg.d/debian-archive-stretch-automatic.gpg
/etc/apt/trusted.gpg.d/debian-archive-buster-stable.gpg
/etc/apt/trusted.gpg.d/debian-archive-bullseye-automatic.gpg
$ find /etc/apt -name "*.gpg" -print | zip debian.gpg.zip -@
  adding: etc/apt/trusted.gpg.d/debian-archive-bullseye-security-automatic.gpg (deflated 4%)
  adding: etc/apt/trusted.gpg.d/debian-archive-stretch-security-automatic.gpg (deflated 3%)
  adding: etc/apt/trusted.gpg.d/debian-archive-bullseye-stable.gpg (deflated 0%)
  adding: etc/apt/trusted.gpg.d/debian-archive-stretch-stable.gpg (stored 0%)
  adding: etc/apt/trusted.gpg.d/debian-archive-buster-security-automatic.gpg (deflated 4%)
  adding: etc/apt/trusted.gpg.d/debian-archive-buster-automatic.gpg (deflated 4%)
  adding: etc/apt/trusted.gpg.d/debian-archive-stretch-automatic.gpg (deflated 3%)
  adding: etc/apt/trusted.gpg.d/debian-archive-buster-stable.gpg (stored 0%)
  adding: etc/apt/trusted.gpg.d/debian-archive-bullseye-automatic.gpg (deflated 4%)

Add an encrypted file.

$ zip -e encrypted.zip /etc/ssh/sshd_config
Enter password: 
Verify password: 
  adding: etc/ssh/sshd_config (deflated 55%)

Delete files from an archive.

$ zip -d debian.gpg.zip etc/apt/trusted.gpg.d/debian-archive-stretch-*
deleting: etc/apt/trusted.gpg.d/debian-archive-stretch-security-automatic.gpg
deleting: etc/apt/trusted.gpg.d/debian-archive-stretch-stable.gpg
deleting: etc/apt/trusted.gpg.d/debian-archive-stretch-automatic.gpg

Add a comment to an archive.

$ zip -z encrypted.zip 
enter new zip file comment (end with .):
OpenSSH configuration    
.

zipcloak

Display help information on zipcloak.

$ zipcloak --help
ZipCloak 3.0 (July 5th 2008)
Usage:  zipcloak [-dq] [-b path] zipfile
  the default action is to encrypt all unencrypted entries in the zip file

  -d  --decrypt      decrypt encrypted entries (copy if given wrong password)
  -b  --temp-path    use "path" for the temporary zip file
  -O  --output-file  write output to new zip file
  -q  --quiet        quiet operation, suppress some informational messages
  -h  --help         show this help
  -v  --version      show version info
  -L  --license      show software license

Use this utility to encrypt the whole archive.

$ zipcloak debian.gpg.zip --temp-path /tmp 
Enter password:  *********
Verify password: *********
encrypting: etc/apt/trusted.gpg.d/debian-archive-bullseye-security-automatic.gpg
encrypting: etc/apt/trusted.gpg.d/debian-archive-bullseye-stable.gpg
encrypting: etc/apt/trusted.gpg.d/debian-archive-buster-security-automatic.gpg
encrypting: etc/apt/trusted.gpg.d/debian-archive-buster-automatic.gpg
encrypting: etc/apt/trusted.gpg.d/debian-archive-buster-stable.gpg
encrypting: etc/apt/trusted.gpg.d/debian-archive-bullseye-automatic.gpg

unzip

Display help information on unzip.

$ unzip --help
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

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 only
  -v  list verbosely/show version info       -T  timestamp archive to latest
  -x  exclude files that follow (in xlist)   -d  extract files into exdir
modifiers:
  -n  never overwrite existing files         -q  quiet mode (-qq => quieter)
  -o  overwrite files WITHOUT prompting      -a  auto-convert any text files
  -j  junk paths (do not make directories)   -aa treat ALL files as text
  -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields
  -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
See "unzip -hh" or unzip.txt for more help.  Examples:
  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

List archive contents.

$ unzip -l archive.zip 
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
      232  2021-04-09 08:54   etc/fstab
        9  2021-04-09 08:55   etc/hostname
      267  2021-04-10 20:15   etc/os-release
        0  2021-09-01 19:06   etc/dpkg/
      446  2015-12-25 03:22   etc/dpkg/dpkg.cfg
      253  2021-01-30 06:07   etc/dpkg/shlibs.override
        0  2021-01-09 06:11   etc/dpkg/dpkg.cfg.d/
      260  2021-01-30 06:07   etc/dpkg/shlibs.default
        0  2021-09-01 18:59   etc/dpkg/origins/
       83  2021-04-10 20:15   etc/dpkg/origins/default
       83  2021-04-10 20:15   etc/dpkg/origins/debian
---------                     -------
     1633                     11 files

List archive contents including additional information like ratio and crc.

$ unzip -l archive.zip 
Archive:  archive.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     232  Defl:N      185  20% 2021-04-09 08:54 a112b49c  etc/fstab
       9  Stored        9   0% 2021-04-09 08:55 52f973c5  etc/hostname
     267  Defl:N      151  43% 2021-04-10 20:15 2e1b8289  etc/os-release
       0  Stored        0   0% 2021-09-01 19:06 00000000  etc/dpkg/
     446  Defl:N      276  38% 2015-12-25 03:22 04283a0e  etc/dpkg/dpkg.cfg
     253  Defl:N      175  31% 2021-01-30 06:07 171b0eb2  etc/dpkg/shlibs.override
       0  Stored        0   0% 2021-01-09 06:11 00000000  etc/dpkg/dpkg.cfg.d/
     260  Defl:N      176  32% 2021-01-30 06:07 36148cc2  etc/dpkg/shlibs.default
       0  Stored        0   0% 2021-09-01 18:59 00000000  etc/dpkg/origins/
      83  Defl:N       64  23% 2021-04-10 20:15 b2487c27  etc/dpkg/origins/default
      83  Defl:N       64  23% 2021-04-10 20:15 b2487c27  etc/dpkg/origins/debian
--------          -------  ---                            -------
    1633             1100  33%                            11 files

Test archive contents.

$ unzip -t archive.zip 
Archive:  archive.zip
    testing: etc/fstab                OK
    testing: etc/hostname             OK
    testing: etc/os-release           OK
    testing: etc/dpkg/                OK
    testing: etc/dpkg/dpkg.cfg        OK
    testing: etc/dpkg/shlibs.override   OK
    testing: etc/dpkg/dpkg.cfg.d/     OK
    testing: etc/dpkg/shlibs.default   OK
    testing: etc/dpkg/origins/        OK
    testing: etc/dpkg/origins/default   OK
    testing: etc/dpkg/origins/debian   OK
No errors detected in compressed data of archive.zip.

Extract file to standard output.

$ unzip -q -c archive.zip etc/fstab
# /etc/fstab: static file system information.
#
#                   

# device during installation: /dev/loop0p1
UUID=f6fba029-9da5-4010-9d3d-6e6698ed663b       /       ext4    rw,discard,errors=remount-ro    0       1

Extract archive to specific directory.

$ unzip archive.zip -d extracted_dir
Archive:  archive.zip
  inflating: extracted_dir/etc/fstab  
 extracting: extracted_dir/etc/hostname  
  inflating: extracted_dir/etc/os-release  
   creating: extracted_dir/etc/dpkg/
  inflating: extracted_dir/etc/dpkg/dpkg.cfg  
  inflating: extracted_dir/etc/dpkg/shlibs.override  
   creating: extracted_dir/etc/dpkg/dpkg.cfg.d/
  inflating: extracted_dir/etc/dpkg/shlibs.default  
   creating: extracted_dir/etc/dpkg/origins/
  inflating: extracted_dir/etc/dpkg/origins/default  
  inflating: extracted_dir/etc/dpkg/origins/debian 

Display archive comment.

$ unzip -z encrypted.zip 
Archive:  encrypted.zip
OpenSSH configuration

zipinfo

Display help information on zipinfo.

$ zipinfo --help
ZipInfo 3.00 of 20 April 2009, by Greg Roelofs and the Info-ZIP group.

List name, date/time, attribute, size, compression method, etc., about files
in list (excluding those in xlist) contained in the specified .zip archive(s).
"file[.zip]" may be a wildcard name containing *, ?, [] (e.g., "[a-j]*.zip").

   usage:  zipinfo [-12smlvChMtTz] file[.zip] [list...] [-x xlist...]
      or:  unzip -Z [-12smlvChMtTz] file[.zip] [list...] [-x xlist...]

main listing-format options:             -s  short Unix "ls -l" format (def.)
  -1  filenames ONLY, one per line       -m  medium Unix "ls -l" format
  -2  just filenames but allow -h/-t/-z  -l  long Unix "ls -l" format
                                         -v  verbose, multi-page format
miscellaneous options:
  -h  print header line       -t  print totals for listed files or for all
  -z  print zipfile comment   -T  print file times in sortable decimal format
  -C  be case-insensitive     -M  page output through built-in "more"
  -x  exclude filenames that follow from listing

Display archive contents.

$ zipinfo archive.zip 
Archive:  archive.zip
Zip file size: 2914 bytes, number of entries: 11
-rw-r--r--  3.0 unx      232 tx defN 21-Apr-09 08:54 etc/fstab
-rw-r--r--  3.0 unx        9 tx stor 21-Apr-09 08:55 etc/hostname
-rw-r--r--  3.0 unx      267 tx defN 21-Apr-10 20:15 etc/os-release
drwxr-xr-x  3.0 unx        0 bx stor 21-Sep-01 19:06 etc/dpkg/
-rw-r--r--  3.0 unx      446 tx defN 15-Dec-25 03:22 etc/dpkg/dpkg.cfg
-rw-r--r--  3.0 unx      253 tx defN 21-Jan-30 06:07 etc/dpkg/shlibs.override
drwxr-xr-x  3.0 unx        0 bx stor 21-Jan-09 06:11 etc/dpkg/dpkg.cfg.d/
-rw-r--r--  3.0 unx      260 tx defN 21-Jan-30 06:07 etc/dpkg/shlibs.default
drwxr-xr-x  3.0 unx        0 bx stor 21-Sep-01 18:59 etc/dpkg/origins/
-rw-r--r--  3.0 unx       83 tx defN 21-Apr-10 20:15 etc/dpkg/origins/default
-rw-r--r--  3.0 unx       83 tx defN 21-Apr-10 20:15 etc/dpkg/origins/debian
11 files, 1633 bytes uncompressed, 1100 bytes compressed:  32.6%

Display archive contents using the long format and sortable date/times.

$ zipinfo -l -T archive.zip 
Archive:  archive.zip
Zip file size: 2914 bytes, number of entries: 11
-rw-r--r--  3.0 unx      232 tx      185 defN 20210409.085419 etc/fstab
-rw-r--r--  3.0 unx        9 tx        9 stor 20210409.085541 etc/hostname
-rw-r--r--  3.0 unx      267 tx      151 defN 20210410.201500 etc/os-release
drwxr-xr-x  3.0 unx        0 bx        0 stor 20210901.190627 etc/dpkg/
-rw-r--r--  3.0 unx      446 tx      276 defN 20151225.032257 etc/dpkg/dpkg.cfg
-rw-r--r--  3.0 unx      253 tx      175 defN 20210130.060717 etc/dpkg/shlibs.override
drwxr-xr-x  3.0 unx        0 bx        0 stor 20210109.061157 etc/dpkg/dpkg.cfg.d/
-rw-r--r--  3.0 unx      260 tx      176 defN 20210130.060717 etc/dpkg/shlibs.default
drwxr-xr-x  3.0 unx        0 bx        0 stor 20210901.185959 etc/dpkg/origins/
-rw-r--r--  3.0 unx       83 tx       64 defN 20210410.201500 etc/dpkg/origins/default
-rw-r--r--  3.0 unx       83 tx       64 defN 20210410.201500 etc/dpkg/origins/debian
11 files, 1633 bytes uncompressed, 1100 bytes compressed:  32.6%

Display archive summary.

$ zipinfo -t  archive.zip 
11 files, 1633 bytes uncompressed, 1100 bytes compressed:  32.6%

Additional notes

There are other useful utilities included in this package like zipgrep to search files in a ZIP archive for specific patterns, zipsplit to split an archive into smaller files, funzip to use it inside pipeline, and unzipsfx to play with self-extracting ZIP archives.

Do not rely on ZIP encryption, use GnuPG or other key-based encryption to encrypt your data.