tags:

views:

1243

answers:

6

Is it possible to create a zip archive using PowerShell?

Thank you.

+2  A: 

Here's one example of how you could do this.

Vinay Sajip
+1  A: 

Not out of the box but you can use SharpZipLib with PowerShell

Shay Erlichmen
+6  A: 

If you head on over to CodePlex and grab the PowerShell Community Extensions, you can use their write-zip cmdlet.

Matt Hamilton
Yep, and it uses 7z as the core library for most of its compression cmdlets. I know, becaues I implemented it ;) +1
x0n
lol nice work, x0n. I imlpemented the feed store provider in PSCX. Slightly less practical but tonnes of fun. :)
Matt Hamilton
+1  A: 

This is really obscure but works. 7za.exe is standalone version of 7zip and is available with install package.

# get files to be send
$logFiles = Get-ChildItem C:\Logging\*.* -Include *.log | where {$_.Name -match $yesterday} 

foreach ($logFile in $logFiles)
{
    Write-Host ("Processing " + $logFile.FullName)

    # compress file
    & ./7za.exe a -mmt=off ($logFile.FullName + ".7z") $logFile.FullName

}
Michal Sznajder
+3  A: 

For compression, I would use a standard library (7-zip is good like Michal suggests).

If you install 7-zip, the installed directory will contain 7z.exe which is a console application.
You can invoke it directly and use any compression option you want.

If you wish to engage with the DLL, that should also be possible.
7-zip is freeware and open source.

nik
Here is an example of using 7 zip with AES encryption from Powershell: http://codeblog.theg2.net/2010/02/powershell-7-zip-amazon-s3-upload.html
Greg Bray
A: 

What about System.IO.Packaging.ZipPackage? Requires .NET framework version >= 3.0.

Peter P.