El TecnoBaúl de Kiquenet

Kiquenet boring stories

Archive for 8 junio 2012

Zip y Powershell

Posted by kiquenet en 8 junio 2012

Utilidades para comprimir en ZIP utilizando SharpZipLib y DotNetZip.

# http://stackoverflow.com/questions/1153126/how-to-create-a-zip-archive-with-powershell
$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Path

function Zip-SharpZipLib ([string]$deployzipFilePath, [string]$deployPath )
{
    <# [void] [System.Reflection.Assembly]::LoadWithPartialName("ICSharpCode.SharpZipLib")#>
    $zipLibraryPath =   [System.IO.Path]::GetFullPath(  [System.IO.Path]::Combine( $ScriptDirectory,  "ICSharpCode.SharpZipLib.dll" ));
    [void][System.Reflection.Assembly]::LoadFile($zipLibraryPath);
    $zip = new-object ICSharpCode.SharpZipLib.Zip.FastZip;
   
    "————– Deployment Script ————-"
    "Copy files to: $deployPath"
    "Zip file name: $deployZipFilePath"
    "——————————————————–"
    "Zipping up…."
    # zip the entire deployment folder and make one zip file
    $zip.CreateZip( $deployZipFilePath, $deployPath, [Boolean]"true", [string]::Empty );

    # Turn this on if you want to test if the zip is correct or not
    #$zip.ExtractZip( $deployZipFilePath, $deployPath+"_Extracted", [string]::Empty );
    "Zipping done…"
}

# http://dotnetzip.codeplex.com/wikipage?title=PS-Examples&referringTitle=Examples
# http://blogs.msdn.com/b/dotnetinterop/archive/2008/06/26/zip-creation-within-powershell.aspx
function Zip-DotNetZip ([string]$deployzipFilePath, [string]$deployPath )
{
    $zipLibraryPath =   [System.IO.Path]::GetFullPath(  [System.IO.Path]::Combine( $ScriptDirectory,  "Ionic.Zip.dll" ));
    [void][System.Reflection.Assembly]::LoadFrom($zipLibraryPath);

    $zipfile =  new-object Ionic.Zip.ZipFile
    $e= $zipfile.AddDirectory($deployPath, "home")
    $zipfile.Save("$deployzipFilePath")
    $zipfile.Dispose()
}

Anuncios

Posted in Scripts | Etiquetado: , | 5 Comments »