How to create a zip archive with PowerShell?
是否有可能使用PowerShell创建一个zip压缩文件?
If you head on over to CodePlex and grab the PowerShell Community Extensions, you can use their write-zip
cmdlet.
Since
CodePlex is in read-only mode in preparation for shutdown
you can go to PowerShell Gallary.
A pure Powershell alternative that works with Powershell 3 and .NET 4.5 (if you can use it):
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)
}
Just pass in the full path to the zip archive you would like to create and the full path to the directory containing the files you would like to zip.
PowerShell v5.0 adds Compress-Archive
and Expand-Archive
cmdlets. The linked pages have full examples, but the gist of it is:
# 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
链接地址: http://www.djcxy.com/p/57018.html