Thursday 4 March 2010

How to Zip and UnZip Using Powershell

Here are some easy-to-use functions for zipping up a directory and unzipping using Powershell. The functions use the opensource SharpZipLib library, a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. So you'll need to download this and reference the SharpZipLib assembly in the appropriate place in the functions below.

function ZipFiles($sourcePath,$zipFilename)
{
   # Reference the SharpZipLib assembly here
   [System.Reflection.Assembly]::LoadFrom("C:\ICSharpCode.SharpZipLib.dll")

   $zip = New-Object ICSharpCode.SharpZipLib.Zip.FastZip
   $zip.CreateZip($zipFilename,$sourcePath, $true,"")
} 


function UnzipFiles($zipFilename,$destinationPath)
{ 
   # Reference the SharpZipLib assembly here
   [System.Reflection.Assembly]::LoadFrom("C:\ICSharpCode.SharpZipLib.dll")

   $zip = New-Object ICSharpCode.SharpZipLib.Zip.FastZip
   $zip.ExtractZip($zipFilename,$destinationPath,"")
} 

2 comments: