如何将单个txt文件移动到PowerShell 3中的zip

Mee*_*101 5 powershell

我正在尝试将一个文本文件复制/移动到zip.我不想解压缩它,复制文件并将其压缩回来.我们有什么办法可以直接将文本文件复制或移动到PowerShell中的zip文件中.当我在powershell中这样做的时候,它正在那之后,当我试图查看拉链时,它说的是无效的路径.

Powershell命令:

$A = "20160914.4"

New-Item "C:\test\VersionLabel.txt" -ItemType file

$A | Set-Content "C:\test\VersionLabel.txt"

Copy-Item "C:\test\VersionLabel.txt" "C:\test\Abc.zip"  -Force
Run Code Online (Sandbox Code Playgroud)

错误:压缩文件夹无效

Joh*_*van 10

> = PowerShell 5.0

Per @ SonnyPuijk上面的回答,请使用Compress-Archive.

clear-host
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
Compress-Archive -Path $fileToZip -Update -DestinationPath $zipFN
Run Code Online (Sandbox Code Playgroud)

<PowerShell 5.0

要将单个文件添加到现有zip:

clear-host
Add-Type -assembly 'System.IO.Compression'
Add-Type -assembly 'System.IO.Compression.FileSystem'

[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
[System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile]::Open($zipFN, ([System.IO.Compression.ZipArchiveMode]::Update))
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile, $fileToZip, (Split-Path $fileToZip -Leaf))
$ZipFile.Dispose()
Run Code Online (Sandbox Code Playgroud)

要从头创建单个文件zip文件:

同上,仅替换: [System.IO.Compression.ZipArchiveMode]::Update

附: [System.IO.Compression.ZipArchiveMode]::Create

相关文档:


小智 1

您可以用于Compress-Archive此用途。Copy-Item不支持 zip 文件。

如果您没有 PowerShell v5,您可以使用 7Zip 命令行或 .Net