以下代码会压缩以下文件:
c:\test\1.txt
c:\test\2.txt
c:\test\3.txt
进入test.zip,如:
1.txt
2.txt
3.txt
但我想要的是包含目录测试:
test\1.txt
test\2.txt
test\3.txt
关于如何做到这一点的任何想法?
$zipFilename = 'c:\test.zip'
$zipDir = 'c:\test'
Set-Content $zipFilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$shell = New-Object -ComObject Shell.Application
$zipPackage = $shell.NameSpace($zipFilename)
Get-ChildItem $zipDir -Recurse | foreach {
# echo $_.FullName
$zipPackage.CopyHere($_.FullName)
Start-Sleep -Milliseconds 1000
}
答案 0 :(得分:0)
你有理由循环浏览文件吗? 您可以跳过循环并直接压缩文件/文件夹结构
$zipFilename = 'c:\test.zip'
$zipDir = 'c:\test'
Set-Content $zipFilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$shell = New-Object -ComObject Shell.Application
$zipPackage = $shell.NameSpace($zipFilename)
$zipPackage.CopyHere($zipDir)
如果您有其他处理,则可以添加动态睡眠。每个文件等一下。
Sleep (Get-childitem $zipDir -recurse | Measure-Object | Select-Object -expandproperty Count)