Merge Zip Files to 7z

This script will retrieve all zip files in a directory, unzip them one at a time into new directories, then add those directories to a .7z file. Useful if you have a collection of zip files with mostly the same contents, where you’ll benefit from combining them together, but you don’t have enough disk space to unzip all of them at once.

param(
    [Parameter(Position=0,Mandatory)]
    [string]$OutputBaseFileName
)

# Set these two constant values:

$SevenZipExe = "C:\Program Files\7-zip\7z.exe"
$Dir = "C:\temp\je"

$ZipFiles = Get-ChildItem -Path "$($Dir)\*" -Include "*.zip"
$OutputFile = "$($Dir)\$($OutputBaseFileName).7z"

$ZipFiles | ForEach-Object {
    $ZipFileName = $_.FullName
    $DirName = $_.FullName -replace "\.zip$", ""
    Start-Process -FilePath $SevenZipExe -ArgumentList "x `"$ZipFileName`" -o`"$DirName`"" -Wait -NoNewWindow
    Start-Process -FilePath $SevenZipExe -ArgumentList "a `"$OutputFile`" `"$DirName`"" -Wait -NoNewWindow
    Remove-Item -Recurse -Path $DirName
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s