carlpett
carlpett

Reputation: 12613

Copy files from directory within zip file

Our build process results in a bunch of .zip files, such as ComponentA-1.2.3.4.zip, where 1.2.3.4 is the build number. This file in turn contains a folder ComponentA-1.2.3.4, which has the actual artifacts in a folder structure. I'm writing a powershell script which extracts the artifacts and sends them to a server for deploy, and need to put the structure beneath the top level one on the server. So, assuming this structure: ComponentA-1.2.3.4.zip\ComponentA-1.2.3.4\{Web,Lib,Bin}, I need to extract the Web, Lib and Bin folders.

I'm copying the files using this method at the moment

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

I've tried to manipulate the $zipPackage.Items() object, but get errors about the file not exising. The object looks like this in the debugger:

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

Just about anything I do result in Cannot find path 'D:\release\1.2.3.4\System.__ComObject' because it does not exist.

I'd like to not have to extract the file to some temporary location just to walk down one step, but I don't understand how to do it?

Upvotes: 0

Views: 2091

Answers (2)

zdan
zdan

Reputation: 29450

If you know the name of the parent folder within the zip file, you can give the shell com object that name instead. So in your case, something like:

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

Now, when you call $zipRoot.Items() you can get the individual sub folders of that root. You can semi-automate this root path generation by using the parent zip Objectto query for the name of the root within the zip file:

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

Upvotes: 2

user189198
user189198

Reputation:

Have you considered using a ZIP library, such as DotNetZip for this task?

http://dotnetzip.codeplex.com/

Upvotes: 0

Related Questions