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


当前回答

为什么没人看文档?每个人都引用的. net 4.5库允许您做任何想做的事情,包括创建空ZIP并向其中添加单个文件。

请看下面的代码示例:

# Load the .NET assembly
Add-Type -Assembly 'System.IO.Compression'
Add-Type -Assembly 'System.IO.Compression.FileSystem'

# Must be used for relative file locations with .NET functions instead of Set-Location:
[System.IO.Directory]::SetCurrentDirectory('.\Desktop')

# Create the zip file and open it:
$z = [System.IO.Compression.ZipFile]::Open('z.zip', [System.IO.Compression.ZipArchiveMode]::Create)

# Add a compressed file to the zip file:
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($z, 't.txt', 't.txt')

# Close the file
$z.Dispose()

下面是一个关于如何操作zip存档的概述(只是记得之后关闭文件):

You can compress files by specifying a fourth parameter for CreateEntryFromFile(...). Creating an entry returns a ZipArchiveEntry. This object lets you inspect the zipped file afterwards including letting you report the .CompressedLength, view or change the .LastWriteTime (needs Update mode), and more below. If you need to inspect the ZIP archive later, you can access its .Entries property, and use the methods above as well as view the filename, the full path, the decompressed size, or delete the file (needs Update mode). You can extract an archive two ways later. First open it, then extract either the entire archive or an individual entry (from .Entries or .GetEntry(...)). You can also extract an archive by its filename alone. If you need to work with streams, you can create an empty entry and open its stream for writing afterwards. You can also modify an existing zip entry (from .Entries or .GetEntry(...)), which would let you do everything in-memory.

我鼓励您浏览文档,因为这是我找到所有这些的方法。

其他回答

如果您前往CodePlex并获取PowerShell社区扩展,您可以使用他们的write-zip cmdlet。

CodePlex处于只读模式,准备关闭

你可以去PowerShell画廊。

最新。net 4.5框架的原生方式,但完全没有特性:

创造:

Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory("c:\your\directory\to\compress", "yourfile.zip") ;

提取:

Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::ExtractToDirectory("yourfile.zip", "c:\your\destination") ;

如前所述,完全没有特性,所以不要期望有覆盖标志。

更新:下面是其他开发者多年来在此基础上扩展的内容。

自最初的答案发布以来,很多事情都发生了变化。下面是一些使用Compress-Archive命令的最新示例。

命令通过压缩Draftdoc.docx和diagram2两个文件来创建新的归档文件Draft.zip。vsd,由Path参数指定。为此操作指定的压缩级别为“最佳”。

Compress-Archive -Path C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip

命令通过压缩Draft doc.docx和Diagram[2]这两个文件来创建新的归档文件Draft.zip。vsd,由LiteralPath参数指定。为此操作指定的压缩级别为“最佳”。

Compress-Archive -LiteralPath 'C:\Reference\Draft Doc.docx', 'C:\Reference\Images\Diagram [2].vsd'  -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip

命令在C:\Archives文件夹中创建新的归档文件Draft.zip。新的归档文件包含C:\Reference文件夹中的每个文件,因为在Path参数中使用通配符代替了特定的文件名。

Compress-Archive -Path C:\Reference\* -CompressionLevel Fastest -DestinationPath C:\Archives\Draft

命令从整个文件夹C:\Reference创建一个存档

Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft

PowerShell自动将.zip扩展名附加到文件名。

如果你安装了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不创建归档内的完整目录路径

一个纯PowerShell的替代方案,与PowerShell 3和。net 4.5(如果你可以使用它):

function ZipFiles( $zipfilename, $sourcedir )
{
   Add-Type -Assembly System.IO.Compression.FileSystem
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
        $zipfilename, $compressionLevel, $false)
}

只需传入您想要创建的zip归档文件的完整路径,以及包含您想要压缩的文件的目录的完整路径。