在自动构建的时候,最常用的就是压缩备份项目的源文件,PowerShell提供了相关命令。
Compress-Archive(压缩文件)
Compress-Archive[-Path] <String[]>[-DestinationPath] <String>[-CompressionLevel <String>][-PassThru][-WhatIf][-Confirm][<CommonParameters>]
参数说明:
举例:备份如下目录中的文件,但是不包含:node_modules 子目录
备份的zip文件位于: Z:\Documents\sensorwu\VUE\VUE3\Backup.zip
$compress = @{Path = "Z:\Documents\sensorwu\VUE\VUE3\szhn_app_vue3\dist", "Z:\Documents\sensorwu\VUE\VUE3\szhn_app_vue3\public","Z:\Documents\sensorwu\VUE\VUE3\szhn_app_vue3\src","Z:\Documents\sensorwu\VUE\VUE3\szhn_app_vue3\*.*"CompressionLevel = "Fastest"DestinationPath = "Z:\Documents\sensorwu\VUE\VUE3\Backup.zip"}Compress-Archive @compress
压缩后文件用winrar打开:
$compress = @{Path = "Z:\Documents\sensorwu\VUE\VUE3\szhn_app_vue3\*"CompressionLevel = "Fastest"DestinationPath = "Z:\Documents\sensorwu\VUE\VUE3\Backup.zip"}Compress-Archive @compress
Expand-Archive(解压缩文件)
Expand-Archive[-Path] <String>[[-DestinationPath] <String>][-Force][-PassThru][-WhatIf][-Confirm][<CommonParameters>]
举例:将上面备份的文件解压缩到:Z:\Documents\sensorwu\VUE\VUE3\szhn_Demo 目录中。
Expand-Archive -Path "Z:\Documents\sensorwu\VUE\VUE3\Backup.zip" -DestinationPath "Z:\Documents\sensorwu\VUE\VUE3\szhn_Demo"
解压后的效果:
在学习PowerShell之前,我使用的是WinRar的命令行模式,也可以实现压缩和解压缩功能,但是有个问题就是必须安装WinRar,使用PowerShell就无需安装WinRar程序了。