首页 > 系统服务 > 详细

Compress a folder using powershell

时间:2017-01-16 17:44:16      阅读:264      评论:0      收藏:0      [点我收藏+]

There are many ways to compress a folder using powershell:

Method 1: Using System.IO.Compression and System.IO.Compression.FileSystem

Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem

[System.IO.Compression.ZipFile]::CreateFromDirectory($source_WebPortal, $destination_WebPortal,[System.IO.Compression.CompressionLevel]::Fastest,$True)

This method requires .NET Framework 4.5 installed

Method 2: Using pure Powershell script

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 200
    }
}

dir $tempFolder\*.* -Recurse | add-Zip $zipFile

This method can be run at powershell 2.0 and .NET Framework 4.0

Method 3: Using PowerShell Community Extensions

1) Download the extension from http://pscx.codeplex.com/

2) Unzip and put the folder in $PSHome\Modules

3) Execute cmdlet import-module pscx

This method requires administator permission

Compress a folder using powershell

原文:http://www.cnblogs.com/liangzi4000/p/6290280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!