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
Get-Content -Encoding byte $zip | echo
"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.
!