从zip文件中的目录复制文件

时间:2012-03-05 18:48:01

标签: powershell zip

我们的构建过程会产生一堆.zip文件,例如ComponentA-1.2.3.4.zip,其中1.2.3.4是内部版本号。该文件又包含一个文件夹ComponentA-1.2.3.4,其中包含文件夹结构中的实际工件。我正在编写一个powershell脚本,它提取工件并将它们发送到服务器进行部署,并且需要将结构放在服务器上的顶级服务器之下。因此,假设这个结构:ComponentA-1.2.3.4.zip\ComponentA-1.2.3.4\{Web,Lib,Bin},我需要提取Web,Lib和Bin文件夹。

我正在使用此方法复制文件

$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())

我试图操纵$zipPackage.Items()对象,但是文件的错误没有消失。对象在调试器中看起来像这样:

>>> $ZipPackage.Items()

Application  : System.__ComObject
Parent       : System.__ComObject
Name         : ComponentA-1.2.3.4
Path         : D:\Release\1.2.3.4\ComponentA-1.2.3.4.zip\ComponentA-1.2.3.4
GetLink      : 
GetFolder    : System.__ComObject
IsLink       : False
IsFolder     : True
IsFileSystem : False
IsBrowsable  : False
ModifyDate   : 2012-02-27 17:30:34
Size         : 0
Type         : File Folder

我所做的任何事情都会导致Cannot find path 'D:\release\1.2.3.4\System.__ComObject' because it does not exist

我不想将文件提取到某个临时位置只是为了走一步,但我不明白该怎么做?

2 个答案:

答案 0 :(得分:2)

如果您知道zip文件中父文件夹的名称,则可以为shell com对象指定该名称。所以在你的情况下,像:

$zipRoot = $shellApplication.NameSpace('ComponentA-1.2.3.4.zip\ComponentA-1.2.3.4')

现在,当您调用$ zipRoot.Items()时,您可以获取该根的各个子文件夹。您可以通过使用父zip对象查询zip文件中根的名称来半自动生成此根路径:

$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$zipRoot =  $shellApplication.NameSpace(($zipPackage.Items() | select -ExpandProperty Path)

答案 1 :(得分:0)

您是否考虑过使用ZIP库(如DotNetZip)来完成此任务?

http://dotnetzip.codeplex.com/