Home > Software engineering >  Compress-Archive -Update doesn't update the file?
Compress-Archive -Update doesn't update the file?

Time:11-23

I'm trying to create a simple script to update a file within a zip, but running into some issues. MyZip.zip contains some subdirectories, and the file I want to update (myFile.txt) is inside dir1/dir2/dir3:

initialZip

I'm then running the following command to try and update this file with a new version of myFile.txt (which is nested inside dir1/dir2/dir3 on my file system so that it gets updated into the right directory in the zip):

Compress-Archive -Path .\dir1 -Update -DestinationPath .\myZip.zip

But this is resulting in the new version of the file being added to the directory, but not replacing the old version:

DuplicatedFile

What doesn't make sense after this, is that when I then go and update the source file, and rerun the same command, the new version of the file is updated correctly, and it doesn't make a third one?

UpdatedDuplicate

Why wouldn't this work for the original file in the zip? Am I doing something wrong, or do I need to change how I'm doing this?

Thanks in advance.

CodePudding user response:

-Path parameter must reference same directory structure as it is found in zip file.

My test zip has the following directory structure:

test.zip
-> a
-> -> b
-> -> -> c
-> -> -> -> test.txt

My reference directory which I want to merge into existing zip file for update has the following directory structure:

a
-> b
-> -> c
-> -> -> test.txt

Then I edit test.txt in a\b\c folder with new contents.

To put that update into test.txt in the zip archive I run:

Compress-Archive -Path .\a -DestinationPath .\test.zip -Update

And it works just fine.

CodePudding user response:

Workaround that I have found is to use the 7zip CLI to recursively delete the initial file:

./7z.exe d 'C:\Users\tboyle\Downloads\myZip.zip' 'myFile.txt' -r

And then use Compress-Archive to just add the new file in:

Compress-Archive -Path .\dir1 -Update -DestinationPath .\myZip.zip
  • Related