How to Zip and Unzip Files in WSL2

How can we zip and unzip files in WSL2 (Windows Subsystem for Linux)?

zip and unzip

First, we need to install the appropriate packages: zip and unzip.

sudo apt-get install zip unzip -y

zip files

We can then zip files using the zip command.

zip file1.txt file2.txt

We specify the name of the zip file first, then we can list out the relative file paths to zip.

If we specify a pre-existing zip file, then this command will add to that zip.

zip folders

zip -r folder_name

The -r flag is a recursive option to zip an entire directory tree at once.

unzip a file

We can unzip a zip file using unzip.


tar and gzip

If we see a .tar.gz ending, we’ll need to use tar (the tape archiver) and gzip (the GNU-Zip).

sudo apt-get install tar gzip -y

gzip files

tar -cvzf gzipped.tar.gz file1.txt file2.txt

There are lots of flags; let’s quickly break those down.

  • -c: create an archive
  • -v: verbose logs (optional)
  • -z: compress with gzip
  • -f: archived file name

gzip folders

Similar to zip, we can gzip folders as well by simply providing a relative path to a directory.

tar -cvzf gzipped.tar.gz folder_name

Extract a file

tar -xvzf gzipped.tar.gz

We want to use -x instead of -c in order to extract the contents of a zipped file.