PowerShell- adding files to a .zip archive, need to preserve the directory structure PowerShell- adding files to a .zip archive, need to preserve the directory structure powershell powershell

PowerShell- adding files to a .zip archive, need to preserve the directory structure


Based on my knowledge you cannot add to a zip file one specific file using shell.application and saving its folder structure. You have two choice:

  1. Add single files in a flat structure as your script do
  2. Add one folder and all its content ( this save the structure folderusing the folder added as a parent folder):
$Directory = Get-Item .$ParentDirectory = Get-Item ..$ZipFileName = $ParentDirectory.FullName  + $Directory.Name + ".zip"if (test-path $ZipFileName) {    echo "Zip file already exists at $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, as in my comment to your question, to use a safer way to create zip files programmatically as DotNetZip do (IMO).


Ended up using CSharpZipLib. Worked as I'd hoped.

Thanks everyone.