Introduction

ZIP is an archive file format which supports lossless data compression A ZIP file may contain one or more files or directories that may have been compressed. ZIP format is been supported by many Operating Systems like Microsoft Windows ,Mac OS and Linux of course.

In this simple guide, we will explain how to unzip files in Ubuntu system through the command line using the unzip and 7z command and using python/perl script,also we will include how to create a zip file.

The commands listed in this tutorials have been tested in Ubuntu server 20.04 .

Prerequisites

  • An Ubuntu system
  • sudo/root access is required if unzip command is not been installed

Method1: Unzip a zip file using command unzip

Because by default unzip command is not been installed in Ubuntu , so we need to install it using apt.

j@ubuntuserver:~$ unzip

Command 'unzip' not found, but can be installed with:

sudo apt install unzip

Install unzip for Ubuntu / Debian

sudo apt install unzip

Unzip zip file into current directory

unzip file.zip

Without any options , the zip file will be unzipped into current directory

Unzip zip file into specified directory

If need to unzip the files into another directory but not current one , we can use option “-d” ,below example unzip the files into directory dir2

unzip -d dir2 test.zip

Unzip zip file quiet

You can see in above screenshot , unzip will print the file list which are being extracting, we can use option “-q” to suppress it.

unzip -q test.zip

List files of zip file

We can use option “-l” to list the files included by the zip file without actually extracting

unzip -l test.zip

More unzip options can be found from the manpage

j@ubuntuserver:~$ unzip -h
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
  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives
  -I CHARSET  specify a character encoding for UNIX and other archives

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
j@ubuntuserver:~$

Method 2: unzip a zip file using command 7z

7-Zip is an open source free software,you can use 7-Zip on any computer, including a computer in a commercial organization. You don’t need to register or pay for 7-Zip.

Install 7z for Ubuntu

sudo apt-get install p7zip-full

Below are the commands supported by 7z

<Commands>
  a : Add files to archive
  b : Benchmark
  d : Delete files from archive
  e : Extract files from archive (without using directory names)
  h : Calculate hash values for files
  i : Show information about supported formats
  l : List contents of archive
  rn : Rename files in archive
  t : Test integrity of archive
  u : Update files to archive
  x : eXtract files with full paths

Unzip zip file using 7z

7z x test.zip

List files of zip file

7z l test.zip

Method 3: Unzip zip file using python or perl script in Ubuntu

For methods 1 and 2 , we need to install extra application to unzip a zip file , in case somehow we can’t or don’t want to install any new tools , we can use perl/python script as a workaround since most of the linux releases get python and perl installed by default.

Unzip zip file using python script

Below is the code , save it as a file ,suppose as pyunzip.py

#!/usr/bin/env python3
import sys
from zipfile import PyZipFile
for zip_file in sys.argv[1:]:
    pzf = PyZipFile(zip_file)
    pzf.extractall()

Then in command line use below command to unzip a zip file

python3 pyunzip.py test.zip

Unzip zip file using perl script

Similar to using python script ,we also can do it using a perl script , see code below

#!/usr/bin/env perl
use Archive::Extract;
foreach my $filepath (@ARGV){
    my $archive = Archive::Extract->new( archive => $filepath );
    $archive->extract;
}

And then use below command to unzip a zip file

perl perlunzip.pl master.zip

How to create a zip file in Ubuntu

Now we will show you how to create a zip file in Ubuntu using command zip and 7z.

Zip files or directory using command zip

Similarly to command unzip ,we need it install it first.

sudo apt install zip

To zip some files

zip  file.zip file1 file2 file3

To zip a directory

zip -r test.zip testdir

Zip files or directory using command 7z

To zip files and/or directories , we can do it using command like below

7z a  test.zip file1 file2 dir1 dir2

Conclusion

Now we have showed you how to unzip zip file in Ubuntu , you can choose one of methods above based on your requirements.

Normally it’s simple to use command unzip , command 7z is an alternative , and in some scenarios maybe we need to use python/perl script as a workaround.