我想在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