是否可以使用PowerShell创建zip存档?


当前回答

编辑二——这段代码是一个非常丑陋的旧代码。你不会想要的。

这将使用system . io . package . zippackage将。\in文件的内容压缩为。\out.zip

$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()

编辑:不可靠的大文件,可能>10mb, YMMV。与appdomain证据和隔离存储有关。更友好的。net 4.5方法在PS v3上工作得很好,但在我的情况下需要更多的内存。要从PS v2使用. net 4,配置文件需要一个不受支持的调整。

其他回答

旧线程,但仍然,我到了这里:)

这不是最初问题的答案,但也许有人会发现如何用PS创建ZipArchive对象是有用的。

# Example, if you have like I have a $myByteArray byte[] with the compressed data:
Add-Type -AssemblyName System.IO.Compression.FileSystem

# Fixed length stream:
$strm = New-Object -TypeName System.IO.MemoryStream -ArgumentList @(, $myByteArray);

# Create ZipArchive object
$arch = [System.IO.Compression.ZipArchive]::new($strm);

# List entries
$arch.Entries

这里有一个完整的命令行示例,可以从cmd.exe或ssh或您想要的任何地方启动!

powershell.exe -nologo -noprofile -command "&{ Add-Type -A 'System.IO.Compression.FileSystem' [System.IO.Compression.ZipFile]::CreateFromDirectory('c:/path/to/source/folder/', 'c:/path/to/output/file.zip');}"

问候

Windows下压缩解压目录的完整命令行如下:

压缩: powershell.exe -nologo -noprofile -command "&{添加- type -A 'System.IO.Compression.FileSystem';[IO.Compression.ZipFile]:: CreateFromDirectory (C: \印度河,“C: \ Indus.zip”);}” 提取: powershell.exe -nologo -noprofile -command "& {Add-Type -A 'System.IO.Compression.FileSystem';[IO.Compression.ZipFile]::ExtractToDirectory('C:\ industrial .zip','C:\Indus');}”

如果你安装了WinRAR:

function ZipUsingRar([String] $directory, [String] $zipFileName)
{
  Write-Output "Performing operation ""Zip File"" on Target ""Item: $directory Destination:"
  Write-Output ($zipFileName + """")
  $pathToWinRar = "c:\Program Files\WinRAR\WinRar.exe";
  [Array]$arguments = "a", "-afzip", "-df", "-ep1", "$zipFileName", "$directory";
  & $pathToWinRar $arguments;
}

参数的含义:afzip创建zip归档,df删除文件,ep1不创建归档内的完整目录路径

System.IO.Packaging.ZipPackage呢?

它需要。net 3.0或更高版本。

#Load some assemblys. (No line break!)
[System.Reflection.Assembly]::Load("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

#Create a zip file named "MyZipFile.zip". (No line break!)
$ZipPackage=[System.IO.Packaging.ZipPackage]::Open("C:\MyZipFile.zip",
   [System.IO.FileMode]"OpenOrCreate", [System.IO.FileAccess]"ReadWrite")

#The files I want to add to my archive:
$files = @("/Penguins.jpg", "/Lighthouse.jpg")

#For each file you want to add, we must extract the bytes
#and add them to a part of the zip file.
ForEach ($file In $files)
{
   $partName=New-Object System.Uri($file, [System.UriKind]"Relative")
   #Create each part. (No line break!)
   $part=$ZipPackage.CreatePart($partName, "",
      [System.IO.Packaging.CompressionOption]"Maximum")
   $bytes=[System.IO.File]::ReadAllBytes($file)
   $stream=$part.GetStream()
   $stream.Write($bytes, 0, $bytes.Length)
   $stream.Close()
}

#Close the package when we're done.
$ZipPackage.Close()

通过安德斯·赫塞尔博姆