开发者

"Bundle" a folder (e.g. Create an uncompressed zip) from PowerShell

I am doing a nightly backup of all files modified in the last day, using PowerShell.

The goal is to create an uncompressed zip (or any other format) that will group everything in the backup folder into one file, using PowerShell.

The following code works great for compression but it is far too slow:

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 1000
        #开发者_运维知识库500 milliseconds was too short.... 
    }
}

Any ideas?

Thanks!


I would recomend using powershell in conjunction with 7-Zip Command line. 7-Zip has a command line option that allows for No Compression.

-mx0


The PowerShell Community Extensions has a Write-Tar cmdlet that might be of use here.


Borrowing from Eld's answer to another question, I came up with:

function ZipFiles( $zipfilename, $sourcedir )
{
   [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
   $compressionLevel = [System.IO.Compression.CompressionLevel]::NoCompression
   [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
        $zipfilename, $compressionLevel, $false)
}

Eld also says of his solution, which applies here as well:

A pure Powershell alternative that works with Powershell 3 and .NET 4.5 (if you can use it):

The change from his answer was to specify NoCompression instead of Optimal for the level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜