You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本需求:文件选择后加密压缩至指定路径

实现带密码的ZIP压缩PowerShell脚本

原生的.NET System.IO.Compression.ZipArchive 不支持密码加密,所以要实现带密码的ZIP压缩,需要借助第三方工具或库,以下是两种可行方案:

方案一:使用7-Zip(推荐,需提前安装)

7-Zip是免费开源的压缩工具,支持带密码的ZIP压缩,PowerShell可通过调用其命令行工具实现。

完整脚本:

Add-Type -AssemblyName System.Windows.Forms

# 初始化文件选择对话框
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    InitialDirectory = [Environment]::GetFolderPath('Desktop')
    Filter = '视频文件 (*.mp4)|*.mp4'
    Multiselect = $false  # 可改为$true允许多选文件
}

# 弹出对话框并判断是否选择了文件
if ($FileBrowser.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
    $sourceFile = $FileBrowser.FileName
    $outputDir = "C:\Temp"
    
    # 确保输出目录存在,不存在则创建
    if (-not (Test-Path $outputDir)) {
        New-Item -ItemType Directory -Path $outputDir | Out-Null
    }
    
    # 生成唯一的ZIP文件名(原文件名+时间戳)
    $fileName = [System.IO.Path]::GetFileNameWithoutExtension($sourceFile)
    $zipPath = Join-Path $outputDir "$fileName_$(Get-Date -Format 'yyyyMMddHHmmss').zip"
    
    # 7-Zip的默认安装路径,若你的安装路径不同请修改此处
    $7zipPath = "C:\Program Files\7-Zip\7z.exe"
    if (-not (Test-Path $7zipPath)) {
        Write-Error "未找到7-Zip,请确认已安装或修改脚本中的`$7zipPath`路径"
        exit 1
    }
    
    # 设置压缩密码,也可改为交互式输入:
    # $password = Read-Host "请输入压缩密码" -AsSecureString
    # $plainPassword = [System.Net.NetworkCredential]::new("", $password).Password
    $password = "YourSecurePassword"
    
    # 调用7-Zip执行加密压缩,-mx=5为压缩级别(1最快,9最高)
    & $7zipPath a -tzip -p"$password" -mx=5 "$zipPath" "$sourceFile"
    
    # 根据7-Zip的退出码判断是否成功
    if ($LASTEXITCODE -eq 0) {
        Write-Host "加密压缩完成,文件路径:$zipPath"
    } else {
        Write-Error "压缩失败,错误码:$LASTEXITCODE"
    }
} else {
    Write-Host "未选择任何文件"
}

方案二:使用SharpCompress库(无需额外安装工具)

SharpCompress是一个.NET库,支持带密码的ZIP压缩,可通过PowerShell模块直接使用。

完整脚本:

Add-Type -AssemblyName System.Windows.Forms

# 初始化文件选择对话框
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    InitialDirectory = [Environment]::GetFolderPath('Desktop')
    Filter = '视频文件 (*.mp4)|*.mp4'
    Multiselect = $false
}

if ($FileBrowser.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
    $sourceFile = $FileBrowser.FileName
    $outputDir = "C:\Temp"
    
    if (-not (Test-Path $outputDir)) {
        New-Item -ItemType Directory -Path $outputDir | Out-Null
    }
    
    $fileName = [System.IO.Path]::GetFileNameWithoutExtension($sourceFile)
    $zipPath = Join-Path $outputDir "$fileName_$(Get-Date -Format 'yyyyMMddHHmmss').zip"
    
    # 首次运行需安装SharpCompress模块,后续可注释该行
    if (-not (Get-Module -ListAvailable -Name SharpCompress)) {
        Install-Module -Name SharpCompress -Scope CurrentUser -Force
    }
    Import-Module SharpCompress
    
    # 设置密码,同样支持交互式输入
    $password = "YourSecurePassword"
    
    # 创建加密ZIP归档
    try {
        $archive = [SharpCompress.Archives.Zip.ZipArchive]::Create($zipPath)
        $archive.AddEntry([System.IO.Path]::GetFileName($sourceFile), $sourceFile, $password)
        $archive.Dispose()
        Write-Host "加密压缩完成,文件路径:$zipPath"
    } catch {
        Write-Error "压缩失败:$_"
    }
} else {
    Write-Host "未选择任何文件"
}

内容的提问来源于stack exchange,提问作者Terror Death

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 07:32:10