PowerShell- adding files to a .zip archive, need to preserve the directory structure -
i'm using following function add files .zip archive works fine, need able include parent directory of files. ideas?
function add-zipfile { param([string]$zipfilename, [string]$filter) if(-not (test-path($zipfilename))) { set-content $zipfilename ("pk" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipfilename).isreadonly = $false } $shellapplication = new-object -com shell.application $zippackage = $shellapplication.namespace($zipfilename) $files = get-childitem -filter "$filter" -recurse foreach($file in $files) { $zippackage.copyhere($file.fullname) start-sleep -milliseconds 500 } }
based on knowledge cannot add zip file 1 specific file using shell.application , saving folder structure. have 2 choice:
- add single files in flat structure script do
- add 1 folder , content ( save structure folder using folder added parent folder):
$directory = get-item . $parentdirectory = get-item .. $zipfilename = $parentdirectory.fullname + $directory.name + ".zip" if (test-path $zipfilename) { echo "zip file exists @ $zipfilename" return } set-content $zipfilename ("pk" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipfilename).isreadonly = $false $zipfile = (new-object -com shell.application).namespace($zipfilename) $zipfile.copyhere($directory.fullname) i suggest, in comment question, use safer way create zip files programmatically dotnetzip (imo).
Comments
Post a Comment