Windows压缩文件可以用 PowerShell中的Compress-Archive
压缩单个文件
Compress-Archive -Path "D:\tmp\test.txt" -DestinationPath "D:\tmp\test.zip"
压缩多个文件
Compress-Archive -Path "D:\tmp\test.txt","D:\tmp\test2.txt" -DestinationPath "D:\tmp\test2.zip"
压缩文件夹
Compress-Archive -Path "D:\tmp\testFolder" -DestinationPath "D:\tmp\testFolder.zip"
Compress-Archive 是 PowerShell 中的一个命令,用于创建压缩文件(如 ZIP 文件)。以下是关于 Compress-Archive 的基本使用和参数说明:
基本使用
使用 Compress-Archive 命令,你可以将一个或多个文件或文件夹压缩成一个 ZIP 文件。
语法
powershell
Compress-Archive -Path <string[]> -DestinationPath <string> [-CompressionLevel <CompressionLevel>] [-Force] [-Update] [-WhatIf] [-Confirm] [<CommonParameters>]
参数说明
-Path <string[]>: 指定要压缩的文件或文件夹的路径。可以指定多个路径,使用逗号分隔。-DestinationPath <string>: 指定压缩文件的输出路径和文件名。-CompressionLevel <CompressionLevel>: 设置压缩级别。可选项有:Fastest、NoCompression、Optimal。默认为Optimal。-Force: 强制覆盖现有的目标文件,而不提示。-Update: 如果目标压缩文件已存在,并且-Path参数中的某些文件比压缩文件中的相应文件新,则更新压缩文件。-WhatIf: 显示如果执行命令将发生什么,但实际上不执行命令。-Confirm: 在执行命令之前提示确认。
示例
压缩单个文件
powershell
Compress-Archive -Path "C:\Files\Document.txt" -DestinationPath "C:\Archives\Documents.zip"
压缩多个文件
powershell
Compress-Archive -Path "C:\Files\Document1.txt", "C:\Files\Document2.txt" -DestinationPath "C:\Archives\Documents.zip"
压缩文件夹
powershell
Compress-Archive -Path "C:\Files\FolderToCompress" -DestinationPath "C:\Archives\FolderArchive.zip"
使用压缩级别
powershell
Compress-Archive -Path "C:\Files\FolderToCompress" -DestinationPath "C:\Archives\FolderArchive.zip" -CompressionLevel Fastest
注意事项
- 如果目标路径(
-DestinationPath)不存在,Compress-Archive会尝试创建它。 - 如果
-Path参数指定的某个文件或文件夹不存在,Compress-Archive会抛出一个错误。
确保你拥有足够的权限来读取源文件和写入目标文件,否则命令可能会失败。
使用 Compress-Archive 可以方便地创建压缩文件,从而节省存储空间并方便文件传输。

浙公网安备 33010602011771号