Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a zipped/compressed folder in Windows using Powershell or the command line

Tags:

I am creating a nightly database schema file and would like to put all the files created each night, one for each database, into a folder and compress that folder. I have a PowerShell script that creates the schema.Only creation script of the db's and then adds all the files to a new folder. The problem lies within the compression portion of this process.

Does anybody have any idea if this can be accomplished with the pre-installed Windows utility that handles folder compression?

It would be best to use that utility if possible rather than something like 7zip (I don't feel like installing 7zip on every customers' server and it may take IT years to do it if I ask them).

like image 526
TechDawg270 Avatar asked Jun 13 '12 19:06

TechDawg270


People also ask

How do I zip a folder in Windows command line?

Use the cd command to go to the folder where your files are located. Enter the following command in the Command Prompt window and hit Enter. Replace output. zip with the name you want to give your ZIP file, and myfile.

How do I zip a folder in PowerShell?

Compress-Archive uses the Path parameter to specify the root directory, C:\Reference with an asterisk ( * ) wildcard. The DestinationPath parameter specifies the location for the archive file. The Draft. zip archive contains the root directory's files and subdirectories.

How do I make a compressed zip file in Windows?

Right-click on the file or folder.Select “Compressed (zipped) folder”. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.


2 Answers

A native way with latest .NET 4.5 framework, but entirely feature-less:

Creation:

Add-Type -Assembly "System.IO.Compression.FileSystem" ; [System.IO.Compression.ZipFile]::CreateFromDirectory("c:\your\directory\to\compress", "yourfile.zip") ; 

Extraction:

Add-Type -Assembly "System.IO.Compression.FileSystem" ; [System.IO.Compression.ZipFile]::ExtractToDirectory("yourfile.zip", "c:\your\destination") ; 

As mentioned, totally feature-less, so don't expect an overwrite flag.

like image 187
sonjz Avatar answered Oct 20 '22 18:10

sonjz


Here's a couple of zip-related functions that don't rely on extensions: Compress Files with Windows PowerShell.

The main function that you'd likely be interested in is:

function Add-Zip {     param([string]$zipfilename)      if(-not (test-path($zipfilename)))     {         set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))         (dir $zipfilename).IsReadOnly = $false       }      $shellApplication = new-object -com shell.application     $zipPackage = $shellApplication.NameSpace($zipfilename)      foreach($file in $input)      {              $zipPackage.CopyHere($file.FullName)             Start-sleep -milliseconds 500     } } 

Usage:

dir c:\demo\files\*.* -Recurse | Add-Zip c:\demo\myzip.zip 

There is one caveat: the shell.application object's NameSpace() function fails to open up the zip file for writing if the path isn't absolute. So, if you passed a relative path to Add-Zip, it'll fail with a null error, so the path to the zip file must be absolute.

Or you could just add a $zipfilename = resolve-path $zipfilename at the beginning of the function.

like image 21
voithos Avatar answered Oct 20 '22 19:10

voithos