我想在powershell中創建一個zip文件,將項目添加到zip文件中,然後在創建zip文件後立即獲取該zip文件的壓縮內容作爲字節,在同一個scipt。問題在於,它似乎並未將zip應用程序的內容寫入文件系統。如何關閉/清空zip應用程序,以便下一個powershell語句可以訪問新創建的zip文件?Powershell - 如何在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文件,從而獲取內容回報什麼。
請注意,這似乎是copyhere動作異步行爲的問題。如果我在copyhere行之後休息,則zip文件將被填充。但是,我不知道睡了多久,也不想推遲處理。
非常感謝提前!
我從來沒有從PowerShell創建存檔,但這看起來不像它可以工作。 – Joey 2012-02-09 12:58:57
看看這裏:http://dotnetzip.codeplex.com/。 Inoic.zip更適合使用zip文件! – 2012-02-09 13:01:09
它確實有效。如果你看看你執行這些語句的目錄,你會發現已經創建了一個test.zip文件。 – 2012-02-09 13:01:24