Powershell - How to complete / close a zip file in PowerShell

I want to create a zip file in powershell, add elements to a zip file, and then get the compressed content of this zip file as bytes right after creating the zip file in the same scipt. The problem is that it does not seem that the zip application has written its contents to the file system. How to close or close a zip application so that the next powershell statement can access the newly created zip file?

Example:

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" shows a zip file without content, so Get-Content returns nothing.

Note that this seems to be a problem with the asynchronous behavior of the copyhere action. If I slept after the copyhere line, the zip file will be populated. However, I do not know how long to sleep, and I do not want to postpone treatment.

!

+5
5

zip . Windows 2003 Windows xp server 3 . , .

0

, . , 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 
+1

, , , - , zip . , , "$ folder.copyhere" "Copy-ToZip" powerpack.

    $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
        }
    }
+1

:

$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
0

Create the file in close so that the variable disappears from the scope and powershell closes the file ... if you did this part in a routine, then it would be natural when you return.

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
0
source

All Articles