Featured image of post Compressing and Extracting ZIP Files in Windows Command Line

Compressing and Extracting ZIP Files in Windows Command Line

The previous article mentioned using rar.exe from WinRAR for command line extraction. In fact, Windows also has built-in commands for this purpose...

In the previous article, we discussed how to use rar.exe from WinRAR for command line extraction. Actually, Windows comes with its own commands that can achieve the same results.

Compressing Files

To compress files into ZIP format in the Command Prompt (CMD), you can use the powershell command to invoke PowerShell scripts. Below is an example:

1
powershell -command "Add-Type -A'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('C:\path\to\directory','C:\path\to\archive.zip')"

The command above compresses all files in the specified directory C:\path\to\directory into a ZIP file named archive.zip, which will be saved in the C:\path\to directory. Make sure to replace the paths with your actual directory paths.

In this example, we used PowerShell’s ZipFile class to create the ZIP file. The CreateFromDirectory method takes two parameters: the first is the directory path to compress, and the second is the path for the generated ZIP file.

Extracting Files

To extract ZIP files in the Command Prompt (CMD), you can use the following command:

1
powershell -command "Expand-Archive -Path'C:\path\to\archive.zip'-DestinationPath'C:\path\to\extract'"

This command extracts the specified ZIP file C:\path\to\archive.zip to the target directory C:\path\to\extract. Again, be sure to replace the actual file and destination paths as needed.

Here, we utilize PowerShell’s Expand-Archive command to unzip the ZIP file. The -Path parameter specifies the path to the ZIP file to be extracted, while the -DestinationPath parameter indicates where to extract the files.

Make sure that PowerShell is installed on your system, and you can successfully execute the above commands in CMD to extract ZIP files.

I hope this information is helpful. If you have any more questions, feel free to ask!