pet*_*snd 253
适用于PowerShell 3和.NET 4.5的纯PowerShell替代方法(如果可以使用它):
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)
}
Run Code Online (Sandbox Code Playgroud)
只需传递您想要创建的zip存档的完整路径以及包含您要压缩的文件的目录的完整路径.
Bra*_*bby 225
PowerShell v5.0添加Compress-Archive和Expand-Archivecmdlet.链接的页面有完整的例子,但它的要点是:
# Create a zip file with the contents of C:\Stuff\
Compress-Archive -Path C:\Stuff -DestinationPath archive.zip
# Add more files to the zip file
# (Existing files in the zip file with the same name are replaced)
Compress-Archive -Path C:\OtherStuff\*.txt -Update -DestinationPath archive.zip
# Extract the zip file to C:\Destination\
Expand-Archive -Path archive.zip -DestinationPath C:\Destination
Run Code Online (Sandbox Code Playgroud)
Mat*_*ton 120
如果您前往CodePlex并获取PowerShell社区扩展,则可以使用其write-zipcmdlet.
以来
CodePlex处于只读模式,准备关机
你可以去PowerShell Gallery.
son*_*njz 57
采用最新.NET 4.5框架的本机方式,但完全没有功能:
创建:
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory("c:\your\directory\to\compress", "yourfile.zip") ;
Run Code Online (Sandbox Code Playgroud)
萃取:
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::ExtractToDirectory("yourfile.zip", "c:\your\destination") ;
Run Code Online (Sandbox Code Playgroud)
如上所述,完全没有功能,所以不要指望覆盖标志.
更新:请参阅下文,了解多年来对此进行了扩展的其他开发人员......
Kar*_*non 44
安装7zip(或者下载命令行版本)并使用此PowerShell方法:
function create-7zip([String] $aDirectory, [String] $aZipfile){
[string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe";
[Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-r";
& $pathToZipExe $arguments;
}
Run Code Online (Sandbox Code Playgroud)
你可以这样称呼它:
create-7zip "c:\temp\myFolder" "c:\temp\myFolder.zip"
Run Code Online (Sandbox Code Playgroud)
noa*_*oam 15
编辑二 - 这段代码是古代丑陋丑陋的代码.你不想要.
这将压缩的内容.\in,以.\out.zip与System.IO.Packaging.ZipPackage下面的例子在这里
$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()
Run Code Online (Sandbox Code Playgroud)
编辑: 对于较大的文件不可靠,可能> 10mb,YMMV.东西 做与应用程序域证据和独立存储.友好的.NET 4.5 方法在PS v3中运行良好,但在我的情况下需要更多的内存.要从PS v2使用.NET 4,配置文件需要不受支持的 调整.
小智 12
给下面另一个选项.这将压缩一个完整的文件夹,并将存档写入给定名称的给定路径.
需要.NET 3或更高版本
Add-Type -assembly "system.io.compression.filesystem"
$source = 'Source path here'
$destination = "c:\output\dummy.zip"
If(Test-path $destination) {Remove-item $destination}
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
Run Code Online (Sandbox Code Playgroud)
对于压缩,我会使用一个库(7-Zip就像Michal建议的那样好).
如果安装7-Zip,则安装的目录将包含7z.exe哪个是控制台应用程序.
您可以直接调用它并使用您想要的任何压缩选项.
如果您希望使用DLL,那也应该是可能的.
7-Zip是免费软件和开源.
以下是PowerShell v5的本机解决方案,使用Compress-Archive 使用PowerShell创建Zip文件的cmdlet .
另请参阅Microsoft Docs for Compress-Archive.
例1:
Compress-Archive `
-LiteralPath C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd `
-CompressionLevel Optimal `
-DestinationPath C:\Archives\Draft.Zip
Run Code Online (Sandbox Code Playgroud)
例2:
Compress-Archive `
-Path C:\Reference\* `
-CompressionLevel Fastest `
-DestinationPath C:\Archives\Draft
Run Code Online (Sandbox Code Playgroud)
例3:
Write-Output $files | Compress-Archive -DestinationPath $outzipfile
Run Code Online (Sandbox Code Playgroud)
怎么样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()
Run Code Online (Sandbox Code Playgroud)
为什么没人看文档?每个人都引用的同一个 .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()
Run Code Online (Sandbox Code Playgroud)
这是有关如何在使用 zip 存档时操作它的概述(请记住之后关闭文件):
CreateEntryFromFile(...)。ZipArchiveEntry. 此对象允许您随后检查压缩文件,包括让您报告.CompressedLength、查看或更改.LastWriteTime(需求Update模式)等。.Entries属性,并使用上述方法以及查看文件名、完整路径、解压缩大小或删除文件(需要Update模式)。.Entries或.GetEntry(...))。您还可以仅通过文件名提取存档。.Entries或.GetEntry(...)),这将使您可以在内存中执行所有操作。我鼓励您浏览文档,因为这就是我发现所有这些的方式。
自发布初始答案以来,批次已更改。以下是一些使用Compress-Archive命令的最新示例。
命令来创建新的存档文件,Draft.zip通过压缩两个文件,Draftdoc.docx并且diagram2.vsd,由指定的Path参数。为此操作指定的压缩级别为“最佳”。
Compress-Archive -Path C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip
Run Code Online (Sandbox Code Playgroud)
命令来创建新的存档文件,Draft.zip通过压缩两个文件,Draft doc.docx并且Diagram [2].vsd,由指定的LiteralPath参数。为此操作指定的压缩级别为“最佳”。
Compress-Archive -LiteralPath 'C:\Reference\Draft Doc.docx', 'C:\Reference\Images\Diagram [2].vsd' -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip
Run Code Online (Sandbox Code Playgroud)
Draft.zip在C:\Archives文件夹中创建新存档文件的命令。新的存档文件包含C:\ Reference文件夹中的每个文件,因为使用通配符代替了Path参数中的特定文件名。
Compress-Archive -Path C:\Reference\* -CompressionLevel Fastest -DestinationPath C:\Archives\Draft
Run Code Online (Sandbox Code Playgroud)
Command从整个文件夹创建档案, C:\Reference
Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft
Run Code Online (Sandbox Code Playgroud)
PowerShell将.zip扩展名自动附加到文件名。
| 归档时间: |
|
| 查看次数: |
277021 次 |
| 最近记录: |