powershell 下怎么压缩zip文件

2025-03-10 16:02:43
推荐回答(1个)
回答1:


#注意依赖.net 4.5 及以上。

#压缩

add-Type -AssemblyName 'System.IO.Compression.Filesystem'

$源目录 = 'a:\aaa\bbb'

$目标文件 = 'a:\ccc.zip'

[System.IO.Compression.ZipFile]::CreateFromDirectory($源目录,$目标文件)


#下面这个函数 只打包 并不压缩

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 500

}}