Create / extract zip file and overwrite existing files / contents

Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('foo', 'foo.zip')
[IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar')

I found code for creating and extracting ZIP files using PowerShell from this answer , but due to my low reputation I can not ask the question as a comment on this answer.

  • Creature. How to overwrite an existing ZIP file without user intervention?
  • Extract - How to overwrite existing files and folders without user intervention? (Preferably as a robocopys function mir).
+16
source share
2 answers

PowerShell .zip .NET 5 . Compress-Archive -Path string[], / .


:

Compress-Archive -Path C:\Foo -DestinationPath C:\Foo.zip -CompressionLevel Optimal -Force

-Update.

:

Expand-Archive -Path C:\Foo.zip -DestinationPath C:\Foo -Force
+21

PowerShell 5

@Ola-M .

function Unzip($zipfile, $outdir)
{
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $archive = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
    foreach ($entry in $archive.Entries)
    {
        $entryTargetFilePath = [System.IO.Path]::Combine($outdir, $entry.FullName)
        $entryDir = [System.IO.Path]::GetDirectoryName($entryTargetFilePath)

        #Ensure the directory of the archive entry exists
        if(!(Test-Path $entryDir )){
            New-Item -ItemType Directory -Path $entryDir | Out-Null 
        }

        #If the entry is not a directory entry, then extract entry
        if(!$entryTargetFilePath.EndsWith("\")){
            [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entryTargetFilePath, $true);
        }
    }
    $archive.Dispose()
}

Unzip -zipfile "$zip" -outdir "$dir"
+4

Source: https://habr.com/ru/post/1683448/


All Articles