Create ZIP archives


posted by Tobias Weltner
12-01-2008

Downloads: 855
File size: 1.4kB
Views: 11,209

Embed
Create ZIP archives
  1. function Create-myZIP
  2.     param($path, [switch]$includeSubdir, $root = ''
  3.  
  4.  
  5.     begin { 
  6.         Write-Progress "Creating ZIP" "initializing $path" 
  7.         [System.Reflection.Assembly]::LoadFile("$home\ICSharpCode.SharpZipLib.dll") | Out-Null 
  8.         $item = New-Item $path -type File -force 
  9.         $archive = New-Object ICSharpCode.SharpZipLib.Zip.ZipOutputStream(($item.OpenWrite())) 
  10.         if ($root.length -gt 1  )  {$root = Split-Path -noQualifier $root
  11.     
  12.  
  13.     process { 
  14.         if ($_.psIsContainer -ne $true) { 
  15.             Write-Progress "Adding File to ZIP '$path':" "$($_.FullName)" 
  16.             if ($includeSubDir) { 
  17.                 $name = Split-Path -noQualifier $_.fullname 
  18.                 if ($name.startswith($root)) {  
  19.                     $name = $name.subString($root.length
  20.                 }  
  21.                 $name = $name.subString(1) 
  22.             } else {  
  23.                 $name = $_.Name 
  24.             
  25.  
  26.             $file = New-Object icsharpcode.SharpZipLib.Zip.ZipEntry $name 
  27.             $archive.PutNextEntry($file
  28.             [byte[]]$content = Get-Content $_.FullName -encoding Byte -readCount 4GB 
  29.             if ($content.Length -gt 0) { 
  30.                 $archive.write($content, 0, $content.length
  31.             
  32.         
  33.     
  34.  
  35.     end { 
  36.         Write-Progress "Creating ZIP" "Done." 
  37.         $archive.close() 
  38.     
  39.  
  40. # create flat archive with only scripts from top level folder: 
  41. dir $home\*.ps1 | Create-myZIP "$home\aFlat.zip"  
  42.  
  43. # create hierarchical zip with subfolders, setting relative root to $home: 
  44. dir $home\Documents *.ps1 -recurse | Create-myZIP "$home\aAll.zip" -includeSubDir -root $home\Documents 
Filed under: , , ,
Uses an external free ZIP library to create flat and hiearchical ZIP files right from the PowerShell pipeline. Get the free ZIP component here: http://www.icsharpcode.net/OpenSource/SharpZipLib/

Comments

Sylvain LESIRE wrote re: Create ZIP archives
on 12-12-2008 6:50 AM

Hi Tobias,

I was found a script which permits to create Zip at :

blogs.inetium.com/.../295.aspx

dscott98 wrote re: Create ZIP archives
on 04-23-2009 6:48 PM

Great example on how to create an archive, any chance of an example on extracting files?  I've been trying for a couple days with both the SharpZip library, and DotNetZip, and am unable to extract files.

Richard Giles wrote re: Create ZIP archives
on 04-23-2009 10:11 PM

One way is to use the Shell.Application com object:

$sh = new-object -com shell.application

# Where the .zip is

$zipfolder = $sh.namespace("C:\foo\foo.zip")

# The item in the zip

$item = $zipfolder.parsename("foo.txt")  

# Where the item is to go

$targetfolder = $sh.namespace("C:\foo")      

# Copy zipped file item to target folder

$targetfolder.copyhere($item)

dscott98 wrote re: Create ZIP archives
on 04-24-2009 12:36 PM

That works on smaller files, but i run into basically timeout errors when compressing large files.  The ones I am working with are 500mb+ and I'm adding multiples to a single zip archive.

Richard Giles wrote re: Create ZIP archives
on 04-25-2009 1:18 PM

How about this using ICSharpCpde:

$sourceFile = get-item "C:\foo\foo.zip"

$destDir = get-item "C:\foo"

[System.Reflection.Assembly]::LoadFile("C:\foo\ICSharpCode.SharpZipLib.dll")

$unzip = new-object ICSharpCode.SharpZipLib.Zip.FastZip

$unzip.ExtractZip($sourceFile, $destDir,"")

If this does not work, the issue may not be the method you are using but instead be environmental. Such as doing this across a network connection. You alluded to timeouts which is why I am guess it may be network related.

dscott98 wrote re: Create ZIP archives
on 04-29-2009 5:42 PM

Everything seems to be working as long as the SharpZipLib DLL is kept on a local disk.  If this is loaded from a network share, that is when I run into issues.  Any idea's on ways to fix this?  I'd like to keep my code in one file share for all servers to access.

Richard Giles wrote re: Create ZIP archives
on 04-29-2009 7:52 PM

You may want to check this post on Lee Holmes site to see if it helps you any:

www.leeholmes.com/.../CommentView,guid,dfa842ed-8a24-4b9b-aab8-f31df8035585.aspx

dscott98 wrote re: Create ZIP archives
on 05-01-2009 3:11 PM

I checked with a couple of our .NET developers and apparently this is a .NET issue, not a powershell issue.  There are ways around it, but none of them are worth the trouble.  They all recommended that I just copy the DLL to the machine I will be running from.

Thanks for all the help.

Thomas Maierhofer wrote re: Create ZIP archives
on 04-14-2010 1:22 AM

I've created a powershell zip file module. It is available under

powershellzip.codeplex.com

There are cmdlets to export-zip and import-zip files

Give it a try.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.