Search code examples
powershellzip7zip

Move files within a ZIP archive


My Eclipse and Maven development environments are using a different directory structure, such that if I export my results into a JAR / ZIP file, the required directories are included, but at the wrong directory location.

Is there a way, in a PowerShell script on Windows, to move directories within a ZIP file?

If relevant, you may assume that 7-Zip is installed on the system.


Solution

  • There are a variety of PowerShell modules such as 7Zip4Powershell that can help using libraries such as 7-Zip from PowerShell easier.

    However, moving a single file inside of a Zip file is easy with just 7-Zip. Here is an example:

    & "T:\Program Files\7-Zip\7z.exe" rn MyWebApp.zip DirOne\SomeClass.jar DirTwo\SomeClass.jar
    

    The above command will move SomeClass.jar from the DirOne directory to the DirTwo directory inside of the MyWebApp.zip.

    Of course if you need to move more than just one file then you could use 7Zip4Powershell to get a list of file names to move:

    Install-Module 7Zip4Powershell
    Import-Module 7Zip4Powershell
    
    Get-7Zip MyWebApp.zip | ? FileName -match '^DirOne\\' | % {
        & "T:\Program Files\7-Zip\7z.exe" rn MyWebApp.zip $_.FileName ($_.Filename -replace "DirOne", "DirTwo")
    }