簡體   English   中英

如何將單個txt文件移動到PowerShell 3中的zip

[英]How to move a single txt file to the zip in powershell 3

我正在嘗試將一個文本文件復制/移動到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

錯誤:壓縮文件夾無效

> = 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

<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()

要從頭創建單個文件zip文件:

與上面相同,僅替換: [System.IO.Compression.ZipArchiveMode]::Update

使用: [System.IO.Compression.ZipArchiveMode]::Create

相關文檔:

您可以使用Compress-Archive進行此操作。 Copy-Item不支持zip文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM