Creating a zipped/compressed folder in Windows using Powershell or the command line

后端 未结 6 1393
野性不改
野性不改 2020-12-13 04:56

I am creating a nightly database schema file and would like to put all the files created each night, one for each database, into a folder and compress that folder. I have a

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 05:43

    This compresses .\in contents to .\out.zip with System.IO.Packaging.ZipPackage following the example here

    $zipArchive = $pwd.path + "\out.zip"
    [System.Reflection.Assembly]::Load("WindowsBase,Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
    $ZipPackage=[System.IO.Packaging.ZipPackage]::Open($zipArchive, [System.IO.FileMode]"OpenOrCreate", [System.IO.FileAccess]"ReadWrite")
    $in = gci .\in | select -expand fullName
    [array]$files = $in -replace "C:","" -replace "\\","/"
    ForEach ($file In $files) {
       $partName=New-Object System.Uri($file, [System.UriKind]"Relative")
       $part=$ZipPackage.CreatePart($partName, "application/zip", [System.IO.Packaging.CompressionOption]"Maximum")
       $bytes=[System.IO.File]::ReadAllBytes($file)
       $stream=$part.GetStream()
       $stream.Write($bytes, 0, $bytes.Length)
       $stream.Close()
                                                        }
    $ZipPackage.Close()
    

提交回复
热议问题