繁体   English   中英

Powershell - 如何在PowerShell中完成/关闭zip文件

[英]Powershell - How to complete/close a zip file in powershell

我想在powershell中创建一个zip文件,将项目添加到zip文件,然后在创建zip文件后立即将该zip文件的压缩内容作为字节,在同一个scipt中。 问题是,zip应用程序似乎没有将其内容写入文件系统。 如何关闭/刷新zip应用程序,以便下一个powershell语句可以访问新创建的zip文件?

例:

new-item -type File test.zip -force
$zip = ( get-item test.zip ).fullname
$app = new-object -com shell.application
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname )
dir test.zip # <---- Empty test.zip file
Get-Content -Encoding byte $zip | echo # <-- nothing echoed

“dir test.zip”显示没有内容的zip文件,因此Get-Content不返回任何内容。

请注意,这似乎是copyhere操作的异步行为的问题。 如果我在copyhere行之后睡觉,zip文件将被填充。 但是,我不知道必须睡多久,也不想延迟处理。

非常感谢提前!

我也有这个问题,你需要的只是等到压缩操作没有完成。 所以,我想出了这个解决方案,你应该在执行“$ folder.copyhere”或“Copy-ToZip”powerpack cmdlet后放置这段代码。

    $isCompleted = $false
    $guid = [Guid]::NewGuid().ToString()
    $tmpFileName = $zipFileName + $guid
    # The main idea is to try to rename target ZIP file. If it success then archiving operation is complete.
    while(!$isCompleted)
    {
        start-sleep -seconds 1
        Rename-Item $zipFileName $tmpFileName -ea 0 #Try to rename with suppressing errors
        if (Test-Path $tmpFileName)
        {
            Rename-Item $tmpFileName $zipFileName #Rename it back
            $isCompleted = $true
        }
    }

您可能想重新考虑使用第三方库。 但是,如果您必须使用copyhere ,请尝试以下操作:

new-item -type File test.zip -force
$zip = ( get-item test.zip ).fullname
$app = new-object -com shell.application
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname)
while($folder.Items().Item($item.Name) -Eq $null)
{
    start-sleep -seconds 0.01
    write-host "." -nonewline
}
dir test.zip # <---- Empty test.zip file
Get-Content -Encoding byte $zip 

尝试创建这样的zip空文件:

$zipfilename = "myzip.zip"
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

然后是你的其余代码。

我的方框中的代码有效:

$zipfilename = "a.zip"
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$app = new-object -com shell.application
$zip = ( get-item a.zip ).fullname
$folder = $app.namespace( $zip )
$item = new-item file.txt -itemtype file -value "Mooo" -force
$folder.copyhere( $item.fullname )

获取zip的内容使用此:

$zipfilename = "myzip.zip"
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$zipPackage.Items() | Select Path

在闭包中创建文件,以便变量超出范围并且powershell关闭文件...如果您将该部分放入子例程中,那么当您返回时它将自然关闭。

new-item -type File test.zip -force
{ 
  $zip = ( get-item test.zip ).fullname
  $app = new-object -com shell.application
  $folder = $app.namespace( $zip )
  $item = new-item file.txt -itemtype file -value "Mooo" -force
  $folder.copyhere( $item.fullname )
}
dir test.zip # <---- Empty test.zip file
Get-Content -Encoding byte $zip

压缩文件的这种方法不适用于自动脚本。 这在3台演出的Windows 2003和Windows xp服务器中有局限性。 此外,它没有给出正确的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM