Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you zip or unzip from the script using ONLY Windows' built-in capabilities?

In Windows you can zip some files by

right click → Send toCompressed (zipped) folder

And unzip by double clicking on the .zip file and extract the files.

Is there a way to apply those abilities from a script (.bat file) without the need to install any third-party software?

like image 277
Roee Gavirel Avatar asked Jul 09 '13 10:07

Roee Gavirel


People also ask

How do I zip and unzip files in Windows 10 using built in tools?

Right-click the file you want to zip, and then select Send to > Compressed (zipped) folder. Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it.

Does Windows have a built in unzip?

Windows 10 comes with native support for files compression and uncompression using which you can easily compress(zip) and uncompress (unzip) files or folders on your Windows computer.

How do I zip a file in a script?

Now, you need to create a batch file for zipping a folder. To do that, open the Notepad app and enter a script like below: echo on for /f "tokens=3,2,4 delims=/- " %%x in ("%date%") do set d=%%y%%x%%z set data=%d% Echo zipping... "C:\Program Files\7-Zip\7z.exe" a -tzip "C:\twc. zip" "C:\MyDocuments\*.


2 Answers

To expand upon Steven Penny's PowerShell solution, you can incorporate it into a batch file by calling powershell.exe like this:

powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar'); }"

As Ivan Shilo said, this won't work with PowerShell 2, it requires PowerShell 3 or greater and .NET Framework 4.

like image 54
Jason Duffett Avatar answered Oct 18 '22 18:10

Jason Duffett


If you have Java installed, you can compress to a ZIP archive using the jar command:

jar -cMf targetArchive.zip sourceDirectory

c = Creates a new archive file.

M = Specifies that a manifest file should not be added to the archive.

f = Indicates target file name.

like image 149
Noam Manos Avatar answered Oct 18 '22 19:10

Noam Manos