extract, add or list zip files in windows and linux

Simple tips for using zips files no matter of the OS and the tool. Archives are essential part of most computer tasks. Compression is very powerful tool that gives us way to store more information, transfer data faster and safer, provide back ups and many more. Next few tips may save you some time when you need to work with zip files.

List all files from a zip file

Linux

unzip

Install unzip if needed and use the command with argument l: unzip -l

sudo apt-get install unzip
unzip -l samplefile.zip

7z

7z is more advanced tool with bigger range of formats. It can be installed and used by:

sudo apt-get install p7zip-full
7z l zipfile.zip

Windows

For windows if you need to work with zip files via command prompt or with scripts you can use 7-zip (even with portable version). Steps to be used are:

  • Install 7-zip or use the portable version.
  • Open command prompt by:

Windows+R and then type cmd, press enter

or

Open start menu and type: Command Prompt , then press enter

  • Add 7-zip to environmental variable path (optional) in order to be able to execute commands only by typing 7z. Other wise you will need to write the full path the the command every time that you executed:
rem 7zip is added to path variable
7z l C:\Users\user\Documents\file.zip
rem 7zip is not added
C:\Users\user\PortableApps\7-Zip\App\7-Zip64\7z l C:\Users\user\Documents\file.zip

When you are adding it to path variable you will need to find the installation path of the application. Below there are several examples for permanent change, portable or installed version. You may consider also the version of the OS 32 or 64. When you are adding the variable you need only one path (below is just a sample with 3 different variants):

rem 7-zip portable edition
set PATH=%PATH%;C:\Users\user\PortableApps\7-Zip\App\7-Zip64
rem installed version
set PATH=%PATH%;C:\Program Files\7-Zip\
rem permanent change of path variable. Otherwise you need to add 7-zip for each session
setx PATH "%PATH%;C:\Program Files\7-Zip\"

7z
  • List all files in archive file by:
rem use command 7z
7z l C:\Users\user\Documents\file.zip
rem use full path to the command 7z
C:\Users\user\PortableApps\7-Zip\App\7-Zip64\7z l C:\Users\user\Documents\file.zip

Extract one or several files from zip archive

Linux

unzip

First you will need to list the file structure and the to give it in the command for unzipping.

#check file structure of the archive
unzip -l file.zip

#extract single file
unzip -j file.zip path_in_zip/file.txt -d folder

#extract all text files prompted
unzip -j file.zip path_in_zip/*.txt -d folder

#extract all matching pattern 20170728
unzip -j file.zip path_in_zip/*20170728* -d folder

path_in_zip is taken by list command
-d folder - is the output for the extracted file. folder will be created in the current working folder which can be checked by pwd.

7z

List the zip files and extract one or several files that matches a pattern. Output folder can be provided as parameter.

7z l zipfile.zip
#one file current folder
7z x file.zip path_in_zip/file.txt

#all text file
7z x file.zip *.txt

#several file types
7z e file.zip -o folder *.txt *.xml -r

Windows

7z

It's similar to the Linux version

7z l zipfile.zip
rem one file current folder
7z x file.zip path_in_zip/file.txt

rem all text file
7z x file.zip *.txt

rem several file types
7z e file.zip -o folder *.txt *.xml -r

Test zip file

Linux

zip

Check zip integrity with zip and unzip commands.

zip -T
unzip -t

7z

List the zip files and extract one or several files that matches a pattern. Output folder can be provided as parameter.

 7z t file.zip

Windows

7z

It's similar to the Linux version

 7z t file.zip

Create new zip file

Linux

zip

Create archive file with all text files in folder.

#check files for archiving
ls -a
#archive all text files in current folder(check current folder by pwd).
sudo apt-get install zip
zip -r  textfiles.zip *.txt

#archive everything(including sub-folders recursively) in current folder
zip -r  zipfile.zip *.*

#archive files at first level(no sub-folders)
zip  zipfile.zip *.*

7z

Add several files by name or filtered by extension to archive.

#archive several files.
7z a outfile.zip file1 file2

#archive text files
7z a outfile.zip *.txt

#archive all files.
7z a outfile.zip *.*

Windows

7z

It's similar to the Linux version

rem check files for archiving
dir 

#archive several files.
7z a outfile.zip file1 file2

#archive text files
7z a outfile.zip *.txt

#archive all files.
7z a outfile.zip *.*

If you want to see more linux or windows commands you can check here:
Windows cmd vs Linux shell commands

Related Article